swapDevices nixos module vs disko swap module: what's the difference, and which to use?

{
  disko.devices = {
    disk = {
      main = {
        type = "disk";
        device = "/dev/nvme0n1";
        content = {
          type = "gpt";
          partitions = {
            ESP = {
              size = "512M";
              type = "EF00";
              content = {
                type = "filesystem";
                format = "vfat";
                mountpoint = "/boot";
                mountOptions = [ "umask=0077" ];
              };
            };
            luks = {
              size = "100%";
              content = {
                type = "luks";
                name = "crypted";
                # disable settings.keyFile if you want to use interactive password entry
                #passwordFile = "/tmp/secret.key"; # Interactive
                settings = {
                  allowDiscards = true;
                  #keyFile = "/tmp/secret.key";
                };
                #additionalKeyFiles = [ "/tmp/additionalSecret.key" ];
                content = {
                  type = "btrfs";
                  extraArgs = [
                    "-L"
                    "nixos"
                    "-f"
                  ];
                  subvolumes =
                    let
                      mountOpts = [
                        "compress=zstd"
                        "ssd"
                        "space_cache=v2"
                        "noatime"
                        "discard=async"
                      ];
                    in
                    {
                      "/root" = {
                        mountpoint = "/";
                        mountOptions = mountOpts;
                      };
                      "/home" = {
                        mountpoint = "/home";
                        mountOptions = mountOpts;
                      };
                      "/nix" = {
                        mountpoint = "/nix";
                        mountOptions = mountOpts;
                      };
                      "/swap" = {
                        mountpoint = "/swap";
                        mountOptions = [
                          "noatime"
                          "nodatacow"
                          "compress=no"
                        ];
                        swap.swapfile.size = "18G";
                      };
                    };
                };
              };
            };
          };
        };
      };
    };
  };
}

So, this is my disko config. However, I was wondering if I should use swapDevices instead. I want to use zswap with discard option. I don’t know how to configure it, or whether I should I configure it using swapDevices or disko.

Disko just generates a script to set your disk layout. That has nothing to do with your NixOS config.

Doesn’t disko also take care of your NixOS filesystem configuration? Otherwise it wouldn’t make sense to run nixos-generate-config --no-filesystems --root /mnt: disko/docs/quickstart.md at 6f4cf5abbe318e4cd1e879506f6eeafd83f7b998 · nix-community/disko · GitHub

1 Like

Disko doesn’t include that, you have to run that yourself.
They just reiterated normal NixOS install instructions.

But for a normal installation you don’t use
the --no-filesystems flag.

From the disko QuickStart guide

Include the no-filesystems switch when using the nixos-generate-config
command to generate an initial configuration.nix. You will be supplying the
file system configuration details from disk-config.nix.

Okay after reading the code it looks like disko is using swapDevices anyway:

So there’s no difference :person_shrugging:

2 Likes

I think you mean zramswap? zswap is also a thing but slightly different. In case it’s the former, there’s the top-level zramSwap option in NixOS. I use that, and disko for configuring physical disks including swap partitions.

Yea, the idea is that you specify your disk layout once in the disko config, and disko can take care of everything from formatting to mounting to setting the necessary normal NixOS options. So once everything is formatted, it’s basically just an adapter from disko options to the typical NixOS fileSystems / swapDevices / LUKS / ZFS / etc. options. Handy so you only have to specify stuff in one place.

2 Likes

I did read the code. But I was confused, if using a swapfile, how do I set the discard option for that particular partition? And for the randomEncryption option, where do I set the config.randomEncryption?

Edit: I think that that module is for a separate swap partition, not a btrfs swapfile on a swap btrfs subvol…?

Yes, exactly. Disko is about partitioning your disk (hence the name). It won’t create files for you.

If you want to use swap files, use the NixOS option. I would advise against using swapfiles, though, especially if you’re using btrfs:

See this summary in the thread in question on how to set up a swap file, too, but again, don’t actually do this. I’m pretty sure it will mess up your filesystem by default on btrfs.

For swap partitions configured with disko, like this.

If you want to change the encryption options, looks like for the time being you’ll have to manually set them in your NixOS configuration instead of through disko. Thanks to the NixOS module system multiple definitions of the same device will be merged constructively, so you can just redefine it and only set the encryption options.

1 Like