git.iko.soy/typeable/nixpkgs
on master help sign in
dockerTools.buildImageWithNixDb: populate the Nix Db of the image Nix store

Currently, the contents closure is copied to the layer but there is no nix database initialization. If pkgs.nix is added in the contents, nix-store doesn't work because there is no nix database. From the contents of the layer, this commit generates and loads the database in the nix store of the container. This only works if there is no parent layer that already have a nix store (to support several nix layers, we would have to merge nix databases of parent layers). We also add an example to play with the nix store inside the container. Note it seems `more` is a missing dependency of the nix package!

commitdf589a438e01c06a6661cbd18bfe8f568d8b7f83 authorAntoine Eiche <lewo@abesis.fr> dateFri 25 Aug 2017, 11:47 (9 years ago) parent2c2cd34
2 files changed+46−3
pkgs/build-support/docker/default.nix+31−1 pkgs/build-support/docker/examples.nix+15−2
pkgs/build-support/docker/default.nix+31−1
@@ -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}
pkgs/build-support/docker/examples.nix+15−2
@@ -7,7 +7,7 @@
77# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
88# $ docker load < result
99
10−{ pkgs, buildImage, pullImage, shadowSetup }:
10+{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb }:
1111
1212rec {
1313 # 1. basic example
@@ -83,7 +83,7 @@ rec {
8383 };
8484
8585 # 4. example of pulling an image. could be used as a base for other images
86− nix = pullImage {
86+ nixFromDockerHub = pullImage {
8787 imageName = "nixos/nix";
8888 imageTag = "1.11";
8989 # this hash will need change if the tag is updated at docker hub
@@ -101,4 +101,17 @@ rec {
101101 pkgs.nano
102102 ];
103103 };
104+
105+ # 5. nix example to play with the container nix store
106+ # docker run -it --rm nix nix-store -qR $(nix-build '<nixpkgs>' -A nix)
107+ nix = buildImageWithNixDb {
108+ name = "nix";
109+ contents = [
110+ # nix-store -qR uses the 'more' program which is not included in
111+ # the pkgs.nix dependencies. We then have to manually get it
112+ # from the 'eject' package:/
113+ pkgs.eject
114+ pkgs.nix
115+ ];
116+ };
104117}