at 24.11-pre 2.0 kB view raw
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. 6{ pkgs 7, lib 8# List of derivations to be included 9, storePaths 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 ? "" 15, volumeLabel 16, uuid ? "44444444-4444-4444-8888-888888888888" 17, btrfs-progs 18, libfaketime 19, fakeroot 20}: 21 22let 23 sdClosureInfo = pkgs.buildPackages.closureInfo { rootPaths = storePaths; }; 24in 25pkgs.stdenv.mkDerivation { 26 name = "btrfs-fs.img${lib.optionalString compressImage ".zst"}"; 27 28 nativeBuildInputs = [ btrfs-progs libfaketime fakeroot ] ++ lib.optional compressImage zstd; 29 30 buildCommand = 31 '' 32 ${if compressImage then "img=temp.img" else "img=$out"} 33 34 set -x 35 ( 36 mkdir -p ./files 37 ${populateImageCommands} 38 ) 39 40 mkdir -p ./rootImage/nix/store 41 42 xargs -I % cp -a --reflink=auto % -t ./rootImage/nix/store/ < ${sdClosureInfo}/store-paths 43 ( 44 GLOBIGNORE=".:.." 45 shopt -u dotglob 46 47 for f in ./files/*; do 48 cp -a --reflink=auto -t ./rootImage/ "$f" 49 done 50 ) 51 52 cp ${sdClosureInfo}/registration ./rootImage/nix-path-registration 53 54 touch $img 55 faketime -f "1970-01-01 00:00:01" fakeroot mkfs.btrfs -L ${volumeLabel} -U ${uuid} -r ./rootImage --shrink $img 56 57 if ! btrfs check $img; then 58 echo "--- 'btrfs check' failed for BTRFS image ---" 59 return 1 60 fi 61 62 if [ ${builtins.toString compressImage} ]; then 63 echo "Compressing image" 64 zstd -v --no-progress ./$img -o $out 65 fi 66 ''; 67}