Merge pull request #28561 from nlewo/dockerTools-load-nix-db
dockerTools: populate the nix database of the container nix store
This commit is contained in:
commit
38f65be5c3
@ -10,6 +10,8 @@
|
|||||||
lib,
|
lib,
|
||||||
pkgs,
|
pkgs,
|
||||||
pigz,
|
pigz,
|
||||||
|
nixUnstable,
|
||||||
|
perl,
|
||||||
runCommand,
|
runCommand,
|
||||||
rsync,
|
rsync,
|
||||||
shadow,
|
shadow,
|
||||||
@ -27,7 +29,7 @@
|
|||||||
rec {
|
rec {
|
||||||
|
|
||||||
examples = import ./examples.nix {
|
examples = import ./examples.nix {
|
||||||
inherit pkgs buildImage pullImage shadowSetup;
|
inherit pkgs buildImage pullImage shadowSetup buildImageWithNixDb;
|
||||||
};
|
};
|
||||||
|
|
||||||
pullImage =
|
pullImage =
|
||||||
@ -239,6 +241,19 @@ rec {
|
|||||||
${text}
|
${text}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
nixRegistration = contents: runCommand "nix-registration" {
|
||||||
|
buildInputs = [ nixUnstable perl ];
|
||||||
|
# For obtaining the closure of `contents'.
|
||||||
|
exportReferencesGraph =
|
||||||
|
let contentsList = if builtins.isList contents then contents else [ contents ];
|
||||||
|
in map (x: [("closure-" + baseNameOf x) x]) contentsList;
|
||||||
|
}
|
||||||
|
''
|
||||||
|
mkdir $out
|
||||||
|
printRegistration=1 perl ${pkgs.pathsFromGraph} closure-* > $out/db.dump
|
||||||
|
perl ${pkgs.pathsFromGraph} closure-* > $out/storePaths
|
||||||
|
'';
|
||||||
|
|
||||||
# Create a "layer" (set of files).
|
# Create a "layer" (set of files).
|
||||||
mkPureLayer = {
|
mkPureLayer = {
|
||||||
# Name of the layer
|
# Name of the layer
|
||||||
@ -544,4 +559,37 @@ rec {
|
|||||||
|
|
||||||
in
|
in
|
||||||
result;
|
result;
|
||||||
|
|
||||||
|
# Build an image and populate its nix database with the provided
|
||||||
|
# contents. The main purpose is to be able to use nix commands in
|
||||||
|
# the container.
|
||||||
|
# Be careful since this doesn't work well with multilayer.
|
||||||
|
buildImageWithNixDb = args@{ contents ? null, extraCommands ? "", ... }:
|
||||||
|
buildImage (args // {
|
||||||
|
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."
|
||||||
|
# This requires Nix 1.12 or higher
|
||||||
|
export NIX_REMOTE=local?root=$PWD
|
||||||
|
${nixUnstable}/bin/nix-store --load-db < ${nixRegistration contents}/db.dump
|
||||||
|
|
||||||
|
# We fill the store in order to run the 'verify' command that
|
||||||
|
# generates hash and size of output paths.
|
||||||
|
# Note when Nix 1.12 is be the stable one, the database dump
|
||||||
|
# generated by the exportReferencesGraph function will
|
||||||
|
# contains sha and size. See
|
||||||
|
# https://github.com/NixOS/nix/commit/c2b0d8749f7e77afc1c4b3e8dd36b7ee9720af4a
|
||||||
|
storePaths=$(cat ${nixRegistration contents}/storePaths)
|
||||||
|
echo "Copying everything to /nix/store (will take a while)..."
|
||||||
|
cp -prd $storePaths nix/store/
|
||||||
|
${nixUnstable}/bin/nix-store --verify --check-contents
|
||||||
|
|
||||||
|
mkdir -p nix/var/nix/gcroots/docker/
|
||||||
|
for i in ${lib.concatStringsSep " " contents}; do
|
||||||
|
ln -s $i nix/var/nix/gcroots/docker/$(basename $i)
|
||||||
|
done;
|
||||||
|
'' + extraCommands;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
|
# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
|
||||||
# $ docker load < result
|
# $ docker load < result
|
||||||
|
|
||||||
{ pkgs, buildImage, pullImage, shadowSetup }:
|
{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb }:
|
||||||
|
|
||||||
rec {
|
rec {
|
||||||
# 1. basic example
|
# 1. basic example
|
||||||
@ -83,7 +83,7 @@ rec {
|
|||||||
};
|
};
|
||||||
|
|
||||||
# 4. example of pulling an image. could be used as a base for other images
|
# 4. example of pulling an image. could be used as a base for other images
|
||||||
nix = pullImage {
|
nixFromDockerHub = pullImage {
|
||||||
imageName = "nixos/nix";
|
imageName = "nixos/nix";
|
||||||
imageTag = "1.11";
|
imageTag = "1.11";
|
||||||
# this hash will need change if the tag is updated at docker hub
|
# this hash will need change if the tag is updated at docker hub
|
||||||
@ -101,4 +101,17 @@ rec {
|
|||||||
pkgs.nano
|
pkgs.nano
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# 5. nix example to play with the container nix store
|
||||||
|
# docker run -it --rm nix nix-store -qR $(nix-build '<nixpkgs>' -A nix)
|
||||||
|
nix = buildImageWithNixDb {
|
||||||
|
name = "nix";
|
||||||
|
contents = [
|
||||||
|
# nix-store -qR uses the 'more' program which is not included in
|
||||||
|
# the pkgs.nix dependencies. We then have to manually get it
|
||||||
|
# from the 'eject' package:/
|
||||||
|
pkgs.eject
|
||||||
|
pkgs.nix
|
||||||
|
];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user