ubivolume.nix (2365B)
1 { 2 config 3 , pkgs 4 , lib 5 , ... 6 }: 7 let 8 inherit (pkgs) liminix; 9 inherit (lib) mkIf mkOption types concatStringsSep optionalString; 10 in 11 { 12 imports = [ 13 ./initramfs.nix 14 ./ubifs.nix 15 ]; 16 17 options.hardware.ubi = { 18 minIOSize = mkOption { type = types.str; }; 19 eraseBlockSize = mkOption { type = types.str; }; # LEB 20 maxLEBcount = mkOption { type = types.str; }; # LEB 21 }; 22 23 config = mkIf (config.rootfsType == "ubifs") { 24 kernel.config = { 25 MTD_UBI="y"; 26 27 UBIFS_FS = "y"; 28 UBIFS_FS_SECURITY = "n"; 29 }; 30 boot.initramfs.enable = true; 31 32 system.outputs.rootfs = 33 let 34 inherit (pkgs.pkgsBuildBuild) runCommand; 35 ubiVolume = ({ name, volumeId, image, flags ? [] }: 36 '' 37 [${name}] 38 mode=ubi 39 vol_id=${toString volumeId} 40 vol_type=dynamic 41 vol_name=${name} 42 vol_alignment=1 43 ${optionalString (image != null) '' 44 image=${image} 45 ''} 46 ${optionalString (image == null) '' 47 vol_size=1MiB 48 ''} 49 ${optionalString (flags != []) '' 50 vol_flags=${concatStringsSep "," flags} 51 ''} 52 ''); 53 54 ubiImage = (volumes: 55 let 56 ubinizeConfig = pkgs.writeText "ubinize.conf" (concatStringsSep "\n" volumes); 57 inherit (pkgs.pkgsBuildBuild) mtdutils; 58 in 59 runCommand "ubinize" { 60 depsBuildBuild = [ mtdutils ]; 61 # block size := 128kb 62 # page size := 2048 63 # ubninize opts := -E 5 64 } '' 65 ubinize -Q "$SOURCE_DATE_EPOCH" -o $out \ 66 -p ${config.hardware.ubi.physicalEraseBlockSize} -m ${config.hardware.ubi.minIOSize} \ 67 -e ${config.hardware.ubi.logicalEraseBlockSize} \ 68 ${ubinizeConfig} 69 ''); 70 71 ubiDisk = ({ initramfs }: 72 let 73 initramfsUbi = ubiVolume { 74 name = "rootfs"; 75 volumeId = 0; 76 image = initramfs; 77 flags = [ "autoresize" ]; 78 }; 79 in 80 ubiImage [ 81 initramfsUbi 82 ]); 83 84 disk = ubiDisk { 85 initramfs = config.system.outputs.rootubifs; # liminix.builders.squashfs config.filesystem.contents; # # assert this is a proper FIT. 86 }; 87 88 in 89 disk; 90 }; 91 }