NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
16.96k stars 13.33k forks source link

Module request: config.zswap #119244

Open ShamrockLee opened 3 years ago

ShamrockLee commented 3 years ago

Issue description

There is currently no specific modules for zswap, and people configure it semi-manually using boot.kernelParams and boot.initrd.kernelModules. Having a NixOS module that enables and configures zswap would be convenient to users, and it would also make configuration more structural.

Technical details

The current way to enable the zswap kernel feature is specifying the needed kernel parameters and kernel modules. The following example is borrowed from #44901 by @ikervagyok :

{config, pkgs, ...}:
{
  boot.kernelParams = [ "zswap.enabled=1" "zswap.compressor=lz4" "zswap.zpool=z3fold" ];
  boot.initrd.kernelModules = [ "lz4" "z3fold" ];
  # ...
}

The options may include

ikervagyok commented 3 years ago

Just to remind people who find this issue, a custom kernel could be necessary depending on the options you want to use, e.g.:

{ boot.kernelPatches =
  optional (config.swapDevices != []) {
    name = "zswap";
    patch = null;
    extraConfig = ''
      Z3FOLD y
      CRYPTO_LZ4 y
    '';
  };
}

@ShamrockLee thanks for bringing this up again - do you have any code for such a module lying around?

ShamrockLee commented 3 years ago

@ShamrockLee thanks for bringing this up again - do you have any code for such a module lying around?

No.

My knowledge toward the Linux kernel is very limited, and I havent found any documentation (beside the issue) about how to enable it on NixOS, so I'm not sure how to do it right.

stale[bot] commented 2 years ago

I marked this as stale due to inactivity. → More info

nixos-discourse commented 2 years ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nixified-kernelparams/14564/5

stale[bot] commented 2 years ago

I marked this as stale due to inactivity. → More info

Sepero commented 1 year ago

Please include these in the default kernel.

Sepero commented 1 year ago

Everybody keeps reinventing the wheel, and nobody's helping each other solve this simple problem. I suck at writing Nix and Systemd units, but here's my solution that will hopefully help others.

A Hacked Systemd Unit to Enable Zswap. It's not pretty, but it works for me. I welcome improvements.

systemd.services.zswap = {
  description = "Enable ZSwap, set to LZ4 and Z3FOLD";
  enable = true;
  wantedBy = [ "basic.target" ];
  path = [ pkgs.bash ];
  serviceConfig = {
    ExecStart = ''${pkgs.bash}/bin/bash -c 'cd /sys/module/zswap/parameters&& \
    echo 1 > enabled&& \
    echo 20 > max_pool_percent&& \
    echo lz4 > compressor&& \
    echo z3fold > zpool'
    '';
  Type = "simple";
  };
};

You can verify it works by running this

grep -R . /sys/module/zswap/parameters/
nixos-discourse commented 1 month ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/working-zswap-configuration-different-from-zram/47804/5

nixos-discourse commented 1 month ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/working-zswap-configuration-different-from-zram/47804/7