How to activate zswap using zstd at boot time

I’ve been playing around with zswap (not zram, which is easy enough with the zramSwap NixOS option). I’ve set kernelParams = [ "zswap.enabled=1" "zswap.compressor=zstd" ] (and I’m using linuxPackages_latest on nixos-unstable, currently 5.11.11) but when I boot I see the following:

$ journalctl -b | grep zswap
Apr 13 17:54:23 spica kernel: Command line: initrd=\efi\nixos\x0db1dj4h1z9p8vls7kyrvlicbsdpypy-initrd-linux-5.11.11-initrd.efi init=/nix/store/pbrr653xr0b63d6h4pvxpw16pdaws02m-nixos-system-spica-21.05.20210412.311ceed/init zswap.enabled=1 zswap.compressor=zstd loglevel=4
Apr 13 17:54:23 spica kernel: Kernel command line: initrd=\efi\nixos\x0db1dj4h1z9p8vls7kyrvlicbsdpypy-initrd-linux-5.11.11-initrd.efi init=/nix/store/pbrr653xr0b63d6h4pvxpw16pdaws02m-nixos-system-spica-21.05.20210412.311ceed/init zswap.enabled=1 zswap.compressor=zstd loglevel=4
Apr 13 17:54:23 spica kernel: zswap: compressor zstd not available, using default lzo
Apr 13 17:54:23 spica kernel: zswap: loaded using pool lzo/zbud

I’ve tried adding "zstd" to several different combinations of boot.kernelModules, boot.initrd.kernelModules and boot.initrd.availableKernelModules. My reading of people running into this problem on other distros is that the zstd module needs to be loaded into the initramfs, which to me suggests that boot.initrd.kernelModules would be the right place, but it doesn’t work there either: the same compressor zstd not available error is shown, then zstd is loaded just after. I can always switch to zstd via the sysfs interface after boot, and that works fine, it’s only the boot-time configuration that doesn’t work.

I could presumably solve this by building a custom kernel changing CONFIG_CRYPTO_ZSTD to y instead of m but I don’t particularly want to go down that road if I can avoid it:

$ zgrep ZSTD /proc/config.gz 
CONFIG_HAVE_KERNEL_ZSTD=y
CONFIG_KERNEL_ZSTD=y
CONFIG_RD_ZSTD=y
# CONFIG_ZSWAP_COMPRESSOR_DEFAULT_ZSTD is not set
# CONFIG_ZRAM_DEF_COMP_ZSTD is not set
CONFIG_F2FS_FS_ZSTD=y
CONFIG_UBIFS_FS_ZSTD=y
CONFIG_SQUASHFS_ZSTD=y
# CONFIG_PSTORE_ZSTD_COMPRESS is not set
CONFIG_CRYPTO_ZSTD=m
CONFIG_ZSTD_COMPRESS=y
CONFIG_ZSTD_DECOMPRESS=y
CONFIG_DECOMPRESS_ZSTD=y
1 Like

Building custom kernel is not much work.

     nixpkgs.config.packageOverrides = p: {

         linux_4_5 = p.linux_4_5.override {

           extraConfig = "
             USBIP_VHCI_HCD y
             USBIP_HOST y
           ";
         };          
       };

in your configuration.nix and you’re done. Takes some time to compile the kernel but that’s it. initrd etc will be recreated depending on boot method. Welcome to Nix :slight_smile:

1 Like

Thanks, I know it’s not hard in terms of configuration to build and use a custom kernel, but I’d rather avoid spending all those cycles on something that really ought to work with the stock kernel (and does, just not at boot time!)

boot.initrd.availableKernelModules might be what you’ve been looking for ? You may also want to look at the module at nixpkgs/nixos/modules/* and write your own one then you can zswap.enable = true and add some more options your your config and have your module add kernel parameters etc.

1 Like

not got much tine to research but, zstd is not only used for swapping, but for firmware unpacking. I’ve got a feeling that some of those options are related to this. Could be wrong.

Zstd seems quite new and funky (untested), so i see why it may not be built by default.

I know this is an old thread, but did you try that change? If it made it work, maybe it is worth suggesting this change to the project, because current the situation is unchanged from what you’ve reported almost 3 years ago.

I never tried it no, I just gave up and used zram instead.