Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Disko (Disk Partitioning)

Platforms: NixOS only.

Declarative disk configuration with LUKS encryption and BTRFS subvolumes.

Importing

The layout is the separate export nixosModules.disko, which nixosModules.default does not import. It is the only othrys module that writes into the disko option namespace, so a host that wants it imports the export together with the disko module, and a host that partitions another way imports neither:

# flake inputs
disko.follows = "othrys/disko";

# host modules
inputs.othrys.nixosModules.disko
inputs.disko.nixosModules.disko

The export without the disko module fails at evaluation, since a write into an undeclared option namespace fails whatever guards it.

Layout

  • EFI boot partition. Unencrypted FAT32
  • Swap partition. LUKS encrypted, or encrypted with a random key on each boot when swap.randomEncryption is on, which is the default with luks.fido2.enable
  • Root partition. LUKS encrypted BTRFS with subvolumes:
    • root → / (ephemeral, wiped on boot)
    • persist → /persist (all persistent state)
    • nix → /nix (Nix store)

With luks.fido2.enable, the root’s crypttab entry carries fido2-device=auto, so the initrd tries a FIDO2 token before it asks for the passphrase. Stage 1 runs systemd on every host, so the option turns nothing else on. The keyslot itself is enrolled out of band, as Secrets describes.

Options

  options.othrys.system.disko = {
    enable = lib.mkEnableOption "Disko declarative disk configuration";

    device = lib.mkOption {
      type = lib.types.str;
      description = ''
        Disk device path. Use /dev/disk/by-id/ for reliable identification.
        Find your disk ID with: ls -l /dev/disk/by-id/

        A path outside /dev/disk/, such as /dev/sda, evaluates with a
        warning. The kernel hands those names out in probe order, so the same
        name can be another disk on the next boot.
      '';
      example = "/dev/disk/by-id/nvme-VENDOR_MODEL_XXXXXXXXXXXXX";
    };

    bootSize = lib.mkOption {
      type = lib.types.str;
      default = "1G";
      description = "EFI boot partition size.";
    };

    swapSize = lib.mkOption {
      type = lib.types.str;
      default = "8G";
      description = ''
        Swap partition size. Recommendations:
        - For hibernation: swap >= RAM size
        - Without hibernation: 4-8GB is usually sufficient
      '';
      example = "16G";
    };

    # Advanced options with sensible defaults
    luks = {
      name = lib.mkOption {
        type = lib.types.str;
        default = "cryptroot";
        description = "Name for the LUKS encrypted root device (appears as /dev/mapper/<name>).";
      };

      swapName = lib.mkOption {
        type = lib.types.str;
        default = "cryptswap";
        description = "Name for the LUKS encrypted swap device (used only when swap.randomEncryption is false).";
      };

      passwordFile = lib.mkOption {
        type = lib.types.nullOr othrysTypes.secretPath;
        default = null;
        description = ''
          Path to a file containing the LUKS passphrase used when disko first
          formats the disk. Leave null (default) to be prompted interactively at
          format time, recommended since at install time sops secrets do not
          yet exist on the target.

          With luks.fido2.enable, this passphrase becomes the break-glass
          recovery keyslot: day-to-day unlock is via YubiKey, and the passphrase
          is only needed if every enrolled key is unavailable. Boot always falls
          back to this passphrase if no token is present.
        '';
        example = lib.literalExpression ''config.sops.secrets."security/boot/password".path'';
      };

      allowDiscards = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = ''
          Pass TRIM requests through dm-crypt to the SSD. The requests show an
          observer of the raw disk which blocks are free, which is the price
          of keeping the drive's performance. `services.fstrim.enable`
          follows this option, since the passthrough alone discards nothing.
        '';
      };

      bypassWorkqueues = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Bypass dm-crypt workqueues for better SSD performance.";
      };

      fido2.enable = lib.mkEnableOption "FIDO2/YubiKey unlock for the encrypted root (enrolled per-key via `just luks-enroll`; the passphrase stays as break-glass recovery)";
    };

    btrfs = {
      compression = lib.mkOption {
        type = lib.types.str;
        default = "zstd";
        description = "BTRFS compression algorithm for root subvolume.";
      };

      mountOptions = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = ["noatime" "space_cache=v2"];
        description = "Common BTRFS mount options applied to all subvolumes.";
      };
    };

    swap.randomEncryption = lib.mkOption {
      type = lib.types.bool;
      default = cfg.luks.fido2.enable;
      defaultText = lib.literalExpression "config.othrys.system.disko.luks.fido2.enable";
      description = ''
        Encrypt swap with an ephemeral key generated on each boot instead of a
        persistent LUKS passphrase. Avoids a second unlock prompt at boot
        (recommended with FIDO2) at the cost of hibernation/resume. Defaults to
        luks.fido2.enable.
      '';
    };
  };

