1# Upower daemon.
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
8
9 cfg = config.services.upower;
10
11in
12
13{
14
15 ###### interface
16
17 options = {
18
19 services.upower = {
20
21 enable = mkOption {
22 type = types.bool;
23 default = false;
24 description = ''
25 Whether to enable Upower, a DBus service that provides power
26 management support to applications.
27 '';
28 };
29
30 package = mkPackageOption pkgs "upower" { };
31
32 enableWattsUpPro = mkOption {
33 type = types.bool;
34 default = false;
35 description = ''
36 Enable the Watts Up Pro device.
37
38 The Watts Up Pro contains a generic FTDI USB device without a specific
39 vendor and product ID. When we probe for WUP devices, we can cause
40 the user to get a perplexing "Device or resource busy" error when
41 attempting to use their non-WUP device.
42
43 The generic FTDI device is known to also be used on:
44
45 - Sparkfun FT232 breakout board
46 - Parallax Propeller
47 '';
48 };
49
50 noPollBatteries = mkOption {
51 type = types.bool;
52 default = false;
53 description = ''
54 Don't poll the kernel for battery level changes.
55
56 Some hardware will send us battery level changes through
57 events, rather than us having to poll for it. This option
58 allows disabling polling for hardware that sends out events.
59 '';
60 };
61
62 ignoreLid = mkOption {
63 type = types.bool;
64 default = false;
65 description = ''
66 Do we ignore the lid state
67
68 Some laptops are broken. The lid state is either inverted, or stuck
69 on or off. We can't do much to fix these problems, but this is a way
70 for users to make the laptop panel vanish, a state that might be used
71 by a couple of user-space daemons. On Linux systems, see also
72 logind.conf(5).
73 '';
74 };
75
76 usePercentageForPolicy = mkOption {
77 type = types.bool;
78 default = true;
79 description = ''
80 Policy for warnings and action based on battery levels
81
82 Whether battery percentage based policy should be used. The default
83 is to use the percentage, which
84 should work around broken firmwares. It is also more reliable than
85 the time left (frantically saving all your files is going to use more
86 battery than letting it rest for example).
87 '';
88 };
89
90 percentageLow = mkOption {
91 type = types.ints.unsigned;
92 default = 10;
93 description = ''
94 When `usePercentageForPolicy` is
95 `true`, the levels at which UPower will consider the
96 battery low.
97
98 This will also be used for batteries which don't have time information
99 such as that of peripherals.
100
101 If any value (of `percentageLow`,
102 `percentageCritical` and
103 `percentageAction`) is invalid, or not in descending
104 order, the defaults will be used.
105 '';
106 };
107
108 percentageCritical = mkOption {
109 type = types.ints.unsigned;
110 default = 3;
111 description = ''
112 When `usePercentageForPolicy` is
113 `true`, the levels at which UPower will consider the
114 battery critical.
115
116 This will also be used for batteries which don't have time information
117 such as that of peripherals.
118
119 If any value (of `percentageLow`,
120 `percentageCritical` and
121 `percentageAction`) is invalid, or not in descending
122 order, the defaults will be used.
123 '';
124 };
125
126 percentageAction = mkOption {
127 type = types.ints.unsigned;
128 default = 2;
129 description = ''
130 When `usePercentageForPolicy` is
131 `true`, the levels at which UPower will take action
132 for the critical battery level.
133
134 This will also be used for batteries which don't have time information
135 such as that of peripherals.
136
137 If any value (of `percentageLow`,
138 `percentageCritical` and
139 `percentageAction`) is invalid, or not in descending
140 order, the defaults will be used.
141 '';
142 };
143
144 timeLow = mkOption {
145 type = types.ints.unsigned;
146 default = 1200;
147 description = ''
148 When `usePercentageForPolicy` is
149 `false`, the time remaining in seconds at which
150 UPower will consider the battery low.
151
152 If any value (of `timeLow`,
153 `timeCritical` and `timeAction`) is
154 invalid, or not in descending order, the defaults will be used.
155 '';
156 };
157
158 timeCritical = mkOption {
159 type = types.ints.unsigned;
160 default = 300;
161 description = ''
162 When `usePercentageForPolicy` is
163 `false`, the time remaining in seconds at which
164 UPower will consider the battery critical.
165
166 If any value (of `timeLow`,
167 `timeCritical` and `timeAction`) is
168 invalid, or not in descending order, the defaults will be used.
169 '';
170 };
171
172 timeAction = mkOption {
173 type = types.ints.unsigned;
174 default = 120;
175 description = ''
176 When `usePercentageForPolicy` is
177 `false`, the time remaining in seconds at which
178 UPower will take action for the critical battery level.
179
180 If any value (of `timeLow`,
181 `timeCritical` and `timeAction`) is
182 invalid, or not in descending order, the defaults will be used.
183 '';
184 };
185
186 criticalPowerAction = mkOption {
187 type = types.enum [ "PowerOff" "Hibernate" "HybridSleep" ];
188 default = "HybridSleep";
189 description = ''
190 The action to take when `timeAction` or
191 `percentageAction` has been reached for the batteries
192 (UPS or laptop batteries) supplying the computer
193 '';
194 };
195
196 };
197
198 };
199
200
201 ###### implementation
202
203 config = mkIf cfg.enable {
204
205 environment.systemPackages = [ cfg.package ];
206
207 services.dbus.packages = [ cfg.package ];
208
209 services.udev.packages = [ cfg.package ];
210
211 systemd.packages = [ cfg.package ];
212
213 environment.etc."UPower/UPower.conf".text = generators.toINI {} {
214 UPower = {
215 EnableWattsUpPro = cfg.enableWattsUpPro;
216 NoPollBatteries = cfg.noPollBatteries;
217 IgnoreLid = cfg.ignoreLid;
218 UsePercentageForPolicy = cfg.usePercentageForPolicy;
219 PercentageLow = cfg.percentageLow;
220 PercentageCritical = cfg.percentageCritical;
221 PercentageAction = cfg.percentageAction;
222 TimeLow = cfg.timeLow;
223 TimeCritical = cfg.timeCritical;
224 TimeAction = cfg.timeAction;
225 CriticalPowerAction = cfg.criticalPowerAction;
226 };
227 };
228 };
229
230}