From 4313ac6b292708158cb413420c8848f7b8012305 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Thu, 19 Nov 2020 14:31:14 +0100 Subject: [PATCH 1/4] dockerTools.buildLayeredImage: Fix cross compilation --- pkgs/build-support/docker/default.nix | 8 ++++---- pkgs/top-level/all-packages.nix | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index ba76ce2b817..b072c291f77 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -48,7 +48,7 @@ let # A user is required by nix # https://github.com/NixOS/nix/blob/9348f9291e5d9e4ba3c4347ea1b235640f54fd79/src/libutil/util.cc#L478 export USER=nobody - ${nix}/bin/nix-store --load-db < ${closureInfo {rootPaths = contentsList;}}/registration + ${buildPackages.nix}/bin/nix-store --load-db < ${closureInfo {rootPaths = contentsList;}}/registration mkdir -p nix/var/nix/gcroots/docker/ for i in ${lib.concatStringsSep " " contentsList}; do @@ -443,7 +443,7 @@ rec { runCommand "${name}.tar.gz" { inherit (stream) imageName; passthru = { inherit (stream) imageTag; }; - buildInputs = [ pigz ]; + nativeBuildInputs = [ pigz ]; } "${stream} | pigz -nT > $out"; # 1. extract the base image @@ -762,7 +762,7 @@ rec { else lib.head (lib.strings.splitString "-" (baseNameOf conf.outPath)); paths = referencesByPopularity overallClosure; - buildInputs = [ jq ]; + nativeBuildInputs = [ jq ]; } '' ${if (tag == null) then '' outName="$(basename "$out")" @@ -826,7 +826,7 @@ rec { # take images can know in advance how the image is supposed to be used. isExe = true; }; - buildInputs = [ makeWrapper ]; + nativeBuildInputs = [ makeWrapper ]; } '' makeWrapper ${streamScript} $out --add-flags ${conf} ''; diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 372aecc9668..7391f0d12a0 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -239,7 +239,7 @@ in grsync = callPackage ../applications/misc/grsync { }; dockerTools = callPackage ../build-support/docker { - writePython3 = writers.writePython3; + writePython3 = buildPackages.writers.writePython3; }; snapTools = callPackage ../build-support/snap { }; From 8a3b33baed9458a0af56a710b535bedf6d6c2598 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Thu, 19 Nov 2020 15:03:44 +0100 Subject: [PATCH 2/4] dockerTools: Set correct architecture when cross compiling --- pkgs/build-support/docker/default.nix | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index b072c291f77..e4e8f794bc4 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -31,6 +31,7 @@ writeScript, writeText, writePython3, + system, # Note: This is the cross system we're compiling for }: # WARNING: this API is unstable and may be subject to backwards-incompatible changes in the future. @@ -56,6 +57,16 @@ let done; ''; + # Map nixpkgs architecture to Docker notation + # Reference: https://github.com/docker-library/official-images#architectures-other-than-amd64 + getArch = nixSystem: { + aarch64-linux = "arm64v8"; + armv7l-linux = "arm32v7"; + x86_64-linux = "amd64"; + powerpc64le-linux = "ppc64le"; + i686-linux = "i386"; + }.${nixSystem} or "Can't map Nix system ${nixSystem} to Docker architecture notation. Please check that your input and your requested build are correct or update the mapping in Nixpkgs."; + in rec { @@ -72,7 +83,7 @@ rec { , imageDigest , sha256 , os ? "linux" - , arch ? buildPackages.go.GOARCH + , arch ? getArch system # This is used to set name to the pulled image , finalImageName ? imageName @@ -488,7 +499,7 @@ rec { baseJson = let pure = writeText "${baseName}-config.json" (builtins.toJSON { inherit created config; - architecture = buildPackages.go.GOARCH; + architecture = getArch system; os = "linux"; }); impure = runCommand "${baseName}-config.json" @@ -715,7 +726,7 @@ rec { streamScript = writePython3 "stream" {} ./stream_layered_image.py; baseJson = writeText "${name}-base.json" (builtins.toJSON { inherit config; - architecture = buildPackages.go.GOARCH; + architecture = getArch system; os = "linux"; }); From 5357abf49a9c19de84ec5333fcd07a10c05585cc Mon Sep 17 00:00:00 2001 From: adisbladis Date: Thu, 19 Nov 2020 17:31:38 +0100 Subject: [PATCH 3/4] dockerTools: Add cross example --- pkgs/build-support/docker/examples.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix index 4a611add8a1..cd91c721241 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -7,7 +7,7 @@ # $ nix-build '' -A dockerTools.examples.redis # $ docker load < result -{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb }: +{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb, pkgsCross }: rec { # 1. basic example @@ -407,4 +407,11 @@ rec { contents = [ pkgs.bash pkgs.coreutils ] ++ nonRootShadowSetup { uid = 999; user = "somebody"; }; }; + # basic example, with cross compilation + cross-aarch64 = pkgsCross.aarch64-multiplatform.dockerTools.buildImage { + name = "hello-cross"; + tag = "latest"; + contents = pkgsCross.aarch64-multiplatform.hello; + }; + } From 11367b2db107f318c5ca5634a94c85d185f2c498 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Thu, 19 Nov 2020 18:12:36 +0100 Subject: [PATCH 4/4] dockerTools: Add cross compilation test --- nixos/tests/docker-tools.nix | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index c1c41b0fc11..a20a08fc90d 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -234,5 +234,17 @@ import ./make-test-python.nix ({ pkgs, ... }: { "docker run --rm file-in-store nix-store --verify --check-contents", "docker run --rm file-in-store |& grep 'some data'", ) + + with subtest("Ensure cross compiled image can be loaded and has correct arch."): + docker.succeed( + "docker load --input='${pkgs.dockerTools.examples.cross-aarch64}'", + ) + assert ( + docker.succeed( + "docker inspect ${pkgs.dockerTools.examples.cross-aarch64.imageName} " + + "| ${pkgs.jq}/bin/jq -r .[].Architecture" + ).strip() + == "arm64v8" + ) ''; })