From 419a4fa596577667271fcfc057bc9063ddbffe47 Mon Sep 17 00:00:00 2001 From: Louis Blin <45168934+lbpdt@users.noreply.github.com> Date: Tue, 9 Mar 2021 18:32:54 +0000 Subject: [PATCH] dockerTools.buildLayeredImage: image names with registry/ prefix When using `buildLayeredImage`, it is not possible to specify an image name of the form `/my/image`, although it is a valid name. This is due to derivations under `buildLayeredImage` using that image name as their derivation name, but slashes are not permitted in that context. A while ago, #13099 fixed that exact same problem in `buildImage` by using `baseNameOf name` in derivation names instead of `name`. This change does the same thing for `buildLayeredImage`. --- nixos/tests/docker-tools.nix | 16 ++++++++++++++++ pkgs/build-support/docker/default.nix | 12 +++++++----- pkgs/build-support/docker/examples.nix | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index 6638ec4927c..1cc554d002b 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -254,5 +254,21 @@ import ./make-test-python.nix ({ pkgs, ... }: { "docker run --rm ${examples.layeredStoreSymlink.imageName} bash -c 'test -L ${examples.layeredStoreSymlink.passthru.symlink}'", "docker rmi ${examples.layeredStoreSymlink.imageName}", ) + + with subtest("buildImage supports registry/ prefix in image name"): + docker.succeed( + "docker load --input='${examples.prefixedImage}'" + ) + docker.succeed( + "docker images --format '{{.Repository}}' | grep -F '${examples.prefixedImage.imageName}'" + ) + + with subtest("buildLayeredImage supports registry/ prefix in image name"): + docker.succeed( + "docker load --input='${examples.prefixedLayeredImage}'" + ) + docker.succeed( + "docker images --format '{{.Repository}}' | grep -F '${examples.prefixedLayeredImage.imageName}'" + ) ''; }) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index e9014a88954..654f33e3c67 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -447,7 +447,7 @@ rec { let stream = streamLayeredImage args; in - runCommand "${name}.tar.gz" { + runCommand "${baseNameOf name}.tar.gz" { inherit (stream) imageName; passthru = { inherit (stream) imageTag; }; nativeBuildInputs = [ pigz ]; @@ -746,8 +746,10 @@ rec { (lib.assertMsg (maxLayers > 1) "the maxLayers argument of dockerTools.buildLayeredImage function must be greather than 1 (current value: ${toString maxLayers})"); let + baseName = baseNameOf name; + streamScript = writePython3 "stream" {} ./stream_layered_image.py; - baseJson = writeText "${name}-base.json" (builtins.toJSON { + baseJson = writeText "${baseName}-base.json" (builtins.toJSON { inherit config; architecture = defaultArch; os = "linux"; @@ -759,7 +761,7 @@ rec { # things like permissions set on 'extraCommands' are not overriden # by Nix. Then we precompute the sha256 for performance. customisationLayer = symlinkJoin { - name = "${name}-customisation-layer"; + name = "${baseName}-customisation-layer"; paths = contentsList; inherit extraCommands; postBuild = '' @@ -788,7 +790,7 @@ rec { # so they'll be excluded from the created images. unnecessaryDrvs = [ baseJson overallClosure ]; - conf = runCommand "${name}-conf.json" { + conf = runCommand "${baseName}-conf.json" { inherit maxLayers created; imageName = lib.toLower name; passthru.imageTag = @@ -852,7 +854,7 @@ rec { --arg created "$created" | tee $out ''; - result = runCommand "stream-${name}" { + result = runCommand "stream-${baseName}" { inherit (conf) imageName; passthru = { inherit (conf) imageTag; diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix index 86375a40baa..9e33a42af23 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -427,4 +427,18 @@ rec { tag = "latest"; contents = [ pkgs.bash symlink ]; } // { passthru = { inherit symlink; }; }; + + # image with registry/ prefix + prefixedImage = pkgs.dockerTools.buildImage { + name = "registry-1.docker.io/image"; + tag = "latest"; + config.Cmd = [ "${pkgs.hello}/bin/hello" ]; + }; + + # layered image with registry/ prefix + prefixedLayeredImage = pkgs.dockerTools.buildLayeredImage { + name = "registry-1.docker.io/layered-image"; + tag = "latest"; + config.Cmd = [ "${pkgs.hello}/bin/hello" ]; + }; }