@@ -1,9 +1,14 @@
11# Builds an ext4 image containing a populated /nix/store with the closure
2−# of store paths passed in the storePaths parameter. The generated image
3−# is sized to only fit its contents, with the expectation that a script
4−# resizes the filesystem at boot time.
2+# of store paths passed in the storePaths parameter, in addition to the
3+# contents of a directory that can be populated with commands. The
4+# generated image is sized to only fit its contents, with the expectation
5+# that a script resizes the filesystem at boot time.
56{ pkgs
7+# List of derivations to be included
68, storePaths
9+# Shell commands to populate the ./files directory.
10+# All files in that directory are copied to the root of the FS.
11+, populateImageCommands ? ""
712, volumeLabel
813, uuid ? "44444444-4444-4444-8888-888888888888"
914, e2fsprogs
@@ -23,13 +28,17 @@ pkgs.stdenv.mkDerivation {
2328
2429 buildCommand =
2530 ''
31+ (
32+ mkdir -p ./files
33+ ${populateImageCommands}
34+ )
2635 # Add the closures of the top-level store objects.
2736 storePaths=$(cat ${sdClosureInfo}/store-paths)
2837
2938 # Make a crude approximation of the size of the target image.
3039 # If the script starts failing, increase the fudge factors here.
31− numInodes=$(find $storePaths | wc -l)
32− numDataBlocks=$(du -c -B 4096 --apparent-size $storePaths | awk '$2 == "total" { print int($1 * 1.03) }')
40+ numInodes=$(find $storePaths ./files | wc -l)
41+ numDataBlocks=$(du -s -c -B 4096 --apparent-size $storePaths ./files | tail -1 | awk '{ print int($1 * 1.03) }')
3342 bytes=$((2 * 4096 * $numInodes + 4096 * $numDataBlocks))
3443 echo "Creating an EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks)"
3544
@@ -47,6 +56,12 @@ pkgs.stdenv.mkDerivation {
4756 echo "copying store paths to image..."
4857 cptofs -t ext4 -i $out $storePaths /nix/store/
4958
59+ (
60+ echo "copying files to image..."
61+ cd ./files
62+ cptofs -t ext4 -i $out ./* /
63+ )
64+
5065 # I have ended up with corrupted images sometimes, I suspect that happens when the build machine's disk gets full during the build.
5166 if ! fsck.ext4 -n -f $out; then
5267 echo "--- Fsck failed for EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks) ---"