Custom modules

Separate functionality in custom modules and reuse where needed by importing + passing options.

zswap module

{ config, lib, pkgs, ... }:

let cfg = config.krupkat.system.zswap;

in {
  options.krupkat.system.zswap.enable = lib.mkEnableOption "Zswap";

  config = lib.mkIf cfg.enable {
    boot = {
      initrd = {
        kernelModules = [ "lz4" "z3fold" ];
        preDeviceCommands = ''
          printf lz4 > /sys/module/zswap/parameters/compressor
          printf z3fold > /sys/module/zswap/parameters/zpool
          printf 25 > /sys/module/zswap/parameters/max_pool_percent
        '';
      };

      kernel.sysctl = {
        "vm.swappiness" = 180;
        "vm.page-cluster" = 0;
      };
      kernelParams = [ "zswap.enabled=1" ];
    };
  };
}

Import and enable in the main configuration.nix file:

{ config, lib, pkgs, ... }:

{
  imports =
    [
      ./modules/zswap.nix
    ];

  krupkat.system.zswap.enable = true;

  ...
}