Created
April 17, 2021 01:53
-
-
Save piperswe/6be06f58ba3925801b0dcceab22c997b to your computer and use it in GitHub Desktop.
Build multi-architecture Docker images with Nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey I wrote a little flake based on this. You can find it here https://github.com/collinarnett/docker-utils