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