liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ubivolume.nix (2562B)


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