Swap: Difference between revisions

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 ===