diff --git a/maintainers/maintainer-list.nix b/maintainers/maintainer-list.nix index 8d0ab4f476821..946b27a8f3afa 100644 --- a/maintainers/maintainer-list.nix +++ b/maintainers/maintainer-list.nix @@ -19031,6 +19031,12 @@ githubId = 7536431; name = "Jonas Fierlings"; }; + pigsinablanket = { + email = "nixpkgs@pigs.dev"; + github = "pigsinspace"; + githubId = 28584473; + name = "Daniel Reimer"; + }; pimeys = { email = "julius@nauk.io"; github = "pimeys"; diff --git a/nixos/doc/manual/release-notes/rl-2505.section.md b/nixos/doc/manual/release-notes/rl-2505.section.md index 680d3ffb55be6..9a2912002cdc3 100644 --- a/nixos/doc/manual/release-notes/rl-2505.section.md +++ b/nixos/doc/manual/release-notes/rl-2505.section.md @@ -72,6 +72,8 @@ - [uMurmur](https://umurmur.net), minimalistic Mumble server primarily targeted to run on embedded computers. Available as [services.umurmur](options.html#opt-services.umurmur). +- [zswap](https://www.kernel.org/doc/Documentation/vm/zswap.txt) is a lightweight compressed cache for swap pages. It takes pages that are in the process of being swapped out and attempts to compress them into a dynamically allocated RAM-based memory pool. Available as [zswap](options.html#zswap) + - [Zenoh](https://zenoh.io/), a pub/sub/query protocol with low overhead. The Zenoh router daemon is available as [services.zenohd](options.html#opt-services.zenohd.enable) - [ytdl-sub](https://github.com/jmbannon/ytdl-sub), a tool that downloads media via yt-dlp and prepares it for your favorite media player, including Kodi, Jellyfin, Plex, Emby, and modern music players. Available as [services.ytdl-sub](options.html#opt-services.ytdl-sub.instances). diff --git a/nixos/modules/config/zswap.nix b/nixos/modules/config/zswap.nix new file mode 100644 index 0000000000000..e5d39a8516f81 --- /dev/null +++ b/nixos/modules/config/zswap.nix @@ -0,0 +1,110 @@ +{ + config, + lib, + pkgs, + ... +}: + +let + + cfg = config.zswap; + +in + +{ + ###### interface + + options = { + + zswap = { + + enable = lib.mkOption { + default = false; + type = lib.types.bool; + description = '' + Enable zswap, a lightweight compressed cache for swap pages. + See [ + https://www.kernel.org/doc/Documentation/vm/zswap.txt + ] (https://www.kernel.org/doc/Documentation/vm/zswap.txt) + ''; + }; + + maxPoolPercent = lib.mkOption { + default = 20; + type = lib.types.int; + description = '' + The maximum percentage of memory that the compressed pool can occupy. + ''; + }; + + compressor = lib.mkOption { + default = "zstd"; + example = "lzo"; + type = + with lib.types; + either (enum [ + "842" + "lzo" + "lz4" + "lz4hc" + "zstd" + ]) str; + description = '' + Compression algorithm. `lzo` has good compression, + but is slow. `lz4` has bad compression, but is fast. + `zstd` is both good compression and fast. + ''; + }; + + zpool = lib.mkOption { + default = "zsmalloc"; + type = + with lib.types; + either (enum [ + "zsmalloc" + "zbud" + ]) str; + description = '' + Zswap makes use of zpool for managing the compressed memory pool. + See [ + https://docs.kernel.org/admin-guide/mm/zswap.html#design + ] (https://docs.kernel.org/admin-guide/mm/zswap.html#design). + ''; + }; + + acceptThresholdPercent = lib.mkOption { + default = 90; + type = lib.types.int; + description = '' + Sets the threshold at which zswap would start accepting pages again + after it became full. + ''; + }; + }; + + }; + + config = lib.mkMerge [ + + (lib.mkIf cfg.enable { + boot.kernelParams = [ "zswap.enabled=1" ]; + + system.activationScripts.zswap-activate = '' + echo ${toString cfg.maxPoolPercent} > /sys/module/zswap/parameters/max_pool_percent + echo ${cfg.compressor} > /sys/module/zswap/parameters/compressor + echo ${cfg.zpool} > /sys/module/zswap/parameters/zpool + echo ${toString cfg.acceptThresholdPercent} > /sys/module/zswap/parameters/accept_threshold_percent + echo "Y" > /sys/module/zswap/parameters/enabled + ''; + }) + + (lib.mkIf (!cfg.enable) { + system.activationScripts.zswap-activate = '' + echo N > /sys/module/zswap/parameters/enabled + ''; + }) + + ]; + + meta.maintainers = with lib.maintainers; [ pigsinablanket ]; +} diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 4b487179231b6..57ef2cca478d6 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -45,6 +45,7 @@ ./config/xdg/sounds.nix ./config/xdg/terminal-exec.nix ./config/zram.nix + ./config/zswap.nix ./hardware/acpilight.nix ./hardware/all-firmware.nix ./hardware/all-hardware.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index e3e2f5763d170..449d25d8526e8 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -1500,6 +1500,7 @@ in zram-generator = runTest ./zram-generator.nix; zrepl = runTest ./zrepl.nix; zsh-history = runTest ./zsh-history.nix; + zswap = runTest ./zswap.nix; zwave-js = runTest ./zwave-js.nix; zwave-js-ui = runTest ./zwave-js-ui.nix; } diff --git a/nixos/tests/zswap.nix b/nixos/tests/zswap.nix new file mode 100644 index 0000000000000..0c7303250fba6 --- /dev/null +++ b/nixos/tests/zswap.nix @@ -0,0 +1,32 @@ +{ lib, ... }: +{ + name = "zswap"; + meta.maintainers = with lib.maintainers; [ pigsinablanket ]; + + nodes.machine = + { ... }: + { + zswap = { + enable = true; + compressor = "zstd"; + zpool = "zsmalloc"; + maxPoolPercent = 25; + acceptThresholdPercent = 85; + }; + + }; + + testScript = '' + machine.start() + + machine.wait_for_unit("multi-user.target") + + machine.succeed("cat /sys/module/zswap/parameters/enabled | grep Y") + machine.succeed("cat /sys/module/zswap/parameters/compressor | grep zstd") + machine.succeed("cat /sys/module/zswap/parameters/zpool | grep zsmalloc") + machine.succeed("cat /sys/module/zswap/parameters/max_pool_percent | grep 25") + machine.succeed("cat /sys/module/zswap/parameters/accept_threshold_percent | grep 85") + + machine.succeed("dmesg | grep 'zswap: loaded using pool zstd/zsmalloc'") + ''; +}