git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #104271 from adisbladis/dockertools-cross

dockerTools.buildLayeredImage: Fix cross compilation

commitc68e739300d551a755e04dcca6fca1f3dbb21421 authorRobert Hensing <roberth@users.noreply.github.com> dateThu 19 Nov 2020, 20:41 (6 years ago) parentc81c3c311367b2
4 files changed+39−9
nixos/tests/docker-tools.nix+12 pkgs/build-support/docker/default.nix+18−7 pkgs/build-support/docker/examples.nix+8−1 pkgs/top-level/all-packages.nix+1−1
nixos/tests/docker-tools.nix+12
@@ -234,5 +234,17 @@ import ./make-test-python.nix ({ pkgs, ... }: {
234234 "docker run --rm file-in-store nix-store --verify --check-contents",
235235 "docker run --rm file-in-store |& grep 'some data'",
236236 )
237+
238+ with subtest("Ensure cross compiled image can be loaded and has correct arch."):
239+ docker.succeed(
240+ "docker load --input='${pkgs.dockerTools.examples.cross-aarch64}'",
241+ )
242+ assert (
243+ docker.succeed(
244+ "docker inspect ${pkgs.dockerTools.examples.cross-aarch64.imageName} "
245+ + "| ${pkgs.jq}/bin/jq -r .[].Architecture"
246+ ).strip()
247+ == "arm64v8"
248+ )
237249 '';
238250})
pkgs/build-support/docker/default.nix+18−7
@@ -31,6 +31,7 @@
3131 writeScript,
3232 writeText,
3333 writePython3,
34+ system, # Note: This is the cross system we're compiling for
3435}:
3536
3637# WARNING: this API is unstable and may be subject to backwards-incompatible changes in the future.
@@ -48,7 +49,7 @@ let
4849 # A user is required by nix
4950 # https://github.com/NixOS/nix/blob/9348f9291e5d9e4ba3c4347ea1b235640f54fd79/src/libutil/util.cc#L478
5051 export USER=nobody
51− ${nix}/bin/nix-store --load-db < ${closureInfo {rootPaths = contentsList;}}/registration
52+ ${buildPackages.nix}/bin/nix-store --load-db < ${closureInfo {rootPaths = contentsList;}}/registration
5253
5354 mkdir -p nix/var/nix/gcroots/docker/
5455 for i in ${lib.concatStringsSep " " contentsList}; do
@@ -56,6 +57,16 @@ let
5657 done;
5758 '';
5859
60+ # Map nixpkgs architecture to Docker notation
61+ # Reference: https://github.com/docker-library/official-images#architectures-other-than-amd64
62+ getArch = nixSystem: {
63+ aarch64-linux = "arm64v8";
64+ armv7l-linux = "arm32v7";
65+ x86_64-linux = "amd64";
66+ powerpc64le-linux = "ppc64le";
67+ i686-linux = "i386";
68+ }.${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.";
69+
5970in
6071rec {
6172
@@ -72,7 +83,7 @@ rec {
7283 , imageDigest
7384 , sha256
7485 , os ? "linux"
75− , arch ? buildPackages.go.GOARCH
86+ , arch ? getArch system
7687
7788 # This is used to set name to the pulled image
7889 , finalImageName ? imageName
@@ -443,7 +454,7 @@ rec {
443454 runCommand "${name}.tar.gz" {
444455 inherit (stream) imageName;
445456 passthru = { inherit (stream) imageTag; };
446− buildInputs = [ pigz ];
457+ nativeBuildInputs = [ pigz ];
447458 } "${stream} | pigz -nT > $out";
448459
449460 # 1. extract the base image
@@ -488,7 +499,7 @@ rec {
488499 baseJson = let
489500 pure = writeText "${baseName}-config.json" (builtins.toJSON {
490501 inherit created config;
491− architecture = buildPackages.go.GOARCH;
502+ architecture = getArch system;
492503 os = "linux";
493504 });
494505 impure = runCommand "${baseName}-config.json"
@@ -715,7 +726,7 @@ rec {
715726 streamScript = writePython3 "stream" {} ./stream_layered_image.py;
716727 baseJson = writeText "${name}-base.json" (builtins.toJSON {
717728 inherit config;
718− architecture = buildPackages.go.GOARCH;
729+ architecture = getArch system;
719730 os = "linux";
720731 });
721732
@@ -762,7 +773,7 @@ rec {
762773 else
763774 lib.head (lib.strings.splitString "-" (baseNameOf conf.outPath));
764775 paths = referencesByPopularity overallClosure;
765− buildInputs = [ jq ];
776+ nativeBuildInputs = [ jq ];
766777 } ''
767778 ${if (tag == null) then ''
768779 outName="$(basename "$out")"
@@ -826,7 +837,7 @@ rec {
826837 # take images can know in advance how the image is supposed to be used.
827838 isExe = true;
828839 };
829− buildInputs = [ makeWrapper ];
840+ nativeBuildInputs = [ makeWrapper ];
830841 } ''
831842 makeWrapper ${streamScript} $out --add-flags ${conf}
832843 '';
pkgs/build-support/docker/examples.nix+8−1
@@ -7,7 +7,7 @@
77# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
88# $ docker load < result
99
10−{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb }:
10+{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb, pkgsCross }:
1111
1212rec {
1313 # 1. basic example
@@ -407,4 +407,11 @@ rec {
407407 contents = [ pkgs.bash pkgs.coreutils ] ++ nonRootShadowSetup { uid = 999; user = "somebody"; };
408408 };
409409
410+ # basic example, with cross compilation
411+ cross-aarch64 = pkgsCross.aarch64-multiplatform.dockerTools.buildImage {
412+ name = "hello-cross";
413+ tag = "latest";
414+ contents = pkgsCross.aarch64-multiplatform.hello;
415+ };
416+
410417}
pkgs/top-level/all-packages.nix+1−1
@@ -239,7 +239,7 @@ in
239239 grsync = callPackage ../applications/misc/grsync { };
240240
241241 dockerTools = callPackage ../build-support/docker {
242− writePython3 = writers.writePython3;
242+ writePython3 = buildPackages.writers.writePython3;
243243 };
244244
245245 snapTools = callPackage ../build-support/snap { };