about summary refs log tree commit diff
path: root/nixos/lib
diff options
context:
space:
mode:
authorDan Peebles <pumpkin@me.com>2017-02-22 23:47:24 +0000
committerDan Peebles <pumpkin@me.com>2017-02-22 23:49:49 +0000
commit49641e0de006fdc5edd11292b1e94410c3bb8d79 (patch)
tree180b17aa8e5df508f4cecdb8306c91cba4352887 /nixos/lib
parentdf4c0aeff864f8d3308afc1f4386895fcd3e990d (diff)
downloadnixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar
nixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.gz
nixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.bz2
nixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.lz
nixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.xz
nixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.tar.zst
nixlib-49641e0de006fdc5edd11292b1e94410c3bb8d79.zip
make-disk-image.nix: support additional filesystem contents
This makes make-disk-image.nix slightly more consistent with other image
builders we have. Unfortunately I duplicated some code in doing so, but
this is temporary duplication on the path to consolidating everything.
See https://github.com/NixOS/nixpkgs/issues/23052 for more details on that.

I'm also exposing the option in the amazon-image.nix maintainer module.
Diffstat (limited to 'nixos/lib')
-rw-r--r--nixos/lib/make-disk-image.nix47
1 files changed, 46 insertions, 1 deletions
diff --git a/nixos/lib/make-disk-image.nix b/nixos/lib/make-disk-image.nix
index e279803f2ea0..d9d76b5919a5 100644
--- a/nixos/lib/make-disk-image.nix
+++ b/nixos/lib/make-disk-image.nix
@@ -7,6 +7,12 @@
 , # The size of the disk, in megabytes.
   diskSize
 
+  # The files and directories to be placed in the target file system.
+  # This is a list of attribute sets {source, target} where `source'
+  # is the file system object (regular file or directory) to be
+  # grafted in the file system at path `target'.
+, contents ? []
+
 , # Whether the disk should be partitioned (with a single partition
   # containing the root filesystem) or contain the root filesystem
   # directly.
@@ -45,7 +51,14 @@ pkgs.vmTools.runInLinuxVM (
           ${pkgs.vmTools.qemu}/bin/qemu-img create -f ${format} $diskImage "${toString diskSize}M"
           mv closure xchg/
         '';
-      buildInputs = [ pkgs.utillinux pkgs.perl pkgs.e2fsprogs pkgs.parted ];
+      buildInputs = with pkgs; [ utillinux perl e2fsprogs parted rsync ];
+
+      # I'm preserving the line below because I'm going to search for it across nixpkgs to consolidate
+      # image building logic. The comment right below this now appears in 4 different places in nixpkgs :)
+      # !!! should use XML.
+      sources = map (x: x.source) contents;
+      targets = map (x: x.target) contents;
+
       exportReferencesGraph =
         [ "closure" config.system.build.toplevel ];
       inherit postVM;
@@ -98,6 +111,38 @@ pkgs.vmTools.runInLinuxVM (
       # Remove /etc/machine-id so that each machine cloning this image will get its own id
       rm -f /mnt/etc/machine-id
 
+      # Copy arbitrary other files into the image
+      # Semi-shamelessly copied from make-etc.sh. I (@copumpkin) shall factor this stuff out as part of
+      # https://github.com/NixOS/nixpkgs/issues/23052.
+      set -f
+      sources_=($sources)
+      targets_=($targets)
+      set +f
+
+      for ((i = 0; i < ''${#targets_[@]}; i++)); do
+        source="''${sources_[$i]}"
+        target="''${targets_[$i]}"
+
+        if [[ "$source" =~ '*' ]]; then
+
+          # If the source name contains '*', perform globbing.
+          mkdir -p /mnt/$target
+          for fn in $source; do
+            rsync -a --no-o --no-g "$fn" /mnt/$target/
+          done
+
+        else
+
+          mkdir -p /mnt/$(dirname $target)
+          if ! [ -e /mnt/$target ]; then
+            rsync -a --no-o --no-g $source /mnt/$target
+          else
+            echo "duplicate entry $target -> $source"
+            exit 1
+          fi
+        fi
+      done
+
       umount /mnt
 
       # Make sure resize2fs works