Partition Layout

            partitions = {
              # EFI System Partition
              boot = {
                size = cfg.bootSize;
                type = "EF00";
                priority = 1;
                content = {
                  type = "filesystem";
                  format = "vfat";
                  mountpoint = "/boot";
                  mountOptions = ["fmask=0077" "dmask=0077" "defaults"];
                };
              };

              # Swap partition (random-key ephemeral, or LUKS with the recovery passphrase)
              swap = {
                size = cfg.swapSize;
                priority = 2;
                content =
                  if cfg.swap.randomEncryption
                  then {
                    # Re-encrypted with a fresh random key each boot, so no second
                    # unlock prompt, but no hibernation/resume.
                    type = "swap";
                    randomEncryption = true;
                  }
                  else {
                    type = "luks";
                    name = cfg.luks.swapName;
                    inherit (cfg.luks) passwordFile;
                    extraFormatArgs = luksFormatArgs;
                    settings = {
                      inherit (cfg.luks) allowDiscards bypassWorkqueues;
                    };
                    content = {
                      type = "swap";
                      # Without a discard policy swap never trims, whatever
                      # the LUKS layer allows, so it follows allowDiscards.
                      discardPolicy = lib.mkIf cfg.luks.allowDiscards "both";
                    };
                  };
              };

              # LUKS Encrypted BTRFS Root
              root = {
                size = "100%";
                priority = 3;
                content = {
                  type = "luks";
                  inherit (cfg.luks) name passwordFile;
                  extraFormatArgs = luksFormatArgs;
                  settings = {
                    inherit (cfg.luks) allowDiscards bypassWorkqueues;
                  };
                  content = {
                    type = "btrfs";
                    extraArgs = ["-f"];

                    subvolumes = {
                      # Ephemeral root, wiped on every boot
                      "root" = {
                        mountpoint = "/";
                        mountOptions =
                          ["subvol=root" "compress=${cfg.btrfs.compression}"]
                          ++ cfg.btrfs.mountOptions;
                      };

                      # Persistent state, surviving reboots and mounting wherever
                      # the impermanence surface declares its root.
                      "persist" = {
                        mountpoint = config.othrys.system.impermanence.persistRoot;
                        mountOptions = ["subvol=persist"] ++ cfg.btrfs.mountOptions;
                      };

                      # Nix store, persistent
                      "nix" = {
                        mountpoint = "/nix";
                        mountOptions = ["subvol=nix"] ++ cfg.btrfs.mountOptions;
                      };
                    };
                  };
                };
              };
            };

BTRFS Subvolumes

                    subvolumes = {
                      # Ephemeral root, wiped on every boot
                      "root" = {
                        mountpoint = "/";
                        mountOptions =
                          ["subvol=root" "compress=${cfg.btrfs.compression}"]
                          ++ cfg.btrfs.mountOptions;
                      };

                      # Persistent state, surviving reboots and mounting wherever
                      # the impermanence surface declares its root.
                      "persist" = {
                        mountpoint = config.othrys.system.impermanence.persistRoot;
                        mountOptions = ["subvol=persist"] ++ cfg.btrfs.mountOptions;
                      };

                      # Nix store, persistent
                      "nix" = {
                        mountpoint = "/nix";
                        mountOptions = ["subvol=nix"] ++ cfg.btrfs.mountOptions;
                      };
                    };

Usage

Configure in host config:

othrys.system.disko = {
  enable = true;
  device = "/dev/disk/by-id/...";
  swapSize = "8G";
};

luks.passwordFile stays at its default of null for an install, and disko then prompts for the LUKS passphrase when it formats the disk. A sops secret does not exist on the target at that point, so a secrets path there names a file the installer cannot read. The option is for a host where the file is already present at format time.

device is meant to be a path under /dev/disk/by-id/. A name such as /dev/sda evaluates with a warning, since the kernel assigns those in probe order and disko formats whatever disk carries the name.

Both LUKS volumes are formatted with stated parameters, which are LUKS2, aes-xts-plain64 with a 512-bit key, SHA-256 and Argon2id. They match the current cryptsetup defaults and apply at format time only.

With the layout on, othrys.system.impermanence.device defaults to the LUKS mapping of the root, /dev/mapper/<luks.name>, which is the device the boot wipe mounts. See Impermanence.

luks.allowDiscards passes TRIM through the encryption layer, and services.fstrim.enable follows it at mkDefault, so the weekly timer is what issues the discards.

The initial install runs disko once in destroy,format,mount mode against the host, which erases the target disk. This repository ships no recipe for that step, since it holds no hosts. The Installation guide walks through it with the consuming fleet’s justfile.