diff --git a/doc/builders/images/dockertools.section.md b/doc/builders/images/dockertools.section.md index bfe1d17a6067..7ff4b2aeb369 100644 --- a/doc/builders/images/dockertools.section.md +++ b/doc/builders/images/dockertools.section.md @@ -151,6 +151,12 @@ Create a Docker image with many of the store paths being on their own layer to i : 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. +`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 scripts that perform installation in `/` to work as expected. Considering that `fakechroot` is implemented via the same mechanism as `fakeroot`, the same caveats apply. + + *Default:* `false` + ### Behavior of `contents` in the final image {#dockerTools-buildLayeredImage-arg-contents} Each path directly listed in `contents` will have a symlink in the root of the image. 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..9a20df57777c 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,10 @@ 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. + # Perhaps this could be enabled on by default on pkgs.stdenv.buildPlatform.isLinux + enableFakechroot ? false , # We pick 100 to ensure there is plenty of room for extension. I # believe the actual maximum is 128. maxLayers ? 100 @@ -842,16 +851,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..f2d4f809ae4e 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -562,6 +562,20 @@ 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" ]; + enableFakechroot = true; + # 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";