Merge pull request #86595 from adisbladis/buildLayeredImageWithNixDb

dockertools: Add a buildLayeredImageWithNixDb function
This commit is contained in:
adisbladis 2020-05-02 22:06:59 +02:00 committed by GitHub
commit a79dee974c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,7 +32,29 @@
}: }:
# WARNING: this API is unstable and may be subject to backwards-incompatible changes in the future. # WARNING: this API is unstable and may be subject to backwards-incompatible changes in the future.
let
mkDbExtraCommand = contents: let
contentsList = if builtins.isList contents then contents else [ contents ];
in ''
echo "Generating the nix database..."
echo "Warning: only the database of the deepest Nix layer is loaded."
echo " If you want to use nix commands in the container, it would"
echo " be better to only have one layer that contains a nix store."
export NIX_REMOTE=local?root=$PWD
# 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
mkdir -p nix/var/nix/gcroots/docker/
for i in ${lib.concatStringsSep " " contentsList}; do
ln -s $i nix/var/nix/gcroots/docker/$(basename $i)
done;
'';
in
rec { rec {
examples = callPackage ./examples.nix { examples = callPackage ./examples.nix {
@ -874,25 +896,16 @@ rec {
# contents. The main purpose is to be able to use nix commands in # contents. The main purpose is to be able to use nix commands in
# the container. # the container.
# Be careful since this doesn't work well with multilayer. # Be careful since this doesn't work well with multilayer.
buildImageWithNixDb = args@{ contents ? null, extraCommands ? "", ... }: buildImageWithNixDb = args@{ contents ? null, extraCommands ? "", ... }: (
let contentsList = if builtins.isList contents then contents else [ contents ]; buildImage (args // {
in buildImage (args // { extraCommands = (mkDbExtraCommand contents) + extraCommands;
extraCommands = '' })
echo "Generating the nix database..." );
echo "Warning: only the database of the deepest Nix layer is loaded."
echo " If you want to use nix commands in the container, it would"
echo " be better to only have one layer that contains a nix store."
export NIX_REMOTE=local?root=$PWD buildLayeredImageWithNixDb = args@{ contents ? null, extraCommands ? "", ... }: (
# A user is required by nix buildLayeredImage (args // {
# https://github.com/NixOS/nix/blob/9348f9291e5d9e4ba3c4347ea1b235640f54fd79/src/libutil/util.cc#L478 extraCommands = (mkDbExtraCommand contents) + extraCommands;
export USER=nobody })
${nix}/bin/nix-store --load-db < ${closureInfo {rootPaths = contentsList;}}/registration );
mkdir -p nix/var/nix/gcroots/docker/
for i in ${lib.concatStringsSep " " contentsList}; do
ln -s $i nix/var/nix/gcroots/docker/$(basename $i)
done;
'' + extraCommands;
});
} }