Skip to content

Instantly share code, notes, and snippets.

@piperswe
Created April 17, 2021 01:53
Show Gist options
  • Save piperswe/6be06f58ba3925801b0dcceab22c997b to your computer and use it in GitHub Desktop.
Save piperswe/6be06f58ba3925801b0dcceab22c997b to your computer and use it in GitHub Desktop.
Build multi-architecture Docker images with Nix
#!/bin/sh
# Required on macOS because cctools is marked as broken
export NIXPKGS_ALLOW_BROKEN=1
nix run -f image.nix -c push
docker run ghcr.io/piperswe/hello
{ name ? "ghcr.io/piperswe/hello", cmd ? ({ hello }: "${hello}/bin/hello")
, tagBase ? "latest" }:
let
buildImage = arch:
{ dockerTools, callPackage }:
dockerTools.buildImage {
inherit name;
tag = "${tagBase}-${arch}";
config = { Cmd = [ (callPackage cmd { }) ]; };
};
architectures = [ "i686" "x86_64" "aarch64" "powerpc64le" ];
nixpkgs = import <nixpkgs>;
crossSystems = map (arch: {
inherit arch;
pkgs = (nixpkgs {
crossSystem = { config = "${arch}-unknown-linux-musl"; };
}).pkgsStatic;
}) architectures;
pkgs = nixpkgs { };
lib = pkgs.lib;
images = map ({ arch, pkgs }: rec {
inherit arch;
image = pkgs.callPackage (buildImage arch) { };
tag = "${tagBase}-${arch}";
}) crossSystems;
loadAndPush = builtins.concatStringsSep "\n" (lib.concatMap
({ arch, image, tag }: [
"$docker load -i ${image}"
"$docker push ${name}:${tag}"
]) images);
imageNames = builtins.concatStringsSep " "
(map ({ arch, image, tag }: "${name}:${tag}") images);
in pkgs.writeTextFile {
inherit name;
text = ''
#!${pkgs.stdenv.shell}
set -euxo pipefail
docker=${pkgs.docker}/bin/docker
${loadAndPush}
$docker manifest create --amend ${name}:${tagBase} ${imageNames}
$docker manifest push ${name}:${tagBase}
'';
executable = true;
destination = "/bin/push";
}
@collinarnett
Copy link

Hey I wrote a little flake based on this. You can find it here https://github.com/collinarnett/docker-utils

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment