From ac745a5762390a545f6250c8f6d1230b3ea4e098 Mon Sep 17 00:00:00 2001 From: Lumpiasty Date: Thu, 19 Feb 2026 20:32:43 +0100 Subject: [PATCH] limit arc cache size to 5% of ram --- modules/default.nix | 1 + modules/system/zfs.nix | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 modules/system/zfs.nix diff --git a/modules/default.nix b/modules/default.nix index 1536ffc..ecc2178 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -15,6 +15,7 @@ system/ssh.nix system/gaming.nix system/nix.nix + system/zfs.nix desktop/plasma.nix desktop/touchpad.nix diff --git a/modules/system/zfs.nix b/modules/system/zfs.nix new file mode 100644 index 0000000..cf46178 --- /dev/null +++ b/modules/system/zfs.nix @@ -0,0 +1,37 @@ +{ config, lib, pkgs, modulesPath, ... }: + +{ + config = lib.mkIf config.boot.zfs.enabled { + # Set ARC max to 5% of physical RAM at boot + systemd.services."zfs-arc-limit" = { + description = "Set ZFS ARC max to 5% of physical RAM"; + # Ensure the module is loaded before we write to /sys + after = [ "systemd-modules-load.service" ]; + # Run early, but it’s fine if ZFS has already imported; the limit still applies + wantedBy = [ "multi-user.target" ]; + path = [ pkgs.gawk ]; + script = '' + set -euo pipefail + # Total RAM in kB + mem_kb=$(awk '/MemTotal:/ {print $2}' /proc/meminfo) + echo "DEBUG: Total RAM: $mem_kb kB" + # 5%, in bytes + arc_max_bytes=$(( mem_kb * 1024 / 100 * 5 )) + echo "DEBUG: Setting ZFS ARC max to: $arc_max_bytes bytes" + param="/sys/module/zfs/parameters/zfs_arc_max" + if [ -w "$param" ]; then + echo "DEBUG: Writing to $param" + echo "$arc_max_bytes" > "$param" + echo "DEBUG: ZFS ARC max successfully set" + else + echo "WARN: $param not writable; is the zfs module loaded?" >&2 + exit 0 + fi + ''; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + }; + }; +} \ No newline at end of file