@@ -10,6 +10,8 @@
1010 lib,
1111 pkgs,
1212 pigz,
13+ nixUnstable,
14+ perl,
1315 runCommand,
1416 rsync,
1517 shadow,
@@ -27,7 +29,7 @@
2729rec {
2830
2931 examples = import ./examples.nix {
30− inherit pkgs buildImage pullImage shadowSetup;
32+ inherit pkgs buildImage pullImage shadowSetup buildImageWithNixDb;
3133 };
3234
3335 pullImage =
@@ -239,6 +241,17 @@ rec {
239241 ${text}
240242 '';
241243
244+ nixRegistration = contents: runCommand "nix-registration" {
245+ buildInputs = [ nixUnstable perl ];
246+ # For obtaining the closure of `contents'.
247+ exportReferencesGraph =
248+ let contentsList = if builtins.isList contents then contents else [ contents ];
249+ in map (x: [("closure-" + baseNameOf x) x]) contentsList;
250+ }
251+ ''
252+ printRegistration=1 perl ${pkgs.pathsFromGraph} closure-* > $out
253+ '';
254+
242255 # Create a "layer" (set of files).
243256 mkPureLayer = {
244257 # Name of the layer
@@ -544,4 +557,21 @@ rec {
544557
545558 in
546559 result;
560+
561+ # Build an image and populate its nix database with the provided
562+ # contents. The main purpose is to be able to use nix commands in
563+ # the container.
564+ # Be careful since this doesn't work well with multilayer.
565+ buildImageWithNixDb = args@{ contents ? null, extraCommands ? "", ... }:
566+ buildImage (args // {
567+ extraCommands = ''
568+ echo "Generating the nix database..."
569+ echo "Warning: only the database of the deepest Nix layer is loaded."
570+ echo " If you want to use nix commands in the container, it would"
571+ echo " be better to only have one layer that contains a nix store."
572+ # This requires Nix 1.12 or higher
573+ export NIX_REMOTE=local?root=$PWD
574+ ${nixUnstable}/bin/nix-store --load-db < ${nixRegistration contents}
575+ '' + extraCommands;
576+ });
547577}