1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.ananicy;
7 configFile = pkgs.writeText "ananicy.conf" (generators.toKeyValue { } cfg.settings);
8 extraRules = pkgs.writeText "extraRules" cfg.extraRules;
9 servicename = if ((lib.getName cfg.package) == (lib.getName pkgs.ananicy-cpp)) then "ananicy-cpp" else "ananicy";
10in
11{
12 options = {
13 services.ananicy = {
14 enable = mkEnableOption (lib.mdDoc "Ananicy, an auto nice daemon");
15
16 package = mkOption {
17 type = types.package;
18 default = pkgs.ananicy;
19 defaultText = literalExpression "pkgs.ananicy";
20 example = literalExpression "pkgs.ananicy-cpp";
21 description = lib.mdDoc ''
22 Which ananicy package to use.
23 '';
24 };
25
26 settings = mkOption {
27 type = with types; attrsOf (oneOf [ int bool str ]);
28 default = { };
29 example = {
30 apply_nice = false;
31 };
32 description = lib.mdDoc ''
33 See <https://github.com/Nefelim4ag/Ananicy/blob/master/ananicy.d/ananicy.conf>
34 '';
35 };
36
37 extraRules = mkOption {
38 type = types.str;
39 default = "";
40 description = lib.mdDoc ''
41 Extra rules in json format on separate lines. See:
42 <https://github.com/Nefelim4ag/Ananicy#configuration>
43 <https://gitlab.com/ananicy-cpp/ananicy-cpp/#global-configuration>
44 '';
45 example = literalExpression ''
46 '''
47 { "name": "eog", "type": "Image-View" }
48 { "name": "fdupes", "type": "BG_CPUIO" }
49 '''
50 '';
51
52 };
53 };
54 };
55
56 config = mkIf cfg.enable {
57 environment = {
58 systemPackages = [ cfg.package ];
59 etc."ananicy.d".source = pkgs.runCommandLocal "ananicyfiles" { } ''
60 mkdir -p $out
61 # ananicy-cpp does not include rules or settings on purpose
62 cp -r ${pkgs.ananicy}/etc/ananicy.d/* $out
63 rm $out/ananicy.conf
64 cp ${configFile} $out/ananicy.conf
65 ${optionalString (cfg.extraRules != "") "cp ${extraRules} $out/nixRules.rules"}
66 '';
67 };
68
69 # ananicy and ananicy-cpp have different default settings
70 services.ananicy.settings =
71 let
72 mkOD = mkOptionDefault;
73 in
74 {
75 cgroup_load = mkOD true;
76 type_load = mkOD true;
77 rule_load = mkOD true;
78 apply_nice = mkOD true;
79 apply_ioclass = mkOD true;
80 apply_ionice = mkOD true;
81 apply_sched = mkOD true;
82 apply_oom_score_adj = mkOD true;
83 apply_cgroup = mkOD true;
84 } // (if ((lib.getName cfg.package) == (lib.getName pkgs.ananicy-cpp)) then {
85 # https://gitlab.com/ananicy-cpp/ananicy-cpp/-/blob/master/src/config.cpp#L12
86 loglevel = mkOD "warn"; # default is info but its spammy
87 cgroup_realtime_workaround = mkOD config.systemd.enableUnifiedCgroupHierarchy;
88 } else {
89 # https://github.com/Nefelim4ag/Ananicy/blob/master/ananicy.d/ananicy.conf
90 check_disks_schedulers = mkOD true;
91 check_freq = mkOD 5;
92 });
93
94 systemd = {
95 # https://gitlab.com/ananicy-cpp/ananicy-cpp/#cgroups applies to both ananicy and -cpp
96 enableUnifiedCgroupHierarchy = mkDefault false;
97 packages = [ cfg.package ];
98 services."${servicename}" = {
99 wantedBy = [ "default.target" ];
100 };
101 };
102 };
103
104 meta = {
105 maintainers = with maintainers; [ artturin ];
106 };
107}