Commit f868dce

HPCesia <me@hpcesia.com>
2026-06-10 04:53:42
Add disko based host file system config
1 parent a060c7e
modules/disk/btrfs-disko.nix
@@ -0,0 +1,144 @@
+{
+  lib,
+  den,
+  ...
+}: {
+  den.aspects.disk.btrfs-disko = {
+    includes = [
+      den.aspects.disk.disko
+      den.aspects.disk.btrfs
+    ];
+
+    settings = {
+      deviceId = lib.mkOption {
+        type = lib.types.str;
+        default = "";
+        description = ''
+          Disk device id (e.g., "ata-..." or "/dev/disk/by-id/...").
+          If not set, auto-detects a single non-USB disk via facter.
+        '';
+      };
+      swapSize = lib.mkOption {
+        type = lib.types.int;
+        default = 0;
+        description = "Size of swap in MiB, 0 disables swap.";
+      };
+    };
+
+    nixos = {
+      config,
+      host,
+      ...
+    }: let
+      cfg = host.settings.disk.btrfs-disko;
+
+      diskDevice =
+        if cfg.deviceId != ""
+        then
+          if lib.hasPrefix "/dev/" cfg.deviceId
+          then cfg.deviceId
+          else "/dev/disk/by-id/" + cfg.deviceId
+        else let
+          native-disks = builtins.filter (f: f.driver != "usb-storage") config.hardware.facter.report.hardware.disk;
+          disk-labels =
+            map (
+              disk:
+                builtins.head (
+                  builtins.filter (f: builtins.substring 0 16 f == "/dev/disk/by-id/") disk.unix_device_names
+                )
+            )
+            native-disks;
+        in
+          if (builtins.length disk-labels == 1)
+          then (builtins.head disk-labels)
+          else
+            abort (
+              "Multiple disks found. Please set settings.disk.btrfs-disko.deviceId. Found: "
+              + toString disk-labels
+            );
+
+      defaultBtrfsOpts = [
+        "defaults"
+        "compress=zstd:1"
+        "ssd"
+        "discard=async"
+      ];
+    in {
+      disko.devices = {
+        disk = {
+          main = {
+            device = diskDevice;
+            type = "disk";
+            content = {
+              type = "gpt";
+              partitions = {
+                boot = lib.mkIf (!config.hardware.facter.report.uefi.supported) {
+                  label = "GRUB";
+                  size = "1M";
+                  type = "EF02";
+                  priority = 0;
+                };
+                ESP = {
+                  label = "boot";
+                  name = "ESP";
+                  size = "512M";
+                  type = "EF00";
+                  priority = 1;
+                  content = {
+                    type = "filesystem";
+                    format = "vfat";
+                    mountpoint = "/boot";
+                    mountOptions = ["defaults"];
+                  };
+                };
+                nixos = {
+                  size = "100%";
+                  priority = 2;
+                  content = {
+                    type = "btrfs";
+                    subvolumes =
+                      {
+                        "/root" = {
+                          mountpoint = "/";
+                          mountOptions = defaultBtrfsOpts ++ ["noatime"];
+                        };
+                        "/home" = {
+                          mountpoint = "/home";
+                          mountOptions = defaultBtrfsOpts ++ ["noatime"];
+                        };
+                        "/nix" = {
+                          mountpoint = "/nix";
+                          mountOptions = defaultBtrfsOpts ++ ["noatime"];
+                        };
+                        "/persist" = {
+                          mountpoint = "/persist";
+                          mountOptions = defaultBtrfsOpts;
+                        };
+                        "/cache" = {
+                          mountpoint = "/cache";
+                          mountOptions = defaultBtrfsOpts;
+                        };
+                      }
+                      // lib.optionalAttrs (cfg.swapSize > 0) {
+                        "@swap" = {
+                          mountpoint = "/swap";
+                          swap.swapfile.size = "${toString cfg.swapSize}M";
+                        };
+                      };
+                  };
+                };
+              };
+            };
+          };
+        };
+      };
+
+      fileSystems = {
+        "/nix".neededForBoot = true;
+        "/home".neededForBoot = true;
+        "/persist".neededForBoot = true;
+        "/cache".neededForBoot = true;
+      };
+    };
+  };
+}
modules/disk/btrfs.nix
@@ -0,0 +1,12 @@
+{
+  den.aspects.disk.btrfs = {
+    nixos = {
+      boot.supportedFilesystems.btrfs = true;
+
+      services.btrfs.autoScrub = {
+        enable = true;
+        fileSystems = ["/"];
+      };
+    };
+  };
+}
modules/disk/disko.nix
@@ -0,0 +1,12 @@
+{inputs, ...}: {
+  flake-file.inputs.disko = {
+    url = "github:nix-community/disko";
+    inputs.nixpkgs.follows = "nixpkgs";
+  };
+
+  den.aspects.disk.disko = {
+    nixos = {
+      imports = [inputs.disko.nixosModules.disko];
+    };
+  };
+}
modules/hosts/hyacine/file-system.nix
@@ -0,0 +1,11 @@
+{den, ...}: {
+  den.hosts.hyacine.settings = {
+    disk.btrfs-disko = {
+      deviceId = "/dev/vda";
+    };
+  };
+
+  den.aspects.hyacine = {
+    includes = [den.aspects.disk.btrfs-disko];
+  };
+}
modules/hosts/kevin/file-system.nix
@@ -0,0 +1,20 @@
+{den, ...}: {
+  den.hosts.kevin.settings = {
+    disk.btrfs-disko = {
+      deviceId = "/dev/disk/by-id/nvme-YMTC_YMSS2ED08D25MC_YMB51T0RA24460074X";
+      swapSize = 32 * 1024; # 32GiB
+    };
+  };
+
+  den.aspects.kevin = {
+    includes = [den.aspects.disk.btrfs-disko];
+
+    # nixos = {
+    #   disko.devices.disk = {
+    #     storage = {
+    #       device = "/dev/disk/by-id/nvme-Samsung_SSD_990_EVO_Plus_2TB_S7U7NJ0XC02928B";
+    #     };
+    #   };
+    # };
+  };
+}
modules/hosts/mobius/file-system.nix
@@ -0,0 +1,11 @@
+{den, ...}: {
+  den.hosts.mobius.settings = {
+    disk.btrfs-disko = {
+      swapSize = 16 * 1024; # 16GiB
+    };
+  };
+
+  den.aspects.mobius = {
+    includes = [den.aspects.disk.btrfs-disko];
+  };
+}
modules/hosts/tribios/file-system.nix
@@ -0,0 +1,57 @@
+{den, ...}: {
+  den.aspects.tribios = {
+    # Manually manage file system.
+    # Since tribios has edk2-rk3588 firmware in main storage.
+    # Using disko will break this.
+    includes = [
+      # den.aspects.disk.btrfs-disko
+      den.aspects.disk.btrfs
+    ];
+
+    nixos = let
+      defaultBtrfsOpts = [
+        "defaults"
+        "compress=zstd:1"
+        "ssd"
+        "discard=async"
+      ];
+    in {
+      fileSystems = {
+        "/boot" = {
+          device = "/dev/disk/by-uuid/A0F7-6D8C";
+          fsType = "vfat";
+          options = ["fmask=0022" "dmask=0022"];
+        };
+        "/" = {
+          device = "/dev/disk/by-uuid/9886235f-5a74-41e8-919b-9e4096a7cb9e";
+          fsType = "btrfs";
+          options = ["subvol=/root"] ++ defaultBtrfsOpts;
+        };
+        "/nix" = {
+          device = "/dev/disk/by-uuid/9886235f-5a74-41e8-919b-9e4096a7cb9e";
+          fsType = "btrfs";
+          options = ["subvol=/nix" "noatime"] ++ defaultBtrfsOpts;
+          neededForBoot = true;
+        };
+        "/home" = {
+          device = "/dev/disk/by-uuid/9886235f-5a74-41e8-919b-9e4096a7cb9e";
+          fsType = "btrfs";
+          options = ["subvol=/home"] ++ defaultBtrfsOpts;
+          neededForBoot = true;
+        };
+        "/persist" = {
+          device = "/dev/disk/by-uuid/9886235f-5a74-41e8-919b-9e4096a7cb9e";
+          fsType = "btrfs";
+          options = ["subvol=/persist"] ++ defaultBtrfsOpts;
+          neededForBoot = true;
+        };
+        "/cache" = {
+          device = "/dev/disk/by-uuid/9886235f-5a74-41e8-919b-9e4096a7cb9e";
+          fsType = "btrfs";
+          options = ["subvol=/cache"] ++ defaultBtrfsOpts;
+          neededForBoot = true;
+        };
+      };
+    };
+  };
+}
flake.lock
@@ -35,6 +35,26 @@
         "type": "github"
       }
     },
+    "disko": {
+      "inputs": {
+        "nixpkgs": [
+          "nixpkgs"
+        ]
+      },
+      "locked": {
+        "lastModified": 1781152676,
+        "narHash": "sha256-RxWs5ND31KzTG7wvMM+PMfUjyNpmIEr999lqNARaM5o=",
+        "owner": "nix-community",
+        "repo": "disko",
+        "rev": "ff8702b4de27f72b4c78573dfb89ec74e36abdf1",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-community",
+        "repo": "disko",
+        "type": "github"
+      }
+    },
     "flake-compat": {
       "locked": {
         "lastModified": 1777699697,
@@ -138,6 +158,7 @@
       "inputs": {
         "den": "den",
         "devshell": "devshell",
+        "disko": "disko",
         "flake-compat": "flake-compat",
         "flake-file": "flake-file",
         "flake-parts": "flake-parts",
flake.nix
@@ -11,6 +11,10 @@
       url = "github:numtide/devshell";
       inputs.nixpkgs.follows = "nixpkgs";
     };
+    disko = {
+      url = "github:nix-community/disko";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
     flake-compat.url = "https://git.lix.systems/lix-project/flake-compat/archive/main.tar.gz";
     flake-file.url = "github:denful/flake-file";
     flake-parts = {