2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-02-22 16:08:22 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-03-06 12:25:25 +00:00
|
|
|
|
2009-02-22 16:08:22 +00:00
|
|
|
let
|
2021-02-27 15:18:32 +00:00
|
|
|
cfg = config.services.acpid;
|
2009-02-22 16:08:22 +00:00
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
canonicalHandlers = {
|
|
|
|
powerEvent = {
|
2009-02-22 16:08:37 +00:00
|
|
|
event = "button/power.*";
|
2021-02-27 15:18:32 +00:00
|
|
|
action = cfg.powerEventCommands;
|
2009-02-22 16:08:37 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
lidEvent = {
|
2009-02-22 16:08:37 +00:00
|
|
|
event = "button/lid.*";
|
2021-02-27 15:18:32 +00:00
|
|
|
action = cfg.lidEventCommands;
|
2009-02-22 16:08:37 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
acEvent = {
|
2009-02-22 16:08:37 +00:00
|
|
|
event = "ac_adapter.*";
|
2021-02-27 15:18:32 +00:00
|
|
|
action = cfg.acEventCommands;
|
2009-02-22 16:08:37 +00:00
|
|
|
};
|
2016-01-17 12:04:54 +00:00
|
|
|
};
|
|
|
|
|
2018-11-08 10:59:03 +00:00
|
|
|
acpiConfDir = pkgs.runCommand "acpi-events" { preferLocalBuild = true; }
|
2016-01-18 21:07:03 +00:00
|
|
|
''
|
|
|
|
mkdir -p $out
|
|
|
|
${
|
|
|
|
# Generate a configuration file for each event. (You can't have
|
|
|
|
# multiple events in one config file...)
|
|
|
|
let f = name: handler:
|
|
|
|
''
|
|
|
|
fn=$out/${name}
|
|
|
|
echo "event=${handler.event}" > $fn
|
2018-02-10 13:26:05 +00:00
|
|
|
echo "action=${pkgs.writeShellScriptBin "${name}.sh" handler.action }/bin/${name}.sh '%e'" >> $fn
|
2016-01-18 21:07:03 +00:00
|
|
|
'';
|
2021-02-27 15:18:32 +00:00
|
|
|
in concatStringsSep "\n" (mapAttrsToList f (canonicalHandlers // cfg.handlers))
|
2016-01-18 21:07:03 +00:00
|
|
|
}
|
2016-01-17 12:04:54 +00:00
|
|
|
'';
|
|
|
|
|
2009-02-22 16:08:22 +00:00
|
|
|
in
|
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
{
|
2009-02-22 16:08:22 +00:00
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
###### interface
|
2009-02-22 16:08:22 +00:00
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
services.acpid = {
|
2009-03-06 12:25:25 +00:00
|
|
|
|
2021-02-27 15:18:32 +00:00
|
|
|
enable = mkEnableOption "the ACPI daemon";
|
2010-04-08 16:27:20 +01:00
|
|
|
|
2018-02-16 03:07:28 +00:00
|
|
|
logEvents = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Log all event activity.";
|
|
|
|
};
|
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
handlers = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
event = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = [ "button/power.*" "button/lid.*" "ac_adapter.*" "button/mute.*" "button/volumedown.*" "cd/play.*" "cd/next.*" ];
|
|
|
|
description = "Event type.";
|
|
|
|
};
|
2010-04-08 16:27:20 +01:00
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
action = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = "Shell commands to execute when the event is triggered.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2010-04-08 16:27:20 +01:00
|
|
|
|
2018-02-10 13:26:05 +00:00
|
|
|
description = ''
|
|
|
|
Event handlers.
|
2016-01-17 12:04:54 +00:00
|
|
|
|
2018-02-10 13:26:05 +00:00
|
|
|
<note><para>
|
|
|
|
Handler can be a single command.
|
|
|
|
</para></note>
|
|
|
|
'';
|
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
ac-power = {
|
|
|
|
event = "ac_adapter/*";
|
|
|
|
action = ''
|
|
|
|
vals=($1) # space separated string to array of multiple values
|
|
|
|
case ''${vals[3]} in
|
|
|
|
00000000)
|
|
|
|
echo unplugged >> /tmp/acpi.log
|
|
|
|
;;
|
|
|
|
00000001)
|
|
|
|
echo plugged in >> /tmp/acpi.log
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo unknown >> /tmp/acpi.log
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2016-01-17 12:04:54 +00:00
|
|
|
};
|
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
powerEventCommands = mkOption {
|
2016-01-17 12:04:54 +00:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2016-01-18 21:07:03 +00:00
|
|
|
description = "Shell commands to execute on a button/power.* event.";
|
2016-01-17 12:04:54 +00:00
|
|
|
};
|
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
lidEventCommands = mkOption {
|
2016-01-17 12:04:54 +00:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2016-01-18 21:07:03 +00:00
|
|
|
description = "Shell commands to execute on a button/lid.* event.";
|
2016-01-17 12:04:54 +00:00
|
|
|
};
|
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
acEventCommands = mkOption {
|
2016-01-17 12:04:54 +00:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2016-01-18 21:07:03 +00:00
|
|
|
description = "Shell commands to execute on an ac_adapter.* event.";
|
2016-01-17 12:04:54 +00:00
|
|
|
};
|
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2021-02-27 15:18:32 +00:00
|
|
|
config = mkIf cfg.enable {
|
2009-03-06 12:25:25 +00:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
systemd.services.acpid = {
|
|
|
|
description = "ACPI Daemon";
|
2021-02-27 15:18:32 +00:00
|
|
|
documentation = [ "man:acpid(8)" ];
|
2009-09-29 10:52:25 +01:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2011-11-25 16:32:54 +00:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
serviceConfig = {
|
2021-02-27 15:18:32 +00:00
|
|
|
ExecStart = escapeShellArgs
|
|
|
|
([ "${pkgs.acpid}/bin/acpid"
|
|
|
|
"--foreground"
|
|
|
|
"--netlink"
|
|
|
|
"--confdir" "${acpiConfDir}"
|
|
|
|
] ++ optional cfg.logEvents "--logevents"
|
|
|
|
);
|
2016-01-06 06:50:18 +00:00
|
|
|
};
|
|
|
|
unitConfig = {
|
|
|
|
ConditionVirtualization = "!systemd-nspawn";
|
|
|
|
ConditionPathExists = [ "/proc/acpi" ];
|
2009-09-29 10:52:25 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
};
|
|
|
|
|
2009-03-06 12:25:25 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-02-22 16:08:22 +00:00
|
|
|
}
|