Hibernate not working with swapfile

Note that you also need to use the systemd initrd for this to work, so to be clear for future readers, the summary is:

{
 swapDevices = [{
    device = "/swap";  # The OP used `/home/swapfile`, which is inadvisable
    size = 30*1024;  # MB
    options = ["discard"];
  }];

  # Only required when using a subpath of `/home` to store your swapfile; hence
  # doing so is inadvisable.
  # systemd.services."systemd-logind".serviceConfig.ProtectHome = "read-only";

  # Systemd is used to automatically resolve the swapfile location when
  # hibernating. This means we *must* use systemd in the initrd.
  boot.initrd.systemd.enable = true;
}

Using a swap partition is probably preferable over a swap file most of the time. Consider re-partitioning instead of using swapfiles.

If you’re doing this because you’re worried that you might change your mind about partition sizes, consider looking into LVM to make resizing partitions easier, and note that growing a swap file/partition can cause performance loss due to fragmentation.

2 Likes