1 # Builds an btrfs 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"
23 sdClosureInfo = pkgs.buildPackages.closureInfo { rootPaths = storePaths; };
25 pkgs.stdenv.mkDerivation {
26 name = "btrfs-fs.img${lib.optionalString compressImage ".zst"}";
28 nativeBuildInputs = [ btrfs-progs libfaketime fakeroot ] ++ lib.optional compressImage zstd;
32 ${if compressImage then "img=temp.img" else "img=$out"}
37 ${populateImageCommands}
40 mkdir -p ./rootImage/nix/store
42 xargs -I % cp -a --reflink=auto % -t ./rootImage/nix/store/ < ${sdClosureInfo}/store-paths
47 for f in ./files/*; do
48 cp -a --reflink=auto -t ./rootImage/ "$f"
52 cp ${sdClosureInfo}/registration ./rootImage/nix-path-registration
55 faketime -f "1970-01-01 00:00:01" fakeroot mkfs.btrfs -L ${volumeLabel} -U ${uuid} -r ./rootImage --shrink $img
57 if ! btrfs check $img; then
58 echo "--- 'btrfs check' failed for BTRFS image ---"
62 if [ ${builtins.toString compressImage} ]; then
63 echo "Compressing image"
64 zstd -v --no-progress ./$img -o $out