fix(nixos): gate zram kernel sysctl tunings behind modules.zram.enable

The zram module was auto-imported on all hosts via base scanPaths, and its
swappiness=180/page-cluster=0 tunings leaked onto hosts that disabled zram
(e.g. idols-ai with a disk swapfile, pushing cold anon pages to disk swap).
Wrap the whole module in modules.zram.enable (default true); idols-ai now
turns off zram and its tunings with a single switch.
This commit is contained in:
Ryan Yin
2026-08-29 11:16:12 +08:00
parent 33ca0819df
commit 4b7375c077
2 changed files with 63 additions and 38 deletions
+2 -1
View File
@@ -39,7 +39,8 @@ in
]; ];
# Zram consumes physical memory for compression, which can cause a deadlock and system hang if the model size approaches the physical memory limit. # Zram consumes physical memory for compression, which can cause a deadlock and system hang if the model size approaches the physical memory limit.
zramSwap.enable = lib.mkForce false; # Disable the whole module (zram device + its swappiness=180 sysctl tunings); this host uses a disk swapfile instead.
modules.zram.enable = false;
services.sunshine.enable = false; services.sunshine.enable = false;
services.tuned.ppdSettings.main.default = lib.mkForce "performance"; services.tuned.ppdSettings.main.default = lib.mkForce "performance";
+61 -37
View File
@@ -1,44 +1,68 @@
{ lib, ... }:
{ {
# Enable in-memory compressed devices and swap space provided by the zram kernel module. lib,
# By enable this, we can store more data in memory instead of fallback to disk-based swap devices directly, config,
# and thus improve I/O performance when we have a lot of memory. ...
# }:
# https://www.kernel.org/doc/Documentation/blockdev/zram.txt let
zramSwap = { cfg = config.modules.zram;
enable = true; in
# one of "lzo", "lz4", "zstd" {
algorithm = lib.mkDefault "zstd"; options.modules.zram.enable =
# Priority of the zram swap devices. lib.mkEnableOption ''
# It should be a number higher than the priority of your disk-based swap devices zram in-memory swap device plus kernel sysctl tunings for it.
# (so that the system will fill the zram swap devices before falling back to disk swap).
priority = lib.mkDefault 100;
# Maximum total amount of memory that can be stored in the zram swap devices (as a percentage of your total memory).
# Defaults to 1/2 of your total RAM. Run zramctl to check how good memory is compressed.
# This doesnt define how much memory will be used by the zram swap devices.
memoryPercent = lib.mkDefault 50;
};
# Optimizing swap on zram The sysctl tunings (swappiness=180 etc.) are designed for in-memory swap
boot.kernel.sysctl = { devices only; they are gated behind this switch so that hosts which
# vm.swappiness - Controls kernel preference for swapping (range: 0-200, default: 60) disable zram (e.g. using a disk swapfile instead) also revert to the
# For in-memory swap devices like zram/zswap, values above 100 are recommended. kernel default swappiness instead of leaking zram tunings onto disk swap.
"vm.swappiness" = lib.mkDefault 180;
# vm.watermark_boost_factor - Controls aggressiveness of memory reclaim (default: 15000) Enabled by default; hosts that want disk-only swap should set it to false.
# Setting to 0 disables watermark boost, preventing premature memory reclamation. ''
# This allows fuller memory utilization before the kernel starts reclaiming pages. // {
"vm.watermark_boost_factor" = lib.mkDefault 0; default = true;
};
# vm.watermark_scale_factor - Controls kswapd wakeup frequency (range: 1-1000, default: 10) config = lib.mkIf cfg.enable {
# A higher value triggers background memory reclamation earlier (at 12.5% memory pressure). # Enable in-memory compressed devices and swap space provided by the zram kernel module.
# Value 125 means kswapd becomes active when free memory drops below 1/125 of total memory, # By enable this, we can store more data in memory instead of fallback to disk-based swap devices directly,
# balancing memory more proactively to prevent sudden swap storms at high swappiness values. # and thus improve I/O performance when we have a lot of memory.
"vm.watermark_scale_factor" = lib.mkDefault 125; #
# https://www.kernel.org/doc/Documentation/blockdev/zram.txt
zramSwap = {
enable = true;
# one of "lzo", "lz4", "zstd"
algorithm = lib.mkDefault "zstd";
# Priority of the zram swap devices.
# It should be a number higher than the priority of your disk-based swap devices
# (so that the system will fill the zram swap devices before falling back to disk swap).
priority = lib.mkDefault 100;
# Maximum total amount of memory that can be stored in the zram swap devices (as a percentage of your total memory).
# Defaults to 1/2 of your total RAM. Run zramctl to check how good memory is compressed.
# This doesn't define how much memory will be used by the zram swap devices.
memoryPercent = lib.mkDefault 50;
};
# vm.page-cluster - Controls swap readahead (range: 0-6, default: 3) # Optimizing swap on zram
# 0 means read only 1 page (2^0) at a time, disabling readahead. boot.kernel.sysctl = {
# For low-latency devices like zram, readahead hurts performance by fetching unnecessary data. # vm.swappiness - Controls kernel preference for swapping (range: 0-200, default: 60)
"vm.page-cluster" = lib.mkDefault 0; # For in-memory swap devices like zram/zswap, values above 100 are recommended.
"vm.swappiness" = lib.mkDefault 180;
# vm.watermark_boost_factor - Controls aggressiveness of memory reclaim (default: 15000)
# Setting to 0 disables watermark boost, preventing premature memory reclamation.
# This allows fuller memory utilization before the kernel starts reclaiming.
"vm.watermark_boost_factor" = lib.mkDefault 0;
# vm.watermark_scale_factor - Controls kswapd wakeup frequency (range: 1-1000, default: 10)
# A higher value triggers background memory reclamation earlier (at 12.5% memory pressure).
# Value 125 means kswapd becomes active when free memory drops below 1/125 of total memory,
# balancing memory more proactively to prevent sudden swap storms at high swappiness values.
"vm.watermark_scale_factor" = lib.mkDefault 125;
# vm.page-cluster - Controls swap readahead (range: 0-6, default: 3)
# 0 means read only 1 page (2^0) at a time, disabling readahead.
# For low-latency devices like zram, readahead hurts performance by fetching unnecessary data.
"vm.page-cluster" = lib.mkDefault 0;
};
}; };
} }