{ lib, ... }:
with lib;
{
  zramSwap = {
    enable = true;
    algorithm = mkDefault "zstd";
    # There is little point creating a zram greater
    # than twice the size of memory.
    memoryPercent = mkDefault 150;
    # Linux supports multithreaded compression for 1 device since 3.15.
    # See https://lkml.org/lkml/2014/2/28/404 for details.
    swapDevices = mkDefault 1;
  };

  boot.kernel.sysctl = {
    # Increase cache pressure, which increases the tendency of the kernel to
    # reclaim memory used for caching of directory and inode objects. You will use
    # less memory over a longer period of time. The performance hit is negated by
    # the downside of swapping sooner.
    "vm.vfs_cache_pressure" = mkDefault 500;

    # Increasing how aggressively the kernel will swap memory pages since we are
    # using ZRAM first.
    "vm.swappiness" = mkDefault 100;

    # Background processes will start writing right away when it hits the 1% limit
    "vm.dirty_background_ratio" = mkDefault 1;

    # The system won’t force synchronous I/O until it gets to 50% dirty_ratio.
    "vm.dirty_ratio" = mkDefault 50;
  };
}