at 24.11-pre 4.0 kB view raw
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 = '' 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 = '' 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 = '' 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 = '' 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 = '' 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 [ "842" "lzo" "lzo-rle" "lz4" "lz4hc" "zstd" ]) str; 77 description = '' 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 = '' 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 services.zram-generator.enable = true; 109 110 services.zram-generator.settings = lib.listToAttrs 111 (builtins.map 112 (dev: { 113 name = dev; 114 value = 115 let 116 size = "${toString cfg.memoryPercent} / 100 * ram"; 117 in 118 { 119 zram-size = if cfg.memoryMax != null then "min(${size}, ${toString cfg.memoryMax} / 1024 / 1024)" else size; 120 compression-algorithm = cfg.algorithm; 121 swap-priority = cfg.priority; 122 } // lib.optionalAttrs (cfg.writebackDevice != null) { 123 writeback-device = cfg.writebackDevice; 124 }; 125 }) 126 devices); 127 128 }; 129 130}