3
0
Fork 0
forked from mirrors/nixpkgs

dockerTools: Add fakechroot to fakeRootCommands

This commit is contained in:
Robert Hensing 2021-10-01 15:53:30 +02:00
parent 66c19d856b
commit 0e9bc9ffd1
6 changed files with 59 additions and 5 deletions

View file

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

View file

@ -57,6 +57,14 @@
new versions will release.
</para>
</listitem>
<listitem>
<para>
<literal>pkgs.dockerTools.buildLayeredImage</literal>/<literal>streamLayeredImage</literal>
enable <literal>enableFakechroot</literal> by default on
Linux. This might be unexpected and can be set to
<literal>false</literal> if image generation fails.
</para>
</listitem>
</itemizedlist>
</section>
<section xml:id="sec-release-22.05-notable-changes">

View file

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

View file

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

View file

@ -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 \

View file

@ -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";