From 0e9bc9ffd107c288571af4e3d4a9c2a6b64cf505 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Oct 2021 15:53:30 +0200 Subject: [PATCH] dockerTools: Add fakechroot to fakeRootCommands --- doc/builders/images/dockertools.section.md | 8 +++++- .../from_md/release-notes/rl-2205.section.xml | 8 ++++++ .../manual/release-notes/rl-2205.section.md | 4 +++ nixos/tests/docker-tools.nix | 5 ++++ pkgs/build-support/docker/default.nix | 26 ++++++++++++++++--- pkgs/build-support/docker/examples.nix | 13 ++++++++++ 6 files changed, 59 insertions(+), 5 deletions(-) diff --git a/doc/builders/images/dockertools.section.md b/doc/builders/images/dockertools.section.md index bfe1d17a6067..af09f2522234 100644 --- a/doc/builders/images/dockertools.section.md +++ b/doc/builders/images/dockertools.section.md @@ -149,7 +149,13 @@ Create a Docker image with many of the store paths being on their own layer to i `fakeRootCommands` _optional_ -: Shell commands to run while creating the archive for the final layer in a fakeroot environment. Unlike `extraCommands`, you can run `chown` to change the owners of the files in the archive, changing fakeroot's state instead of the real filesystem. The latter would require privileges that the build user does not have. Static binaries do not interact with the fakeroot environment. By default all files in the archive will be owned by root. +: Shell commands to run while creating the archive for the final layer in a fakeroot + fakechroot environment. Unlike `extraCommands`, you can run `chown` to change the owners of the files in the archive, changing fakeroot's state instead of the real filesystem. The latter would require privileges that the build user does not have. Static binaries do not interact with the fakeroot environment. By default all files in the archive will be owned by root. + +`enableFakechroot` _optional_ + +: Whether to run in `fakeRootCommands` in `fakechroot`, making programs behave as though `/` is the root of the image being created, while files in the Nix store are available as usual. This allows most scripts that perform installation in `/` to work as expected. Considering that `fakechroot` is implemented via the same mechanism as `fakeroot`, it is not guaranteed to work and will not work for static binaries. + + *Default:* `true` when built on Linux, `false` otherwise ### Behavior of `contents` in the final image {#dockerTools-buildLayeredImage-arg-contents} diff --git a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml index 4752cad6c7b0..5e6a89dc981d 100644 --- a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml +++ b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml @@ -57,6 +57,14 @@ new versions will release. + + + pkgs.dockerTools.buildLayeredImage/streamLayeredImage + enable enableFakechroot by default on + Linux. This might be unexpected and can be set to + false if image generation fails. + +
diff --git a/nixos/doc/manual/release-notes/rl-2205.section.md b/nixos/doc/manual/release-notes/rl-2205.section.md index b0526a1fb3b4..b774b2af2f71 100644 --- a/nixos/doc/manual/release-notes/rl-2205.section.md +++ b/nixos/doc/manual/release-notes/rl-2205.section.md @@ -27,4 +27,8 @@ In addition to numerous new and upgraded packages, this release has the followin org-contrib, refer to the ones in `pkgs.emacsPackages.elpaPackages` and `pkgs.emacsPackages.nongnuPackages` where the new versions will release. +* `pkgs.dockerTools.buildLayeredImage`/`streamLayeredImage` enable + `enableFakechroot` by default on Linux. + This might be unexpected and can be set to `false` if image generation fails. + ## Other Notable Changes {#sec-release-22.05-notable-changes} diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index f3858b8bd81e..19ebed3ebd0b 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -396,6 +396,11 @@ import ./make-test-python.nix ({ pkgs, ... }: { "tar -tf ${examples.exportBash} | grep '\./bin/bash' > /dev/null" ) + with subtest("layered image fakeRootCommands with fakechroot works"): + docker.succeed("${examples.imageViaFakeChroot} | docker load") + docker.succeed("docker run --rm image-via-fake-chroot | grep -i hello") + docker.succeed("docker image rm image-via-fake-chroot:latest") + with subtest("Ensure bare paths in contents are loaded correctly"): docker.succeed( "docker load --input='${examples.build-image-with-path}'", diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index 7958db917122..a6d3109bf6d3 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -5,6 +5,7 @@ , closureInfo , coreutils , e2fsprogs +, fakechroot , fakeroot , findutils , go @@ -34,6 +35,10 @@ }: let + inherit (lib) + optionals + optionalString + ; inherit (lib) escapeShellArgs @@ -811,6 +816,9 @@ rec { , # Optional bash script to run inside fakeroot environment. # Could be used for changing ownership of files in customisation layer. fakeRootCommands ? "" + , # Whether to run fakeRootCommands in fakechroot as well, so that they + # appear to run inside the image, but have access to the normal Nix store. + enableFakechroot ? pkgs.stdenv.buildPlatform.isLinux , # We pick 100 to ensure there is plenty of room for extension. I # believe the actual maximum is 128. maxLayers ? 100 @@ -842,16 +850,26 @@ rec { name = "${baseName}-customisation-layer"; paths = contentsList; inherit extraCommands fakeRootCommands; - nativeBuildInputs = [ fakeroot ]; + nativeBuildInputs = [ + fakeroot + ] ++ optionals enableFakechroot [ + fakechroot + # for chroot + coreutils + # fakechroot needs getopt, which is provided by util-linux + util-linux + ]; postBuild = '' mv $out old_out (cd old_out; eval "$extraCommands" ) mkdir $out - - fakeroot bash -c ' + ${optionalString enableFakechroot '' + export FAKECHROOT_EXCLUDE_PATH=/dev:/proc:/sys:${builtins.storeDir}:$out/layer.tar + ''} + ${optionalString enableFakechroot ''fakechroot chroot $PWD/old_out ''}fakeroot bash -c ' source $stdenv/setup - cd old_out + ${optionalString (!enableFakechroot) ''cd old_out''} eval "$fakeRootCommands" tar \ --sort name \ diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix index bef0c5848693..28b26be1f8a7 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -562,6 +562,19 @@ rec { # Example export of the bash image exportBash = pkgs.dockerTools.exportImage { fromImage = bash; }; + imageViaFakeChroot = pkgs.dockerTools.streamLayeredImage { + name = "image-via-fake-chroot"; + tag = "latest"; + config.Cmd = [ "hello" ]; + # Crucially, instead of a relative path, this creates /bin, which is + # intercepted by fakechroot. + # This functionality is not available on darwin as of 2021. + fakeRootCommands = '' + mkdir /bin + ln -s ${pkgs.hello}/bin/hello /bin/hello + ''; + }; + build-image-with-path = buildImage { name = "build-image-with-path"; tag = "latest";