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