summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuke Granger-Brown <git@lukegb.com>2020-11-05 02:29:46 +0000
committerLuke Granger-Brown <git@lukegb.com>2020-11-25 13:43:38 +0000
commitad62155cb65a9aa0153dbde5be8698b715003473 (patch)
tree9422492db3119fd50082d9b9497d5926529bdc7f
parentdb63be9c9106cf6df3bf8741a3b92c1515796c86 (diff)
downloadnixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.tar
nixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.tar.gz
nixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.tar.bz2
nixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.tar.lz
nixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.tar.xz
nixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.tar.zst
nixpkgs-ad62155cb65a9aa0153dbde5be8698b715003473.zip
nixos/zram: add zramSwap.memoryMax option
This allows capping the total amount of memory that will be used for
zram-swap, in addition to the percentage-based calculation, which is
useful when blanket-applying a configuration to many machines.

This is based off the strategy used by Fedora for their rollout of
zram-swap-by-default in Fedora 33
(https://fedoraproject.org/wiki/Changes/SwapOnZRAM), which caps the
maximum amount of memory used for zram at 4GiB.

In future it might be good to port this to the systemd zram-generator,
instead of using this separate infrastructure.
-rw-r--r--nixos/modules/config/zram.nix16
1 files changed, 15 insertions, 1 deletions
diff --git a/nixos/modules/config/zram.nix b/nixos/modules/config/zram.nix
index 341101bc184..1f513b7e4da 100644
--- a/nixos/modules/config/zram.nix
+++ b/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);
+                    ${lib.optionalString (cfg.memoryMax != null) ''
+                      memory_max=int(${toString cfg.memoryMax}/${toString devicesCount});
+                      if (value > memory_max) { value = memory_max }
+                    ''}
+                  print value
               }' /proc/meminfo)
 
               ${pkgs.util-linux}/sbin/zramctl --size $mem --algorithm ${cfg.algorithm} /dev/${dev}