How to get UUIDs of partitions after running disko into configuration.nix?

Im trying to automate the process of installing NixOS and for the that Im currently looking into setting up my disk-partitions with disko. My configuration looks something like this:

# disk-config.nix
{
  disko.devices = {
    disk = {
      main = {
        type = "disk";
        device = "/dev/vda";
        content = {
          type = "gpt";
          partitions = {
            ESP = {
              size = "500M";
              type = "EF00";
              content = {
                type = "filesystem";
                format = "vfat";
                mountpoint = "/boot";
                mountOptions = [
                  "defaults"
                ];
              };
            };
            luks = {
              size = "100%";
              content = {
                type = "luks";
                name = "crypted";
                extraOpenArgs = [ ];
                settings = {
                  keyFile = "/tmp/secret.key";
                  allowDiscards = true;
                };
                content = {
                  type = "lvm_pv";
                  vg = "pool";
                };
              };
            };
          };
        };
      };
    };
    lvm_vg = {
      pool = {
        type = "lvm_vg";
        lvs = {
          encryptedSwap = {
            size = "32G";
            content = {
              type = "swap";
              randomEncryption = true;
              priority = 100;
            };
          };
          root = {
            size = "100%FREE";
            content = {
              type = "filesystem";
              format = "ext4";
              mountpoint = "/";
              mountOptions = [
                "defaults"
              ];
            };
          };
        };
      };
    };
  };
}

Running this with

sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko -- --mode disko ./disk-config.nix

creates the partitioning exactly as I would expect it.

What I currently struggling with is how to do some of the post processing, i.e. after running the disko command I need to run swapon /dev/pool/encryptedSwap. I also need to know the uuid of the partitions so that I can setup something like this

boot.initrd.luks.devices.luksroot = {
  device = "/dev/disk/by-uuid/<UUID>";
  preLVM = true;
  allowDiscards = true;
};

Any ideas on how to do this? Does nix offer some help with this?

Just import the disko NixOS module and then your disk-config.nix, e.g.: dotfiles/nixos-config/hosts/rin/default.nix at e2432f2928ed2462852416dd54068f8c0c45dc6d · TLATER/dotfiles · GitHub

If you want to use a hardware-configuration.nix, make sure to generate it without the filesystems.

1 Like