aboutsummaryrefslogtreecommitdiff
path: root/modules/services/xsuspender.nix
blob: 2eb40f5dd340e61a889e50cf9ee6b723f2af328c (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.xsuspender;

  xsuspenderOptions = types.submodule {
    options = {
      matchWmClassContains = mkOption {
        description = "Match windows that wm class contains string.";
        type = types.nullOr types.str;
        default = null;
      };

      matchWmClassGroupContains = mkOption {
        description = "Match windows where wm class group contains string.";
        type = types.nullOr types.str;
        default = null;
      };

      matchWmNameContains = mkOption {
        description = "Match windows where wm name contains string.";
        type = types.nullOr types.str;
        default = null;
      };

      suspendDelay = mkOption {
        description = "Initial suspend delay in seconds.";
        type = types.int;
        default = 5;
      };

      resumeEvery = mkOption {
        description = "Resume interval in seconds.";
        type = types.int;
        default = 50;
      };

      resumeFor = mkOption {
        description = "Resume duration in seconds.";
        type = types.int;
        default = 5;
      };

      execSuspend = mkOption {
        description = ''
          Before suspending, execute this shell script. If it fails,
          abort suspension.
        '';
        type = types.nullOr types.str;
        default = null;
        example = ''echo "suspending window $XID of process $PID"'';
      };

      execResume = mkOption {
        description = ''
          Before resuming, execute this shell script. Resume the
          process regardless script failure.
        '';
        type = types.nullOr types.str;
        default = null;
        example = "echo resuming ...";
      };

      sendSignals = mkOption {
        description = ''
          Whether to send SIGSTOP / SIGCONT signals or not.
          If false just the exec scripts are run.
        '';
        type = types.bool;
        default = true;
      };

      suspendSubtreePattern = mkOption {
        description =
          "Also suspend descendant processes that match this regex.";
        type = types.nullOr types.str;
        default = null;
      };

      onlyOnBattery = mkOption {
        description = "Whether to enable process suspend only on battery.";
        type = types.bool;
        default = false;
      };

      autoSuspendOnBattery = mkOption {
        description = ''
          Whether to auto-apply rules when switching to battery
          power even if the window(s) didn't just lose focus.
        '';
        type = types.bool;
        default = true;
      };

      downclockOnBattery = mkOption {
        description = ''
          Limit CPU consumption for this factor when on battery power.
          Value 1 means 50% decrease, 2 means 66%, 3 means 75% etc.
        '';
        type = types.int;
        default = 0;
      };
    };
  };

in {
  meta.maintainers = [ maintainers.offline ];

  options = {
    services.xsuspender = {
      enable = mkEnableOption "XSuspender";

      defaults = mkOption {
        description = "XSuspender defaults.";
        type = xsuspenderOptions;
        default = { };
      };

      rules = mkOption {
        description = "Attribute set of XSuspender rules.";
        type = types.attrsOf xsuspenderOptions;
        default = { };
        example = {
          Chromium = {
            suspendDelay = 10;
            matchWmClassContains = "chromium-browser";
            suspendSubtreePattern = "chromium";
          };
        };
      };

      debug = mkOption {
        description = "Whether to enable debug output.";
        type = types.bool;
        default = false;
      };

      iniContent = mkOption {
        type = types.attrsOf types.attrs;
        internal = true;
      };
    };
  };

  config = mkIf cfg.enable {
    services.xsuspender.iniContent = let
      mkSection = values:
        filterAttrs (_: v: v != null) {
          match_wm_class_contains = values.matchWmClassContains;
          match_wm_class_group_contains = values.matchWmClassGroupContains;
          match_wm_name_contains = values.matchWmNameContains;
          suspend_delay = values.suspendDelay;
          resume_every = values.resumeEvery;
          resume_for = values.resumeFor;
          exec_suspend = values.execSuspend;
          exec_resume = values.execResume;
          send_signals = values.sendSignals;
          suspend_subtree_pattern = values.suspendSubtreePattern;
          only_on_battery = values.onlyOnBattery;
          auto_suspend_on_battery = values.autoSuspendOnBattery;
          downclock_on_battery = values.downclockOnBattery;
        };
    in {
      Default = mkSection cfg.defaults;
    } // mapAttrs (_: mkSection) cfg.rules;

    # To make the xsuspender tool available.
    home.packages = [ pkgs.xsuspender ];

    xdg.configFile."xsuspender.conf".text = generators.toINI { } cfg.iniContent;

    systemd.user.services.xsuspender = {
      Unit = {
        Description = "XSuspender";
        After = [ "graphical-session-pre.target" ];
        PartOf = [ "graphical-session.target" ];
        X-Restart-Triggers =
          [ "${config.xdg.configFile."xsuspender.conf".source}" ];
      };

      Service = {
        ExecStart = "${pkgs.xsuspender}/bin/xsuspender";
        Environment = mkIf cfg.debug [ "G_MESSAGE_DEBUG=all" ];
      };

      Install = { WantedBy = [ "graphical-session.target" ]; };
    };
  };
}