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