2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-02-22 17:08:22 +01:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2009-03-06 13:25:25 +01:00
|
|
|
|
2009-02-22 17:08:22 +01:00
|
|
|
let
|
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
canonicalHandlers = {
|
|
|
|
powerEvent = {
|
2009-02-22 17:08:37 +01:00
|
|
|
event = "button/power.*";
|
2016-01-18 22:07:03 +01:00
|
|
|
action = config.services.acpid.powerEventCommands;
|
2009-02-22 17:08:37 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
lidEvent = {
|
2009-02-22 17:08:37 +01:00
|
|
|
event = "button/lid.*";
|
2016-01-18 22:07:03 +01:00
|
|
|
action = config.services.acpid.lidEventCommands;
|
2009-02-22 17:08:37 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
acEvent = {
|
2009-02-22 17:08:37 +01:00
|
|
|
event = "ac_adapter.*";
|
2016-01-18 22:07:03 +01:00
|
|
|
action = config.services.acpid.acEventCommands;
|
2009-02-22 17:08:37 +01:00
|
|
|
};
|
2016-01-17 13:04:54 +01:00
|
|
|
};
|
|
|
|
|
2018-11-08 11:59:03 +01:00
|
|
|
acpiConfDir = pkgs.runCommand "acpi-events" { preferLocalBuild = true; }
|
2016-01-18 22:07:03 +01: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 14:26:05 +01:00
|
|
|
echo "action=${pkgs.writeShellScriptBin "${name}.sh" handler.action }/bin/${name}.sh '%e'" >> $fn
|
2016-01-18 22:07:03 +01:00
|
|
|
'';
|
|
|
|
in concatStringsSep "\n" (mapAttrsToList f (canonicalHandlers // config.services.acpid.handlers))
|
|
|
|
}
|
2016-01-17 13:04:54 +01:00
|
|
|
'';
|
|
|
|
|
2009-02-22 17:08:22 +01:00
|
|
|
in
|
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
{
|
2009-02-22 17:08:22 +01:00
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
###### interface
|
2009-02-22 17:08:22 +01:00
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-11-15 13:48:42 +01:00
|
|
|
services.acpid = {
|
2009-03-06 13:25:25 +01:00
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
enable = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.bool;
|
2009-09-29 11:52:25 +02:00
|
|
|
default = false;
|
2009-11-15 13:48:42 +01:00
|
|
|
description = "Whether to enable the ACPI daemon.";
|
2009-09-29 11:52:25 +02:00
|
|
|
};
|
2010-04-08 17:27:20 +02:00
|
|
|
|
2018-02-16 04:07:28 +01:00
|
|
|
logEvents = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Log all event activity.";
|
|
|
|
};
|
|
|
|
|
2016-01-18 22:07:03 +01: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 17:27:20 +02:00
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
action = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = "Shell commands to execute when the event is triggered.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2010-04-08 17:27:20 +02:00
|
|
|
|
2018-02-10 14:26:05 +01:00
|
|
|
description = ''
|
|
|
|
Event handlers.
|
2016-01-17 13:04:54 +01:00
|
|
|
|
2018-02-10 14:26:05 +01: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 13:04:54 +01:00
|
|
|
};
|
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
powerEventCommands = mkOption {
|
2016-01-17 13:04:54 +01:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2016-01-18 22:07:03 +01:00
|
|
|
description = "Shell commands to execute on a button/power.* event.";
|
2016-01-17 13:04:54 +01:00
|
|
|
};
|
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
lidEventCommands = mkOption {
|
2016-01-17 13:04:54 +01:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2016-01-18 22:07:03 +01:00
|
|
|
description = "Shell commands to execute on a button/lid.* event.";
|
2016-01-17 13:04:54 +01:00
|
|
|
};
|
|
|
|
|
2016-01-18 22:07:03 +01:00
|
|
|
acEventCommands = mkOption {
|
2016-01-17 13:04:54 +01:00
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2016-01-18 22:07:03 +01:00
|
|
|
description = "Shell commands to execute on an ac_adapter.* event.";
|
2016-01-17 13:04:54 +01:00
|
|
|
};
|
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-09-29 11:52:25 +02:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2009-11-15 13:48:42 +01:00
|
|
|
config = mkIf config.services.acpid.enable {
|
2009-03-06 13:25:25 +01:00
|
|
|
|
2016-01-06 07:50:18 +01:00
|
|
|
systemd.services.acpid = {
|
|
|
|
description = "ACPI Daemon";
|
2009-09-29 11:52:25 +02:00
|
|
|
|
2016-01-06 07:50:18 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "systemd-udev-settle.service" ];
|
2009-09-29 11:52:25 +02:00
|
|
|
|
2016-01-06 07:50:18 +01:00
|
|
|
path = [ pkgs.acpid ];
|
2011-11-25 17:32:54 +01:00
|
|
|
|
2016-01-06 07:50:18 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
};
|
2012-10-05 05:26:01 +02:00
|
|
|
|
2016-01-06 07:50:18 +01:00
|
|
|
unitConfig = {
|
|
|
|
ConditionVirtualization = "!systemd-nspawn";
|
|
|
|
ConditionPathExists = [ "/proc/acpi" ];
|
2009-09-29 11:52:25 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2018-02-16 04:07:28 +01:00
|
|
|
script = "acpid ${optionalString config.services.acpid.logEvents "--logevents"} --confdir ${acpiConfDir}";
|
2016-01-06 07:50:18 +01:00
|
|
|
};
|
|
|
|
|
2009-03-06 13:25:25 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-02-22 17:08:22 +01:00
|
|
|
}
|