Merge pull request #104271 from adisbladis/dockertools-cross

dockerTools.buildLayeredImage: Fix cross compilation
This commit is contained in:
Robert Hensing 2020-11-19 20:41:53 +01:00 committed by GitHub
commit c68e739300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 9 deletions

View File

@ -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"
)
'';
})

View File

@ -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.
@ -48,7 +49,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
@ -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
@ -443,7 +454,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
@ -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";
});
@ -762,7 +773,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 +837,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}
'';

View File

@ -7,7 +7,7 @@
# $ nix-build '<nixpkgs>' -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;
};
}

View File

@ -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 { };