diff --git a/ops/nixos/lib/common.nix b/ops/nixos/lib/common.nix index 4cfb9837e9..1375c86530 100644 --- a/ops/nixos/lib/common.nix +++ b/ops/nixos/lib/common.nix @@ -45,6 +45,11 @@ in time.timeZone = mkDefault "Etc/UTC"; + zramSwap = { + enable = true; + memoryMax = 4 * 1024 * 1024 * 1024; + }; + environment.systemPackages = with pkgs; [ vim rxvt_unicode.terminfo tmux rebuilder tailscale rsync (mercurial.overridePythonAttrs (origAttrs: { diff --git a/third_party/nixpkgs/nixos/modules/config/zram.nix b/third_party/nixpkgs/nixos/modules/config/zram.nix index 5e9870bf6b..ef6e89599e 100644 --- a/third_party/nixpkgs/nixos/modules/config/zram.nix +++ b/third_party/nixpkgs/nixos/modules/config/zram.nix @@ -80,6 +80,15 @@ in ''; }; + memoryMax = mkOption { + default = null; + type = with types; nullOr int; + description = '' + Maximum total amount of memory (in bytes) that can be used by the zram + swap devices. + ''; + }; + priority = mkOption { default = 5; type = types.int; @@ -146,7 +155,12 @@ in # Calculate memory to use for zram mem=$(${pkgs.gawk}/bin/awk '/MemTotal: / { - print int($2*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024) + value=int($2*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024); + ${if cfg.memoryMax != null then '' + memory_max=int(${toString cfg.memoryMax}/${toString devicesCount}); + if (value > memory_max) { value = memory_max } + '' else ""} + print value }' /proc/meminfo) ${pkgs.utillinux}/sbin/zramctl --size $mem --algorithm ${cfg.algorithm} /dev/${dev}