Skip to content

Commit

Permalink
nixos/system: replace zram/zswap with zram-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Dec 29, 2024
1 parent 6b7651a commit ce80778
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 45 deletions.
4 changes: 0 additions & 4 deletions hosts/sankyuu-nixos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@
};
system = {
binfmt.enable = true;
pageCompression = {
enable = "zswap";
memoryPercent = 30;
};
};
};

Expand Down
59 changes: 18 additions & 41 deletions modules/nixos/system/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,25 @@ in
motd.enable = lib.mkEnableOption "show message of the day" // {
default = true;
};
pageCompression = {
enable = lib.mkOption {
description = "Page compression strategy.";
type = lib.types.enum [
"none"
"zram"
"zswap"
];
default = "zram";
};
algorithm = lib.mkOption {
description = "Page compression algorithm.";
type = lib.types.str;
default = "zstd";
};
memoryPercent = lib.mkOption {
description = "Maximum amount of memory (in percentage) that can be used.";
type = lib.types.int;
default = 50;
};
zram.enable = lib.mkEnableOption "page compression" // {
default = true;
};
};

config = lib.mkIf cfg.enable {
boot = {
initrd = {
systemd.enable = lib.mkDefault true;
kernelModules = lib.mkIf (cfg.pageCompression.enable == "zswap") [ "z3fold" ];
};
initrd.systemd.enable = lib.mkDefault true;

kernelParams = lib.mkIf (cfg.pageCompression.enable == "zswap") [
"zswap.compressor=${cfg.pageCompression.algorithm}"
"zswap.enabled=1"
"zswap.max_pool_percent=${toString cfg.pageCompression.memoryPercent}"
"zswap.zpool=z3fold"
];
kernelParams = [ "zswap.enabled=0" ];

kernel.sysctl = {
# Enable Magic keys
"kernel.sysrq" = 1;
# https://wiki.archlinux.org/title/Zram#Optimizing_swap_on_zram
"vm.swappiness" = lib.mkIf (cfg.pageCompression != "none") 180;
"vm.watermark_boost_factor" = lib.mkIf (cfg.pageCompression != "none") 0;
"vm.watermark_scale_factor" = lib.mkIf (cfg.pageCompression != "none") 125;
"vm.page-cluster" = lib.mkIf (cfg.pageCompression != "none") 0;
"vm.swappiness" = lib.mkIf (cfg.zram.enable) 180;
"vm.watermark_boost_factor" = lib.mkIf (cfg.zram.enable) 0;
"vm.watermark_scale_factor" = lib.mkIf (cfg.zram.enable) 125;
"vm.page-cluster" = lib.mkIf (cfg.zram.enable) 0;
};

# Enable NTFS support
Expand Down Expand Up @@ -101,6 +75,13 @@ in
journald.extraConfig = ''
SystemMaxUse=500M
'';

zram-generator = {
inherit (cfg.zram) enable;
settings.zram0 = {
zram-size = "min(ram, 8192)";
};
};
};

systemd = {
Expand All @@ -120,12 +101,8 @@ in
system.rebuild.enableNg = true;

# nixos/modules/misc/version.nix
users.motd = lib.mkIf cfg.motd.enable ''Welcome to '${config.networking.hostName}' running NixOS ${config.system.nixos.version}!'';

# Enable zram to have better memory management
zramSwap = lib.mkIf (cfg.pageCompression.enable == "zram") {
enable = true;
inherit (cfg.pageCompression) algorithm memoryPercent;
};
users.motd = lib.mkIf cfg.motd.enable ''
Welcome to '${config.networking.hostName}' running NixOS ${config.system.nixos.version}!
'';
};
}

0 comments on commit ce80778

Please sign in to comment.