···841841 </listitem>
842842 <listitem>
843843 <para>
844844+ The <literal>zramSwap</literal> is now implemented with
845845+ <literal>zram-generator</literal>, and the option
846846+ <literal>zramSwap.numDevices</literal> for using ZRAM devices
847847+ as general purpose ephemeral block devices has been removed.
848848+ </para>
849849+ </listitem>
850850+ <listitem>
851851+ <para>
844852 The <literal>unifi-poller</literal> package and corresponding
845853 NixOS module have been renamed to <literal>unpoller</literal>
846854 to match upstream.
+2
nixos/doc/manual/release-notes/rl-2305.section.md
···209209210210- The `services.fwupd` module now allows arbitrary daemon settings to be configured in a structured manner ([`services.fwupd.daemonSettings`](#opt-services.fwupd.daemonSettings)).
211211212212+- The `zramSwap` is now implemented with `zram-generator`, and the option `zramSwap.numDevices` for using ZRAM devices as general purpose ephemeral block devices has been removed.
213213+212214- The `unifi-poller` package and corresponding NixOS module have been renamed to `unpoller` to match upstream.
213215214216- The new option `services.tailscale.useRoutingFeatures` controls various settings for using Tailscale features like exit nodes and subnet routers. If you wish to use your machine as an exit node, you can set this setting to `server`, otherwise if you wish to use an exit node you can set this setting to `client`. The strict RPF warning has been removed as the RPF will be loosened automatically based on the value of this setting.
+38-122
nixos/modules/config/zram.nix
···11{ config, lib, pkgs, ... }:
2233-with lib;
44-53let
6475 cfg = config.zramSwap;
88-99- # don't set swapDevices as mkDefault, so we can detect user had read our warning
1010- # (see below) and made an action (or not)
1111- devicesCount = if cfg.swapDevices != null then cfg.swapDevices else cfg.numDevices;
1212-1313- devices = map (nr: "zram${toString nr}") (range 0 (devicesCount - 1));
1414-1515- modprobe = "${pkgs.kmod}/bin/modprobe";
1616-1717- warnings =
1818- assert cfg.swapDevices != null -> cfg.numDevices >= cfg.swapDevices;
1919- flatten [
2020- (optional (cfg.numDevices > 1 && cfg.swapDevices == null) ''
2121- Using several small zram devices as swap is no better than using one large.
2222- Set either zramSwap.numDevices = 1 or explicitly set zramSwap.swapDevices.
2323-2424- Previously multiple zram devices were used to enable multithreaded
2525- compression. Linux supports multithreaded compression for 1 device
2626- since 3.15. See https://lkml.org/lkml/2014/2/28/404 for details.
2727- '')
2828- ];
66+ devices = map (nr: "zram${toString nr}") (lib.range 0 (cfg.swapDevices - 1));
297308in
3193210{
33111212+ imports = [
1313+ (lib.mkRemovedOptionModule [ "zramSwap" "numDevices" ] "Using ZRAM devices as general purpose ephemeral block devices is no longer supported")
1414+ ];
1515+3416 ###### interface
35173618 options = {
37193820 zramSwap = {
39214040- enable = mkOption {
2222+ enable = lib.mkOption {
4123 default = false;
4242- type = types.bool;
2424+ type = lib.types.bool;
4325 description = lib.mdDoc ''
4426 Enable in-memory compressed devices and swap space provided by the zram
4527 kernel module.
···4931 '';
5032 };
51335252- numDevices = mkOption {
5353- default = 1;
5454- type = types.int;
5555- description = lib.mdDoc ''
5656- Number of zram devices to create. See also
5757- `zramSwap.swapDevices`
5858- '';
5959- };
6060-6161- swapDevices = mkOption {
6262- default = null;
3434+ swapDevices = lib.mkOption {
3535+ default = 0;
6336 example = 1;
6464- type = with types; nullOr int;
3737+ type = lib.types.int;
6538 description = lib.mdDoc ''
6666- Number of zram devices to be used as swap. Must be
6767- `<= zramSwap.numDevices`.
6868- Default is same as `zramSwap.numDevices`, recommended is 1.
3939+ Number of zram devices to be used as swap, recommended is 1.
6940 '';
7041 };
71427272- memoryPercent = mkOption {
4343+ memoryPercent = lib.mkOption {
7344 default = 50;
7474- type = types.int;
4545+ type = lib.types.int;
7546 description = lib.mdDoc ''
7647 Maximum total amount of memory that can be stored in the zram swap devices
7748 (as a percentage of your total memory). Defaults to 1/2 of your total
···8051 '';
8152 };
82538383- memoryMax = mkOption {
5454+ memoryMax = lib.mkOption {
8455 default = null;
8585- type = with types; nullOr int;
5656+ type = with lib.types; nullOr int;
8657 description = lib.mdDoc ''
8758 Maximum total amount of memory (in bytes) that can be stored in the zram
8859 swap devices.
···9061 '';
9162 };
92639393- priority = mkOption {
6464+ priority = lib.mkOption {
9465 default = 5;
9595- type = types.int;
6666+ type = lib.types.int;
9667 description = lib.mdDoc ''
9768 Priority of the zram swap devices. It should be a number higher than
9869 the priority of your disk-based swap devices (so that the system will
···10071 '';
10172 };
10273103103- algorithm = mkOption {
7474+ algorithm = lib.mkOption {
10475 default = "zstd";
10576 example = "lz4";
106106- type = with types; either (enum [ "lzo" "lz4" "zstd" ]) str;
7777+ type = with lib.types; either (enum [ "lzo" "lz4" "zstd" ]) str;
10778 description = lib.mdDoc ''
10879 Compression algorithm. `lzo` has good compression,
10980 but is slow. `lz4` has bad compression, but is fast.
···1168711788 };
11889119119- config = mkIf cfg.enable {
120120-121121- inherit warnings;
9090+ config = lib.mkIf cfg.enable {
1229112392 system.requiredKernelConfig = with config.lib.kernelConfig; [
12493 (isModule "ZRAM")
···12897 # once in stage 2 boot, and again when the zram-reloader service starts.
12998 # boot.kernelModules = [ "zram" ];
13099131131- boot.extraModprobeConfig = ''
132132- options zram num_devices=${toString cfg.numDevices}
133133- '';
134134-135135- boot.kernelParams = ["zram.num_devices=${toString cfg.numDevices}"];
136136-137137- services.udev.extraRules = ''
138138- KERNEL=="zram[0-9]*", ENV{SYSTEMD_WANTS}="zram-init-%k.service", TAG+="systemd"
139139- '';
140140-141141- systemd.services =
142142- let
143143- createZramInitService = dev:
144144- nameValuePair "zram-init-${dev}" {
145145- description = "Init swap on zram-based device ${dev}";
146146- after = [ "dev-${dev}.device" "zram-reloader.service" ];
147147- requires = [ "dev-${dev}.device" "zram-reloader.service" ];
148148- before = [ "dev-${dev}.swap" ];
149149- requiredBy = [ "dev-${dev}.swap" ];
150150- unitConfig.DefaultDependencies = false; # needed to prevent a cycle
151151- serviceConfig = {
152152- Type = "oneshot";
153153- RemainAfterExit = true;
154154- ExecStop = "${pkgs.runtimeShell} -c 'echo 1 > /sys/class/block/${dev}/reset'";
155155- };
156156- script = ''
157157- set -euo pipefail
158158-159159- # Calculate memory to use for zram
160160- mem=$(${pkgs.gawk}/bin/awk '/MemTotal: / {
161161- value=int($2*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024);
162162- ${lib.optionalString (cfg.memoryMax != null) ''
163163- memory_max=int(${toString cfg.memoryMax}/${toString devicesCount});
164164- if (value > memory_max) { value = memory_max }
165165- ''}
166166- print value
167167- }' /proc/meminfo)
168168-169169- ${pkgs.util-linux}/sbin/zramctl --size $mem --algorithm ${cfg.algorithm} /dev/${dev}
170170- ${pkgs.util-linux}/sbin/mkswap /dev/${dev}
171171- '';
172172- restartIfChanged = false;
173173- };
174174- in listToAttrs ((map createZramInitService devices) ++ [(nameValuePair "zram-reloader"
175175- {
176176- description = "Reload zram kernel module when number of devices changes";
177177- wants = [ "systemd-udevd.service" ];
178178- after = [ "systemd-udevd.service" ];
179179- unitConfig.DefaultDependencies = false; # needed to prevent a cycle
180180- serviceConfig = {
181181- Type = "oneshot";
182182- RemainAfterExit = true;
183183- ExecStartPre = "-${modprobe} -r zram";
184184- ExecStart = "-${modprobe} zram";
185185- ExecStop = "-${modprobe} -r zram";
186186- };
187187- restartTriggers = [
188188- cfg.numDevices
189189- cfg.algorithm
190190- cfg.memoryPercent
191191- ];
192192- restartIfChanged = true;
193193- })]);
100100+ systemd.packages = [ pkgs.zram-generator ];
101101+ systemd.services."systemd-zram-setup@".path = [ pkgs.util-linux ]; # for mkswap
194102195195- swapDevices =
196196- let
197197- useZramSwap = dev:
198198- {
199199- device = "/dev/${dev}";
200200- priority = cfg.priority;
201201- };
202202- in map useZramSwap devices;
103103+ environment.etc."systemd/zram-generator.conf".source =
104104+ (pkgs.formats.ini { }).generate "zram-generator.conf" (lib.listToAttrs
105105+ (builtins.map
106106+ (dev: {
107107+ name = dev;
108108+ value =
109109+ let
110110+ size = "${toString cfg.memoryPercent} / 100 * ram";
111111+ in
112112+ {
113113+ zram-size = if cfg.memoryMax != null then "min(${size}, ${toString cfg.memoryMax} / 1024 / 1024)" else size;
114114+ compression-algorithm = cfg.algorithm;
115115+ swap-priority = cfg.priority;
116116+ };
117117+ })
118118+ devices));
203119204120 };
205121