1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 acpiConfDir = pkgs.runCommand "acpi-events" {}
8 ''
9 mkdir -p $out
10 ${
11 # Generate a configuration file for each event. (You can't have
12 # multiple events in one config file...)
13 let f = event:
14 ''
15 fn=$out/${event.name}
16 echo "event=${event.event}" > $fn
17 echo "action=${pkgs.writeScript "${event.name}.sh" event.action}" >> $fn
18 '';
19 in lib.concatMapStrings f events
20 }
21 '';
22
23 events = [powerEvent lidEvent acEvent];
24
25 # Called when the power button is pressed.
26 powerEvent =
27 { name = "power-button";
28 event = "button/power.*";
29 action =
30 ''
31 #! ${pkgs.bash}/bin/sh
32 ${config.services.acpid.powerEventCommands}
33 '';
34 };
35
36 # Called when the laptop lid is opened/closed.
37 lidEvent =
38 { name = "lid";
39 event = "button/lid.*";
40 action =
41 ''
42 #! ${pkgs.bash}/bin/sh
43 ${config.services.acpid.lidEventCommands}
44 '';
45 };
46
47 # Called when the AC power is connected or disconnected.
48 acEvent =
49 { name = "ac-power";
50 event = "ac_adapter.*";
51 action =
52 ''
53 #! ${pkgs.bash}/bin/sh
54 ${config.services.acpid.acEventCommands}
55 '';
56 };
57
58in
59
60{
61
62 ###### interface
63
64 options = {
65
66 services.acpid = {
67
68 enable = mkOption {
69 type = types.bool;
70 default = false;
71 description = "Whether to enable the ACPI daemon.";
72 };
73
74 powerEventCommands = mkOption {
75 type = types.lines;
76 default = "";
77 description = "Shell commands to execute on a button/power.* event.";
78 };
79
80 lidEventCommands = mkOption {
81 type = types.lines;
82 default = "";
83 description = "Shell commands to execute on a button/lid.* event.";
84 };
85
86 acEventCommands = mkOption {
87 type = types.lines;
88 default = "";
89 description = "Shell commands to execute on an ac_adapter.* event.";
90 };
91
92 };
93
94 };
95
96
97 ###### implementation
98
99 config = mkIf config.services.acpid.enable {
100
101 jobs.acpid =
102 { description = "ACPI Daemon";
103
104 wantedBy = [ "multi-user.target" ];
105 after = [ "systemd-udev-settle.service" ];
106
107 path = [ pkgs.acpid ];
108
109 daemonType = "fork";
110
111 exec = "acpid --confdir ${acpiConfDir}";
112
113 unitConfig.ConditionVirtualization = "!systemd-nspawn";
114 unitConfig.ConditionPathExists = [ "/proc/acpi" ];
115 };
116
117 };
118
119}