Skip to content

Instantly share code, notes, and snippets.

@kuznero
Last active September 30, 2023 12:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuznero/bffb7f9a0b9829bf036be09b058e4322 to your computer and use it in GitHub Desktop.
Save kuznero/bffb7f9a0b9829bf036be09b058e4322 to your computer and use it in GitHub Desktop.
Disabling swap in NixOS permanently without removing partition

Swap is activated by systemd, so let's query which unit is responsible for it on your NixOS:

sudo systemctl --type swap

This will among other things output the name of the unit that is responsible for activating swap. Copy it to your clipboard (for me the value is dev-nvme0n1p2.swap).

Now we will need to let NixOS know that we don't need any swap. For that, we will need to change the following/similar line in /etc/nixos/hardware-configuration.nix:

  swapDevices =
    [ { device = "/dev/disk/by-uuid/f1378b43-5a09-4d00-a92c-7207e83ba088"; }
    ];

To something that looks like this:

  systemd.units."dev-nvme0n1p2.swap".enable = false;
  swapDevices = [];
    # [ { device = "/dev/disk/by-uuid/f1378b43-5a09-4d00-a92c-7207e83ba088"; }
    # ];

Please note that systemd unit will have to correspond to the value you copied to the clipboard in the beginning.

To check if you have swap enabled or disabled you can use one of the following methods:

lsblk    # should produce no lines containing [SWAP]
free -th # should indicate if there is swap capacity available or not
@MichaelBurge
Copy link

This disables swapping of anonymous pages but not mapped pages. Because anonymous pages are swapped first and accessed less frequently, this will cause even more swap thrashing when you run out of memory than if you had kept a swap partition.

You need either a kernel patch or to configure a cgroup with memory.min set to all RAM along with this change in order for both types of pages to be unswappable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment