summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/misc/ananicy.nix
blob: 38f16d99bb4bbe416c4239fb837a483a86793be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.ananicy;
  configFile = pkgs.writeText "ananicy.conf" (lib.generators.toKeyValue { } cfg.settings);
  extraRules = pkgs.writeText "extraRules" (
    lib.concatMapStringsSep "\n" (l: builtins.toJSON l) cfg.extraRules
  );
  extraTypes = pkgs.writeText "extraTypes" (
    lib.concatMapStringsSep "\n" (l: builtins.toJSON l) cfg.extraTypes
  );
  extraCgroups = pkgs.writeText "extraCgroups" (
    lib.concatMapStringsSep "\n" (l: builtins.toJSON l) cfg.extraCgroups
  );
  servicename =
    if ((lib.getName cfg.package) == (lib.getName pkgs.ananicy-cpp)) then "ananicy-cpp" else "ananicy";
in
{
  options.services.ananicy = {
    enable = lib.mkEnableOption "Ananicy, an auto nice daemon";

    package = lib.mkPackageOption pkgs "ananicy" { example = "ananicy-cpp"; };

    rulesProvider = lib.mkPackageOption pkgs "ananicy" { example = "ananicy-cpp"; } // {
      description = ''
        Which package to copy default rules,types,cgroups from.
      '';
    };

    settings = lib.mkOption {
      type =
        with lib.types;
        attrsOf (oneOf [
          int
          bool
          str
        ]);
      default = { };
      example = {
        apply_nice = false;
      };
      description = ''
        See <https://github.com/Nefelim4ag/Ananicy/blob/master/ananicy.d/ananicy.conf>
      '';
    };

    extraRules = lib.mkOption {
      type = with lib.types; listOf attrs;
      default = [ ];
      description = ''
        Rules to write in 'nixRules.rules'. See:
        <https://github.com/Nefelim4ag/Ananicy#configuration>
        <https://gitlab.com/ananicy-cpp/ananicy-cpp/#global-configuration>
      '';
      example = [
        {
          name = "eog";
          type = "Image-Viewer";
        }
        {
          name = "fdupes";
          type = "BG_CPUIO";
        }
      ];
    };
    extraTypes = lib.mkOption {
      type = with lib.types; listOf attrs;
      default = [ ];
      description = ''
        Types to write in 'nixTypes.types'. See:
        <https://gitlab.com/ananicy-cpp/ananicy-cpp/#types>
      '';
      example = [
        {
          type = "my_type";
          nice = 19;
          other_parameter = "value";
        }
        {
          type = "compiler";
          nice = 19;
          sched = "batch";
          ioclass = "idle";
        }
      ];
    };
    extraCgroups = lib.mkOption {
      type = with lib.types; listOf attrs;
      default = [ ];
      description = ''
        Cgroups to write in 'nixCgroups.cgroups'. See:
        <https://gitlab.com/ananicy-cpp/ananicy-cpp/#cgroups>
      '';
      example = [
        {
          cgroup = "cpu80";
          CPUQuota = 80;
        }
      ];
    };
  };

  config = lib.mkIf cfg.enable {
    environment = {
      systemPackages = [ cfg.package ];
      etc."ananicy.d".source =
        pkgs.runCommand "ananicyfiles"
          {
            preferLocalBuild = true;
          }
          ''
            mkdir -p $out
            # ananicy-cpp does not include rules or settings on purpose
            if [[ -d "${cfg.rulesProvider}/etc/ananicy.d/00-default" ]]; then
              cp -r ${cfg.rulesProvider}/etc/ananicy.d/* $out
            else
              cp -r ${cfg.rulesProvider}/* $out
            fi

            # configured through .setings
            rm -f $out/ananicy.conf
            cp ${configFile} $out/ananicy.conf
            ${lib.optionalString (cfg.extraRules != [ ]) "cp ${extraRules} $out/nixRules.rules"}
            ${lib.optionalString (cfg.extraTypes != [ ]) "cp ${extraTypes} $out/nixTypes.types"}
            ${lib.optionalString (cfg.extraCgroups != [ ]) "cp ${extraCgroups} $out/nixCgroups.cgroups"}
          '';
    };

    # ananicy and ananicy-cpp have different default settings
    services.ananicy.settings =
      let
        mkOD = lib.mkOptionDefault;
      in
      {
        cgroup_load = mkOD true;
        type_load = mkOD true;
        rule_load = mkOD true;
        apply_nice = mkOD true;
        apply_ioclass = mkOD true;
        apply_ionice = mkOD true;
        apply_sched = mkOD true;
        apply_oom_score_adj = mkOD true;
        apply_cgroup = mkOD true;
      }
      // (
        if servicename == "ananicy-cpp" then
          {
            # https://gitlab.com/ananicy-cpp/ananicy-cpp/-/blob/master/src/config.cpp#L12
            loglevel = mkOD "warn"; # default is info but its spammy
            cgroup_realtime_workaround = true;
            log_applied_rule = mkOD false;
          }
        else
          {
            # https://github.com/Nefelim4ag/Ananicy/blob/master/ananicy.d/ananicy.conf
            check_disks_schedulers = mkOD true;
            check_freq = mkOD 5;
          }
      );

    systemd = {
      packages = [ cfg.package ];
      services."${servicename}" = {
        wantedBy = [ "default.target" ];
      };
    };
  };

  meta.maintainers = with lib.maintainers; [ artturin ];
}