nix-community / disko

Declarative disk partitioning and formatting using nix [maintainers=@Lassulus @Enzime @iFreilicht]
MIT License
1.84k stars 196 forks source link

Specify a size range for partitions #600

Open cfitz25 opened 7 months ago

cfitz25 commented 7 months ago

Would it be possible to set a drive size with a minimum and a maximum value? This would make it easier to use the same disko layout on different drive/partition sizes. eg, for separate /nix partition of 100GB on a 1TB+ drive but 10GB on a smaller VM drive

Lassulus commented 7 months ago

hmm, not really. I guess you would need to get the disk data into nix first and then do some detection on it. Another possibility would be to generate multiple different disko configs and execute a different one depending on a script you write yourself. Something like:

environment.systemPackages = [
  (pkgs.writeScriptBin "multidisko" ''
    device_to_format=$1
    if [ $(fdisk -x "$device_to_format" | grep '[0-9]* bytes, | sed 's/ bytes,//') -lt 1000000 ]; then
      ${nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          { disko.devices = {
             disk = {
               device = "$device_to_format"; #not sure if this works, it should in theory
               content = {
                 type = "filesystem";
                 format = "ext4";
                 mountpoint = "/";
               };
             };
          };
        ];
      }.config.system.build.diskoScript}
     else
      ${nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          { disko.devices = {
             disk = {
               device = "$device_to_format"; #not sure if this works, it should in theory
               content = {
                 type = "filesystem";
                 format = "xfs";
                 mountpoint = "/";
               };
             };
          };
        ];
      }.config.system.build.diskoScript}
    fi

  '')
];
iFreilicht commented 1 month ago

This would become feasible with #789