base16-schemes: unstable-2024-06-21 -> unstable-2024-11-12
[NixPkgs.git] / nixos / modules / services / system / swapspace.nix
blob9dfea492cd672ce868fb414014a5b3871e67ef91
2   config,
3   lib,
4   pkgs,
5   utils,
6   ...
7 }:
8 let
9   cfg = config.services.swapspace;
10   inherit (lib)
11     types
12     mkOption
13     mkPackageOption
14     mkEnableOption
15     ;
16   configFile = pkgs.writeText "swapspace.conf" (lib.generators.toKeyValue { } cfg.settings);
19   options.services.swapspace = {
20     enable = mkEnableOption "Swapspace, a dynamic swap space manager";
21     package = mkPackageOption pkgs "swapspace" { };
22     extraArgs = mkOption {
23       type = types.listOf types.str;
24       default = [ ];
25       example = [
26         "-P"
27         "-v"
28       ];
29       description = "Any extra arguments to pass to swapspace";
30     };
31     settings = mkOption {
32       type = types.submodule {
33         options = {
34           swappath = mkOption {
35             type = types.str;
36             default = "/var/lib/swapspace";
37             description = "Location where swapspace may create and delete swapfiles";
38           };
39           lower_freelimit = mkOption {
40             type = types.ints.between 0 99;
41             default = 20;
42             description = "Lower free-space threshold: if the percentage of free space drops below this number, additional swapspace is allocated";
43           };
44           upper_freelimit = mkOption {
45             type = types.ints.between 0 100;
46             default = 60;
47             description = "Upper free-space threshold: if the percentage of free space exceeds this number, swapspace will attempt to free up swapspace";
48           };
49           freetarget = mkOption {
50             type = types.ints.between 2 99;
51             default = 30;
52             description = ''
53               Percentage of free space swapspace should aim for when adding swapspace.
54               This should fall somewhere between lower_freelimit and upper_freelimit.
55             '';
56           };
57           min_swapsize = mkOption {
58             type = types.str;
59             default = "4m";
60             description = "Smallest allowed size for individual swapfiles";
61           };
62           max_swapsize = mkOption {
63             type = types.str;
64             default = "2t";
65             description = "Greatest allowed size for individual swapfiles";
66           };
67           cooldown = mkOption {
68             type = types.ints.unsigned;
69             default = 600;
70             description = ''
71               Duration (roughly in seconds) of the moratorium on swap allocation that is instated if disk space runs out, or the cooldown time after a new swapfile is successfully allocated before swapspace will consider deallocating swap space again.
72               The default cooldown period is about 10 minutes.
73             '';
74           };
75           buffer_elasticity = mkOption {
76             type = types.ints.between 0 100;
77             default = 30;
78             description = ''Percentage of buffer space considered to be "free"'';
79           };
80           cache_elasticity = mkOption {
81             type = types.ints.between 0 100;
82             default = 80;
83             description = ''Percentage of cache space considered to be "free"'';
84           };
85         };
86       };
87       default = { };
88       description = ''
89         Config file for swapspace.
90         See the options here: <https://github.com/Tookmund/Swapspace/blob/master/swapspace.conf>
91       '';
92     };
93   };
95   config = lib.mkIf cfg.enable {
96     environment.systemPackages = [ cfg.package ];
97     systemd.packages = [ cfg.package ];
98     systemd.services.swapspace = {
99       wantedBy = [ "multi-user.target" ];
100       serviceConfig = {
101         ExecStart = [
102           ""
103           "${lib.getExe cfg.package} -c ${configFile} ${utils.escapeSystemdExecArgs cfg.extraArgs}"
104         ];
105       };
106     };
107     systemd.tmpfiles.settings.swapspace = {
108       ${cfg.settings.swappath}.d = {
109         mode = "0700";
110       };
111     };
112   };
114   meta = {
115     maintainers = with lib.maintainers; [
116       Luflosi
117       phanirithvij
118     ];
119   };