Swap: Difference between revisions
imported>Yuu m Isa moved page Disable Swap on NixOS to Swap: few content, so better to unify in a Swap article |
Add section about zram as an option for swap on nixos |
||
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
Swap on NixOS is set with | [[Category: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>. | |||
=== Add a Swapfile === | |||
Add a swapfile with the following : | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
swapDevices = lib | swapDevices = [ { | ||
device = "/var/lib/swapfile"; | |||
size = 16*1024; | |||
} ]; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Size [https://search.nixos.org/options?channel=23.05&show=swapDevices.*.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 <code>/etc/fstab</code>. | |||
<syntaxhighlight lang="nix"> | |||
swapDevices = lib.mkForce [ ]; | |||
</syntaxhighlight> | |||
This can be done with gptfdisk or similar: | If you are using GPT partitioning tables, <code>systemd-gpt-auto-generator(8)</code> 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: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
Line 23: | Line 35: | ||
w | w | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== 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. | |||
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. | |||
=== 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). | |||
<syntaxhighlight lang="nix"> | |||
swapDevices = [ { | |||
device = "/dev/sdXY"; | |||
randomEncryption.enable = true; | |||
} ]; | |||
</syntaxhighlight> | |||
=== 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.<ref>https://utcc.utoronto.ca/~cks/space/blog/solaris/ZFSForSwapMyViews</ref> |
Latest revision as of 12:47, 24 July 2024
Configuration
Swap on NixOS is set with with one of two options swapDevices
or zramSwap.enable
on /etc/nixos/hardware-configuration.nix
.
Add a Swapfile
Add a swapfile with the following :
swapDevices = [ {
device = "/var/lib/swapfile";
size = 16*1024;
} ];
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. The option zramSwap.enable
creates such a zram block device and uses it as 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.
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]