2014-04-25 01:56:36 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.zramSwap;
|
2023-02-02 04:28:12 +01:00
|
|
|
devices = map (nr: "zram${toString nr}") (lib.range 0 (cfg.swapDevices - 1));
|
2018-12-27 23:00:48 +01:00
|
|
|
|
2014-04-25 01:56:36 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [ "zramSwap" "numDevices" ] "Using ZRAM devices as general purpose ephemeral block devices is no longer supported")
|
|
|
|
];
|
|
|
|
|
2014-04-25 01:56:36 +02:00
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
zramSwap = {
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
enable = lib.mkOption {
|
2014-04-25 01:56:36 +02:00
|
|
|
default = false;
|
2023-02-02 04:28:12 +01:00
|
|
|
type = lib.types.bool;
|
2014-04-25 01:56:36 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-12-27 23:00:48 +01:00
|
|
|
Enable in-memory compressed devices and swap space provided by the zram
|
|
|
|
kernel module.
|
|
|
|
See [
|
|
|
|
https://www.kernel.org/doc/Documentation/blockdev/zram.txt
|
|
|
|
](https://www.kernel.org/doc/Documentation/blockdev/zram.txt).
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
swapDevices = lib.mkOption {
|
2023-02-07 02:16:16 +01:00
|
|
|
default = 1;
|
2023-02-02 04:28:12 +01:00
|
|
|
type = lib.types.int;
|
2018-12-27 23:00:48 +01:00
|
|
|
description = lib.mdDoc ''
|
2023-02-02 04:28:12 +01:00
|
|
|
Number of zram devices to be used as swap, recommended is 1.
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
memoryPercent = lib.mkOption {
|
2014-04-25 01:56:36 +02:00
|
|
|
default = 50;
|
2023-02-02 04:28:12 +01:00
|
|
|
type = lib.types.int;
|
2014-04-25 01:56:36 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-08-31 15:15:02 +02:00
|
|
|
Maximum total amount of memory that can be stored in the zram swap devices
|
2014-04-25 01:56:36 +02:00
|
|
|
(as a percentage of your total memory). Defaults to 1/2 of your total
|
2022-08-31 15:15:02 +02:00
|
|
|
RAM. Run `zramctl` to check how good memory is compressed.
|
|
|
|
This doesn't define how much memory will be used by the zram swap devices.
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
memoryMax = lib.mkOption {
|
2020-11-05 03:29:46 +01:00
|
|
|
default = null;
|
2023-02-02 04:28:12 +01:00
|
|
|
type = with lib.types; nullOr int;
|
2020-11-05 03:29:46 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-08-31 15:15:02 +02:00
|
|
|
Maximum total amount of memory (in bytes) that can be stored in the zram
|
2020-11-05 03:29:46 +01:00
|
|
|
swap devices.
|
2022-08-31 15:15:02 +02:00
|
|
|
This doesn't define how much memory will be used by the zram swap devices.
|
2020-11-05 03:29:46 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
priority = lib.mkOption {
|
2014-04-25 01:56:36 +02:00
|
|
|
default = 5;
|
2023-02-02 04:28:12 +01:00
|
|
|
type = lib.types.int;
|
2014-04-25 01:56:36 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Priority of the zram swap devices. It should be a number higher than
|
|
|
|
the priority of your disk-based swap devices (so that the system will
|
|
|
|
fill the zram swap devices before falling back to disk swap).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
algorithm = lib.mkOption {
|
2020-05-21 20:30:03 +02:00
|
|
|
default = "zstd";
|
2019-03-07 01:07:52 +01:00
|
|
|
example = "lz4";
|
2023-02-02 04:28:12 +01:00
|
|
|
type = with lib.types; either (enum [ "lzo" "lz4" "zstd" ]) str;
|
2018-12-27 23:00:48 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Compression algorithm. `lzo` has good compression,
|
|
|
|
but is slow. `lz4` has bad compression, but is fast.
|
2019-03-07 01:07:52 +01:00
|
|
|
`zstd` is both good compression and fast, but requires newer kernel.
|
2018-12-27 23:00:48 +01:00
|
|
|
You can check what other algorithms are supported by your zram device with
|
2022-08-13 11:47:02 +02:00
|
|
|
{command}`cat /sys/class/block/zram*/comp_algorithm`
|
2018-12-27 23:00:48 +01:00
|
|
|
'';
|
|
|
|
};
|
2023-03-20 15:50:11 +01:00
|
|
|
|
|
|
|
writebackDevice = lib.mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/dev/zvol/tarta-zoot/swap-writeback";
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Write incompressible pages to this device,
|
|
|
|
as there's no gain from keeping them in RAM.
|
|
|
|
'';
|
|
|
|
};
|
2014-04-25 01:56:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-02-02 04:28:12 +01:00
|
|
|
config = lib.mkIf cfg.enable {
|
2018-12-27 23:00:48 +01:00
|
|
|
|
2023-03-20 15:50:11 +01:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.writebackDevice == null || cfg.swapDevices <= 1;
|
|
|
|
message = "A single writeback device cannot be shared among multiple zram devices";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-08-13 09:38:40 +02:00
|
|
|
services.zram-generator.enable = true;
|
|
|
|
|
|
|
|
services.zram-generator.settings = lib.listToAttrs
|
|
|
|
(builtins.map
|
|
|
|
(dev: {
|
|
|
|
name = dev;
|
|
|
|
value =
|
|
|
|
let
|
|
|
|
size = "${toString cfg.memoryPercent} / 100 * ram";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
zram-size = if cfg.memoryMax != null then "min(${size}, ${toString cfg.memoryMax} / 1024 / 1024)" else size;
|
|
|
|
compression-algorithm = cfg.algorithm;
|
|
|
|
swap-priority = cfg.priority;
|
|
|
|
} // lib.optionalAttrs (cfg.writebackDevice != null) {
|
|
|
|
writeback-device = cfg.writebackDevice;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
devices);
|
2014-04-25 01:56:36 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|