Skip to content

Persistently encrypted swap seems to be supported #1134

@tie-ling

Description

@tie-ling

I use systemctl sleep, which invokes suspend-then-hibernate by default. This is useful in preventing data loss, when laptop battery reaches 5% while suspended-to-RAM.

In master branch, inside lib/types/swap.nix, it remarks, " TODO: we don't support encrypted swap yet". It seems that, at least NixOS configuration generation is well supported. I tested the following config, and it Just Works with systemctl hibernate. Everything is resumed upon next boot.

{
  # see https://github.com/nix-community/disko/tree/master/example
  disko.devices = {
    disk = {
      main = {
        type = "disk";
        content = {
          type = "gpt";
          partitions = {
            # gpt-bios-compat
            boot = {
              size = "1M";
              type = "EF02"; # for grub MBR
            };
            ESP = {
              size = "1024M";
              type = "EF00";
              content = {
                type = "filesystem";
                format = "vfat";
                mountpoint = "/boot";
                mountOptions = [ "umask=0077" ];
              };
            };
            encryptedSwap = {
              # for hibernation (suspend to disk)
              # should be no less than RAM size
              size = "8G";
              content = {
                type = "luks";
                name = "cryptedSwap";
                settings.allowDiscards = true;
                # do not use keyFile
                # as keyFile will need to be within initrd in /boot
                # which is unencrypted
                # systemd initrd will reuse password for /,
                # if passwd for swap and / are the same
                passwordFile = "/tmp/secret.key";
                content = {
                  type = "swap";
                  resumeDevice = true;
                  randomEncryption = false;
                };
              };
            };
            luks = {
              size = "100%";
              content = {
                type = "luks";
                name = "crypted";
                settings.allowDiscards = true;
                passwordFile = "/tmp/secret.key";
                content = {
                  type = "filesystem";
                  format = "xfs";
                  mountpoint = "/";
                };
              };
            };
          };
        };
      };
    };
  };
}

It remains to be seen, whether automated partitioning during NixOS installation works. I do not have an opportunity to test this at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions