# Declarative disk layout, generated into `disko.devices` from `my.disks`. # # system disk (NVMe): ESP + btrfs "nixos" with @root @home @nix @log @swap # data disk (SATA): btrfs "data", one subvolume per home folder, each # mounted inside the user's home (~/Games, ~/Music, ...) # # `disko-install` / the install script partitions the disks from this; on a # running system the same data produces the matching `fileSystems` entries. { config, lib, ... }: let cfg = config.my.disks; inherit (lib) mkOption types; user = config.my.user.name; home = "/home/${user}"; folders = cfg.data.homeFolders; hasFolders = cfg.data.enable && folders != { }; # Compression keeps the NVMe small-write friendly; noatime avoids metadata writes. btrfsOptions = [ "compress=zstd:1" "noatime" ]; placeholderDevices = lib.filter (d: lib.hasInfix "CHANGE-ME" d) ( [ cfg.system.device ] ++ lib.optional cfg.data.enable cfg.data.device ); in { options.my.disks = { enable = lib.mkEnableOption "the disko-managed disk layout"; system = { device = mkOption { type = types.str; example = "/dev/disk/by-id/nvme-Samsung_SSD_990_PRO_1TB_S6XXXXXXXXXXXX"; description = "Whole disk for the ESP and the btrfs root. Use a stable /dev/disk/by-id path."; }; espSize = mkOption { type = types.str; default = "2G"; description = "Size of the EFI system partition (mounted on /boot)."; }; swapSize = mkOption { type = types.str; default = "16G"; description = "Size of the swapfile in the @swap subvolume."; }; }; data = { enable = lib.mkEnableOption "the second (data) disk"; device = mkOption { type = types.str; example = "/dev/disk/by-id/ata-Samsung_SSD_870_EVO_2TB_S6XXXXXXXXXXXX"; description = "Whole disk holding the home folder subvolumes."; }; mountOptions = mkOption { type = types.listOf types.str; default = [ "compress=zstd:1" "noatime" # The machine still boots (to the system disk) if the SSD is missing. "nofail" "x-systemd.device-timeout=10s" ]; description = "Mount options of every data subvolume."; }; homeFolders = mkOption { type = types.attrsOf ( types.submodule ( { name, ... }: { options.subvolume = mkOption { type = types.str; default = "@" + lib.toLower name; defaultText = lib.literalExpression ''"@" + lib.toLower name''; description = "Btrfs subvolume backing this folder."; }; } ) ); default = { }; example = lib.literalExpression ''{ Games = { }; Music.subvolume = "@tunes"; }''; description = '' Folders of the home directory that live on the data disk. The attribute name is the folder name and is mounted at /home//. ''; }; }; }; config = lib.mkIf cfg.enable ( lib.mkMerge [ { disko.devices.disk.system = { type = "disk"; inherit (cfg.system) device; content = { type = "gpt"; partitions = { ESP = { size = cfg.system.espSize; type = "EF00"; content = { type = "filesystem"; format = "vfat"; mountpoint = "/boot"; # Only root may read the ESP (random seed, lanzaboote images). mountOptions = [ "umask=0077" ]; }; }; root = { size = "100%"; content = { type = "btrfs"; extraArgs = [ "-f" "-L" "nixos" ]; subvolumes = { "@root" = { mountpoint = "/"; mountOptions = btrfsOptions; }; "@home" = { mountpoint = "/home"; mountOptions = btrfsOptions; }; "@nix" = { mountpoint = "/nix"; mountOptions = btrfsOptions; }; "@log" = { mountpoint = "/var/log"; mountOptions = btrfsOptions; }; # No compression on the swapfile's subvolume. "@swap" = { mountpoint = "/swap"; mountOptions = [ "noatime" ]; swap.swapfile.size = cfg.system.swapSize; }; }; }; }; }; }; }; # Logs must be mounted before journald starts in stage 2. fileSystems."/var/log".neededForBoot = true; services.btrfs.autoScrub = { enable = true; interval = "monthly"; fileSystems = [ "/" ] ++ lib.optional hasFolders "${home}/${lib.head (lib.attrNames folders)}"; }; warnings = lib.optional (placeholderDevices != [ ]) '' my.disks still has placeholder device paths (${lib.concatStringsSep ", " placeholderDevices}). Run `nix run .#install`, or edit hosts/desktop/disk-ids.json, before installing. ''; } (lib.mkIf cfg.data.enable { disko.devices.disk.data = { type = "disk"; inherit (cfg.data) device; content = { type = "gpt"; partitions.data = { size = "100%"; content = { type = "btrfs"; extraArgs = [ "-f" "-L" "data" ]; subvolumes = lib.mapAttrs' ( name: folder: lib.nameValuePair folder.subvolume { mountpoint = "${home}/${name}"; inherit (cfg.data) mountOptions; } ) folders; }; }; }; }; # Subvolume roots are created root-owned; hand them to the user. The # home directory itself is listed first so the mount points have a parent. systemd.tmpfiles.rules = [ "d ${home} 0700 ${user} users -" ] ++ lib.mapAttrsToList (name: _: "d ${home}/${name} 0755 ${user} users -") folders; }) ] ); }