Skip to main content

NixOS: Partitioning Drives with Disko

NixOS - This article is part of a series.
Part 4: This Article

Importing the Disko module
#

Before installing NixOS, it is important that all drives are correctly formatted and mounted. This can be done declaratively by importing the Disko module flake.

Defining disk partitions
#

Disk partitions can be defined using the disko.devices configuration option. An example of a BTRFS boot drive declaration including LUKS encryption and dedicated persistence sub-partition looks like:

{
  inputs,
  ...
}:
{
  flake.modules.nixos.xps15-disk =
    {
      config,
      lib,
      pkgs,
      ...
    }:
    {
      disko.devices = {
        disk = {
          main = {
            type = "disk";
            device = "/dev/nvme0n1";
            content = {
              type = "gpt";
              partitions = {
                boot = {
                  size = "1M";
                  type = "EF02"; # for grub MBR
                };
                ESP = {
                  size = "512M";
                  type = "EF00";
                  content = {
                    type = "filesystem";
                    format = "vfat";
                    mountpoint = "/boot";
                  };
                };
                luks = {
                  size = "100%";
                  content = {
                    type = "luks";
                    name = "crypted";
                    content = {
                      type = "btrfs";
                      extraArgs = [ "-f" ];
                      subvolumes = {
                        "/root" = {
                          mountpoint = "/";
                        };

                        "/persist" = {
                          mountpoint = "/persist";
                          mountOptions = [ "subvol=persist" ];
                        };

                        "/nix" = {
                          mountpoint = "/nix";
                          mountOptions = [ "subvol=nix" ];
                        };

                        "/swap" = {
                          mountpoint = "/.swapvol";
                          swap.swapfile.size = "16384M";
                        };
                      };
                    };
                  };
                };
              };
            };
          };
        };
      };
    };
}

Partitioning disks
#

Prior to installing NixOS, we can partition disks like:

sudo nix run --experimental-features "nix-command flakes" github:nix-community/disko/latest -- --mode destroy,format,mount --flake github:robbiejennings/nix-config#<system>
NixOS - This article is part of a series.
Part 4: This Article