Jump to content

Swap: Difference between revisions

From NixOS Wiki
m final edit
Pigs (talk | contribs)
Add btrfs subsection, minor readability changes
Line 2: Line 2:


== Configuration ==
== Configuration ==
Swap on NixOS is set with with one of two options <code>swapDevices</code> or <code>zramSwap.enable</code> on <code>/etc/nixos/hardware-configuration.nix</code>.
 
Swap on NixOS is set with with one of two options <code>swapDevices</code> for traditional swap files or partitions, or <code>zramSwap.enable</code> for compressed RAM-based swap. Add either option to your <code>/etc/nixos/configuration.nix</code>.
 
{{note| <code>nixos-generate-config</code> does not automatically generate a <code>swapDevices</code> entry if your system uses a swap file or <code>/dev/zram</code> for swap. For details, see the [https://github.com/NixOS/nixpkgs/pull/63083 discussion on GitHub]}}
 
To check your current swap setup, you can use the following command: <code>swapon --show</code>


=== Add a Swapfile ===
=== Add a Swapfile ===


Add a swapfile with the following :
Add a swapfile with the following:


<syntaxhighlight lang="nix">
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
swapDevices = [{
swapDevices = [{
   device = "/var/lib/swapfile";
   device = "/var/lib/swapfile";
   size = 16*1024;
   size = 16*1024; # 16 GB
}];
}];
</syntaxhighlight>
</nowiki>
}}


Size [https://search.nixos.org/options?channel=24.11&show=swapDevices.*.size is in megabytes]
Size [https://search.nixos.org/options?channel=24.11&show=swapDevices.*.size is in megabytes]
Line 37: Line 44:


=== Enable zram swap ===
=== Enable zram swap ===
Zram is a kernel module for creating a compressed block device in RAM. The option <code>zramSwap.enable</code> creates such a zram block device and uses it as swap device.
Zram is a kernel module for creating a compressed block device in RAM.  
 
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
zramSwap.enable = true; # Creates a zram block device and uses it as a swap device
</nowiki>
}}


It is an alternative or complementary approach to swap disks, suitable for systems with enough RAM. In the event the system needs to swap it will move uncompressed RAM contents into the compressed area, saving RAM space while effectively increasing the available RAM at the cost of computational power for compression and decompression.
It is an alternative or complementary approach to swap disks, suitable for systems with enough RAM. In the event the system needs to swap it will move uncompressed RAM contents into the compressed area, saving RAM space while effectively increasing the available RAM at the cost of computational power for compression and decompression.
See [https://search.nixos.org/options?query=zramSwap zramSwap] for a full list of available options and their descriptions.


=== Encrypt swap with random key ===
=== Encrypt swap with random key ===
Line 57: Line 72:


Instead you should set up a swap partition or swapfile on a non-ZFS filesystem.<ref>https://utcc.utoronto.ca/~cks/space/blog/solaris/ZFSForSwapMyViews</ref>
Instead you should set up a swap partition or swapfile on a non-ZFS filesystem.<ref>https://utcc.utoronto.ca/~cks/space/blog/solaris/ZFSForSwapMyViews</ref>
=== Using Swap Files on Btrfs ===
For file system-specific considerations, see the [[Btrfs#Swap file|Btrfs swap file section]].


=== Tips and Tricks ===
=== Tips and Tricks ===

Revision as of 06:00, 22 April 2025


Configuration

Swap on NixOS is set with with one of two options swapDevices for traditional swap files or partitions, or zramSwap.enable for compressed RAM-based swap. Add either option to your /etc/nixos/configuration.nix.

Note: nixos-generate-config does not automatically generate a swapDevices entry if your system uses a swap file or /dev/zram for swap. For details, see the discussion on GitHub

To check your current swap setup, you can use the following command: swapon --show

Add a Swapfile

Add a swapfile with the following:

❄︎ /etc/nixos/configuration.nix
swapDevices = [{
  device = "/var/lib/swapfile";
  size = 16*1024; # 16 GB
}];

Size is in megabytes

Disable swap

To remove all swap devices from NixOS, set the following to remove the swap partition or file from being included in /etc/fstab.

swapDevices = lib.mkForce [ ];

If you are using GPT partitioning tables, systemd-gpt-auto-generator(8) will still mount your swap partition automatically. You must therefore turn on attribute 63 on your partition in the partition table. This can be done with gptfdisk or similar:

gdisk /dev/sda
x
a
<partition number>
63
<enter>
w

Enable zram swap

Zram is a kernel module for creating a compressed block device in RAM.

❄︎ /etc/nixos/configuration.nix
zramSwap.enable = true; # Creates a zram block device and uses it as a swap device

It is an alternative or complementary approach to swap disks, suitable for systems with enough RAM. In the event the system needs to swap it will move uncompressed RAM contents into the compressed area, saving RAM space while effectively increasing the available RAM at the cost of computational power for compression and decompression.

See zramSwap for a full list of available options and their descriptions.

Encrypt swap with random key

Swap can be automatically encrypted with a new key on every boot. This can be used to simplify certain disk layouts, such as securing a swap file on a filesystem partition without an encryption container (such as LUKS).

swapDevices = [{
  device = "/dev/sdXY";
  randomEncryption.enable = true; 
}];

ZFS and Swap

OpenZFS does not support swap on zvols nor do they support swapfiles on a ZFS dataset.

Instead you should set up a swap partition or swapfile on a non-ZFS filesystem.[1]

Using Swap Files on Btrfs

For file system-specific considerations, see the Btrfs swap file section.

Tips and Tricks

Swapspace

Swapspace is a dynamic swap space manager for GNU/Linux. i.e. it allows unused disk space to be utilised as swap to handle the occasional memory-intensive task, and frees the disk space once done.

Enable it via services.swapspace.enable = true; in your nixos configuration. And after switching, check that systemctl status swapspace.service is green, that's all, swapspace will auto manage swap for you.

See all the options it supports here, search.nixos.org

You can also use zramSwap along with this service.

See your active swap partitions/files with swapon. For eg.

⚠︎
Warning: Do not run the following without swapspace active + more than 34GB free disk space (assuming 8GB ram) OR without 42GB+ ram to spare.
# Read the WARNING above, and adjust 2, 20GB according to your free space
$ # nix shell nixpkgs#stress.out -c stress --vm 2 --vm-bytes 20G
$ swapon
NAME                 TYPE       SIZE  USED PRIO
/dev/zram0           partition 13.8G  2.6G    5
/var/lib/swapspace/1 file       5.2G 59.2M   -2
/var/lib/swapspace/2 file       6.1G 56.4M   -3