fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Kylie McClainxsuspender: fix typo that made debug option a noop (#3653)6d95d98b

master
5.5 KiB200 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.xsuspender;
8
9  iniFormat = pkgs.formats.ini { };
10
11  xsuspenderOptions = types.submodule {
12    options = {
13      matchWmClassContains = mkOption {
14        description = "Match windows that wm class contains string.";
15        type = types.nullOr types.str;
16        default = null;
17      };
18
19      matchWmClassGroupContains = mkOption {
20        description = "Match windows where wm class group contains string.";
21        type = types.nullOr types.str;
22        default = null;
23      };
24
25      matchWmNameContains = mkOption {
26        description = "Match windows where wm name contains string.";
27        type = types.nullOr types.str;
28        default = null;
29      };
30
31      suspendDelay = mkOption {
32        description = "Initial suspend delay in seconds.";
33        type = types.int;
34        default = 5;
35      };
36
37      resumeEvery = mkOption {
38        description = "Resume interval in seconds.";
39        type = types.int;
40        default = 50;
41      };
42
43      resumeFor = mkOption {
44        description = "Resume duration in seconds.";
45        type = types.int;
46        default = 5;
47      };
48
49      execSuspend = mkOption {
50        description = ''
51          Before suspending, execute this shell script. If it fails,
52          abort suspension.
53        '';
54        type = types.nullOr types.str;
55        default = null;
56        example = ''echo "suspending window $XID of process $PID"'';
57      };
58
59      execResume = mkOption {
60        description = ''
61          Before resuming, execute this shell script. Resume the
62          process regardless script failure.
63        '';
64        type = types.nullOr types.str;
65        default = null;
66        example = "echo resuming ...";
67      };
68
69      sendSignals = mkOption {
70        description = ''
71          Whether to send SIGSTOP / SIGCONT signals or not.
72          If false just the exec scripts are run.
73        '';
74        type = types.bool;
75        default = true;
76      };
77
78      suspendSubtreePattern = mkOption {
79        description =
80          "Also suspend descendant processes that match this regex.";
81        type = types.nullOr types.str;
82        default = null;
83      };
84
85      onlyOnBattery = mkOption {
86        description = "Whether to enable process suspend only on battery.";
87        type = types.bool;
88        default = false;
89      };
90
91      autoSuspendOnBattery = mkOption {
92        description = ''
93          Whether to auto-apply rules when switching to battery
94          power even if the window(s) didn't just lose focus.
95        '';
96        type = types.bool;
97        default = true;
98      };
99
100      downclockOnBattery = mkOption {
101        description = ''
102          Limit CPU consumption for this factor when on battery power.
103          Value 1 means 50% decrease, 2 means 66%, 3 means 75% etc.
104        '';
105        type = types.int;
106        default = 0;
107      };
108    };
109  };
110
111in {
112  meta.maintainers = [ maintainers.offline ];
113
114  options = {
115    services.xsuspender = {
116      enable = mkEnableOption "XSuspender";
117
118      defaults = mkOption {
119        description = "XSuspender defaults.";
120        type = xsuspenderOptions;
121        default = { };
122      };
123
124      rules = mkOption {
125        description = "Attribute set of XSuspender rules.";
126        type = types.attrsOf xsuspenderOptions;
127        default = { };
128        example = {
129          Chromium = {
130            suspendDelay = 10;
131            matchWmClassContains = "chromium-browser";
132            suspendSubtreePattern = "chromium";
133          };
134        };
135      };
136
137      debug = mkOption {
138        description = "Whether to enable debug output.";
139        type = types.bool;
140        default = false;
141      };
142
143      iniContent = mkOption {
144        type = iniFormat.type;
145        internal = true;
146      };
147    };
148  };
149
150  config = mkIf cfg.enable {
151    assertions = [
152      (lib.hm.assertions.assertPlatform "services.xsuspender" pkgs
153        lib.platforms.linux)
154    ];
155
156    services.xsuspender.iniContent = let
157      mkSection = values:
158        filterAttrs (_: v: v != null) {
159          match_wm_class_contains = values.matchWmClassContains;
160          match_wm_class_group_contains = values.matchWmClassGroupContains;
161          match_wm_name_contains = values.matchWmNameContains;
162          suspend_delay = values.suspendDelay;
163          resume_every = values.resumeEvery;
164          resume_for = values.resumeFor;
165          exec_suspend = values.execSuspend;
166          exec_resume = values.execResume;
167          send_signals = values.sendSignals;
168          suspend_subtree_pattern = values.suspendSubtreePattern;
169          only_on_battery = values.onlyOnBattery;
170          auto_suspend_on_battery = values.autoSuspendOnBattery;
171          downclock_on_battery = values.downclockOnBattery;
172        };
173    in {
174      Default = mkSection cfg.defaults;
175    } // mapAttrs (_: mkSection) cfg.rules;
176
177    # To make the xsuspender tool available.
178    home.packages = [ pkgs.xsuspender ];
179
180    xdg.configFile."xsuspender.conf".source =
181      iniFormat.generate "xsuspender.conf" cfg.iniContent;
182
183    systemd.user.services.xsuspender = {
184      Unit = {
185        Description = "XSuspender";
186        After = [ "graphical-session-pre.target" ];
187        PartOf = [ "graphical-session.target" ];
188        X-Restart-Triggers =
189          [ "${config.xdg.configFile."xsuspender.conf".source}" ];
190      };
191
192      Service = {
193        ExecStart = "${pkgs.xsuspender}/bin/xsuspender";
194        Environment = mkIf cfg.debug [ "G_MESSAGES_DEBUG=all" ];
195      };
196
197      Install = { WantedBy = [ "graphical-session.target" ]; };
198    };
199  };
200}