1 { config, lib, pkgs, ... }:
3 cfg = config.services.acpid;
7 event = "button/power.*";
8 action = cfg.powerEventCommands;
12 event = "button/lid.*";
13 action = cfg.lidEventCommands;
17 event = "ac_adapter.*";
18 action = cfg.acEventCommands;
22 acpiConfDir = pkgs.runCommand "acpi-events" { preferLocalBuild = true; }
26 # Generate a configuration file for each event. (You can't have
27 # multiple events in one config file...)
28 let f = name: handler:
31 echo "event=${handler.event}" > $fn
32 echo "action=${pkgs.writeShellScriptBin "${name}.sh" handler.action }/bin/${name}.sh '%e'" >> $fn
34 in lib.concatStringsSep "\n" (lib.mapAttrsToList f (canonicalHandlers // cfg.handlers))
48 enable = lib.mkEnableOption "the ACPI daemon";
50 logEvents = lib.mkOption {
51 type = lib.types.bool;
53 description = "Log all event activity.";
56 handlers = lib.mkOption {
57 type = lib.types.attrsOf (lib.types.submodule {
59 event = lib.mkOption {
61 example = lib.literalExpression ''"button/power.*" "button/lid.*" "ac_adapter.*" "button/mute.*" "button/volumedown.*" "cd/play.*" "cd/next.*"'';
62 description = "Event type.";
65 action = lib.mkOption {
66 type = lib.types.lines;
67 description = "Shell commands to execute when the event is triggered.";
76 Handler can be a single command.
82 event = "ac_adapter/*";
84 vals=($1) # space separated string to array of multiple values
87 echo unplugged >> /tmp/acpi.log
90 echo plugged in >> /tmp/acpi.log
93 echo unknown >> /tmp/acpi.log
101 powerEventCommands = lib.mkOption {
102 type = lib.types.lines;
104 description = "Shell commands to execute on a button/power.* event.";
107 lidEventCommands = lib.mkOption {
108 type = lib.types.lines;
110 description = "Shell commands to execute on a button/lid.* event.";
113 acEventCommands = lib.mkOption {
114 type = lib.types.lines;
116 description = "Shell commands to execute on an ac_adapter.* event.";
124 ###### implementation
126 config = lib.mkIf cfg.enable {
128 systemd.services.acpid = {
129 description = "ACPI Daemon";
130 documentation = [ "man:acpid(8)" ];
132 wantedBy = [ "multi-user.target" ];
135 ExecStart = lib.escapeShellArgs
136 ([ "${pkgs.acpid}/bin/acpid"
139 "--confdir" "${acpiConfDir}"
140 ] ++ lib.optional cfg.logEvents "--logevents"
144 ConditionVirtualization = "!systemd-nspawn";
145 ConditionPathExists = [ "/proc/acpi" ];