# [NixOS] Set Up Zram Swap

> Source: <https://gist.github.com/kekneus373/8e22121c23d0ced11b1c8c4a4fd1a2e6>
> Published: 2026-03-22 19:39:18+00:00

zramSwap in NixOS enables compressed swap space in RAM using the kernel's zram module, acting as a faster, disk-free alternative to traditional swap partitions or files. It's ideal for systems with sufficient RAM and helps reduce SSD wear.
-
Enable zramSwap in your configuration with:
boot.kernelModules = [ "zram" ]; zramSwap = { enable = true; priority = 100; # Higher than disk swap to prefer zram algorithm = "zstd"; # Balances speed and compression memoryPercent = 50; # Use up to 50% of RAM for zram };
-
Key benefits: Faster than disk swap, no disk I/O, reduces SSD wear.
-
Trade-offs: Uses CPU for compression; not suitable for hibernation (requires a swap partition equal to RAM size).
-
Advanced use: Supports writeback to a disk device for incompressible data via
writebackDevice
.
For detailed options, see the NixOS Wiki or the zram.nix module source.
