1 # Builds an ext4 image containing a populated /nix/store with the closure
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.
8 # List of derivations to be included
10 # Whether or not to compress the resulting image with zstd
11 , compressImage ? false, zstd
12 # Shell commands to populate the ./files directory.
13 # All files in that directory are copied to the root of the FS.
14 , populateImageCommands ? ""
16 , uuid ? "44444444-4444-4444-8888-888888888888"
24 sdClosureInfo = pkgs.buildPackages.closureInfo { rootPaths = storePaths; };
26 pkgs.stdenv.mkDerivation {
27 name = "ext4-fs.img${lib.optionalString compressImage ".zst"}";
29 nativeBuildInputs = [ e2fsprogs.bin libfaketime perl fakeroot ]
30 ++ lib.optional compressImage zstd;
34 ${if compressImage then "img=temp.img" else "img=$out"}
37 ${populateImageCommands}
40 echo "Preparing store paths for image..."
42 # Create nix/store before copying path
43 mkdir -p ./rootImage/nix/store
45 xargs -I % cp -a --reflink=auto % -t ./rootImage/nix/store/ < ${sdClosureInfo}/store-paths
50 for f in ./files/*; do
51 cp -a --reflink=auto -t ./rootImage/ "$f"
55 # Also include a manifest of the closures in a format suitable for nix-store --load-db
56 cp ${sdClosureInfo}/registration ./rootImage/nix-path-registration
58 # Make a crude approximation of the size of the target image.
59 # If the script starts failing, increase the fudge factors here.
60 numInodes=$(find ./rootImage | wc -l)
61 numDataBlocks=$(du -s -c -B 4096 --apparent-size ./rootImage | tail -1 | awk '{ print int($1 * 1.20) }')
62 bytes=$((2 * 4096 * $numInodes + 4096 * $numDataBlocks))
63 echo "Creating an EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks)"
65 mebibyte=$(( 1024 * 1024 ))
66 # Round up to the nearest mebibyte.
67 # This ensures whole 512 bytes sector sizes in the disk image
68 # and helps towards aligning partitions optimally.
69 if (( bytes % mebibyte )); then
70 bytes=$(( ( bytes / mebibyte + 1) * mebibyte ))
73 truncate -s $bytes $img
75 faketime -f "1970-01-01 00:00:01" fakeroot mkfs.ext4 -L ${volumeLabel} -U ${uuid} -d ./rootImage $img
77 export EXT2FS_NO_MTAB_OK=yes
78 # I have ended up with corrupted images sometimes, I suspect that happens when the build machine's disk gets full during the build.
79 if ! fsck.ext4 -n -f $img; then
80 echo "--- Fsck failed for EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks) ---"
85 # We may want to shrink the file system and resize the image to
86 # get rid of the unnecessary slack here--but see
87 # https://github.com/NixOS/nixpkgs/issues/125121 for caveats.
92 # Add 16 MebiByte to the current_size
93 new_size=$(dumpe2fs -h $img | awk -F: \
94 '/Block count/{count=$2} /Block size/{size=$2} END{print (count*size+16*2**20)/size}')
96 resize2fs $img $new_size
98 if [ ${builtins.toString compressImage} ]; then
99 echo "Compressing image"
100 zstd -v --no-progress ./$img -o $out