Swap: Difference between revisions
Add btrfs subsection, minor readability changes |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Configuration]] | [[Category:Configuration]] | ||
= Configuration = | |||
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>. | 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>. | ||
To check your current swap setup and usage, you can use the following command: <code>swapon</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]}} | {{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]}} | ||
== Swap file == | |||
== | |||
Add a | Add a swap file with the following: | ||
{{file|/etc/nixos/configuration.nix|nix| | {{file|/etc/nixos/configuration.nix|nix| | ||
Line 24: | Line 24: | ||
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] | ||
=== Disable swap == | == Swap partition == | ||
Swap partitions are typically created during the initial disk partitioning phase of a NixOS installation. For instructions on creating swap partitions, see the relevant NixOS manual sections for [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning-UEFI UEFI]/[https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning-MBR MBR] partition schemes and [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning-formatting formatting]. | |||
== Zram swap == | |||
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. | |||
See [https://search.nixos.org/options?query=zramSwap zramSwap] for a full list of available options and their descriptions. | |||
== 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>. | 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>. | ||
Line 43: | Line 62: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | = Tips and Tricks = | ||
== 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). | 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). | ||
Line 67: | Line 75: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== ZFS and swap == | |||
OpenZFS does not support swap on zvols nor do they support swap files on a ZFS dataset. | |||
Instead you should set up a swap partition or swap file on a non-ZFS filesystem.<ref>https://utcc.utoronto.ca/~cks/space/blog/solaris/ZFSForSwapMyViews</ref> | |||
== Using swap files on Btrfs == | |||
For Btrfs file system-specific considerations, see the [[Btrfs#Swap file|Btrfs swap file section]]. | |||
== Swapspace == | |||
[https://github.com/Tookmund/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. | [https://github.com/Tookmund/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. |