1{ config, lib, pkgs, ... }:
2
3let
4
5 cfg = config.zramSwap;
6 devices = map (nr: "zram${toString nr}") (lib.range 0 (cfg.swapDevices - 1));
7
8in
9
10{
11
12 imports = [
13 (lib.mkRemovedOptionModule [ "zramSwap" "numDevices" ] "Using ZRAM devices as general purpose ephemeral block devices is no longer supported")
14 ];
15
16 ###### interface
17
18 options = {
19
20 zramSwap = {
21
22 enable = lib.mkOption {
23 default = false;
24 type = lib.types.bool;
25 description = lib.mdDoc ''
26 Enable in-memory compressed devices and swap space provided by the zram
27 kernel module.
28 See [
29 https://www.kernel.org/doc/Documentation/blockdev/zram.txt
30 ](https://www.kernel.org/doc/Documentation/blockdev/zram.txt).
31 '';
32 };
33
34 swapDevices = lib.mkOption {
35 default = 1;
36 type = lib.types.int;
37 description = lib.mdDoc ''
38 Number of zram devices to be used as swap, recommended is 1.
39 '';
40 };
41
42 memoryPercent = lib.mkOption {
43 default = 50;
44 type = lib.types.int;
45 description = lib.mdDoc ''
46 Maximum total amount of memory that can be stored in the zram swap devices
47 (as a percentage of your total memory). Defaults to 1/2 of your total
48 RAM. Run `zramctl` to check how good memory is compressed.
49 This doesn't define how much memory will be used by the zram swap devices.
50 '';
51 };
52
53 memoryMax = lib.mkOption {
54 default = null;
55 type = with lib.types; nullOr int;
56 description = lib.mdDoc ''
57 Maximum total amount of memory (in bytes) that can be stored in the zram
58 swap devices.
59 This doesn't define how much memory will be used by the zram swap devices.
60 '';
61 };
62
63 priority = lib.mkOption {
64 default = 5;
65 type = lib.types.int;
66 description = lib.mdDoc ''
67 Priority of the zram swap devices. It should be a number higher than
68 the priority of your disk-based swap devices (so that the system will
69 fill the zram swap devices before falling back to disk swap).
70 '';
71 };
72
73 algorithm = lib.mkOption {
74 default = "zstd";
75 example = "lz4";
76 type = with lib.types; either (enum [ "lzo" "lz4" "zstd" ]) str;
77 description = lib.mdDoc ''
78 Compression algorithm. `lzo` has good compression,
79 but is slow. `lz4` has bad compression, but is fast.
80 `zstd` is both good compression and fast, but requires newer kernel.
81 You can check what other algorithms are supported by your zram device with
82 {command}`cat /sys/class/block/zram*/comp_algorithm`
83 '';
84 };
85
86 writebackDevice = lib.mkOption {
87 default = null;
88 example = "/dev/zvol/tarta-zoot/swap-writeback";
89 type = lib.types.nullOr lib.types.path;
90 description = lib.mdDoc ''
91 Write incompressible pages to this device,
92 as there's no gain from keeping them in RAM.
93 '';
94 };
95 };
96
97 };
98
99 config = lib.mkIf cfg.enable {
100
101 assertions = [
102 {
103 assertion = cfg.writebackDevice == null || cfg.swapDevices <= 1;
104 message = "A single writeback device cannot be shared among multiple zram devices";
105 }
106 ];
107
108
109 system.requiredKernelConfig = with config.lib.kernelConfig; [
110 (isModule "ZRAM")
111 ];
112
113 # Disabling this for the moment, as it would create and mkswap devices twice,
114 # once in stage 2 boot, and again when the zram-reloader service starts.
115 # boot.kernelModules = [ "zram" ];
116
117 systemd.packages = [ pkgs.zram-generator ];
118 systemd.services."systemd-zram-setup@".path = [ pkgs.util-linux ]; # for mkswap
119
120 environment.etc."systemd/zram-generator.conf".source =
121 (pkgs.formats.ini { }).generate "zram-generator.conf" (lib.listToAttrs
122 (builtins.map
123 (dev: {
124 name = dev;
125 value =
126 let
127 size = "${toString cfg.memoryPercent} / 100 * ram";
128 in
129 {
130 zram-size = if cfg.memoryMax != null then "min(${size}, ${toString cfg.memoryMax} / 1024 / 1024)" else size;
131 compression-algorithm = cfg.algorithm;
132 swap-priority = cfg.priority;
133 } // lib.optionalAttrs (cfg.writebackDevice != null) {
134 writeback-device = cfg.writebackDevice;
135 };
136 })
137 devices));
138
139 };
140
141}