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
|
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
canonicalHandlers = {
|
|
|
|
powerEvent = {
|
2009-02-22 16:08:37 +00:00
|
|
|
event = "button/power.*";
|
2016-01-18 21:07:03 +00:00
|
|
|
action = config.services.acpid.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.*";
|
2016-01-18 21:07:03 +00:00
|
|
|
action = config.services.acpid.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.*";
|
2016-01-18 21:07:03 +00:00
|
|
|
action = config.services.acpid.acEventCommands;
|
2009-02-22 16:08:37 +00:00
|
|
|
};
|
2016-01-17 12:04:54 +00:00
|
|
|
};
|
|
|
|
|
2016-01-18 21:07:03 +00:00
|
|
|
acpiConfDir = pkgs.runCommand "acpi-events" {}
|
|
|
|
''
|
|
|
|
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
|
|
|
'';
|
|
|
|
in concatStringsSep "\n" (mapAttrsToList f (canonicalHandlers // config.services.acpid.handlers))
|
|
|
|
}
|
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
|
|
|
|
2009-09-29 10:52:25 +01:00
|
|
|
enable = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.bool;
|
2009-09-29 10:52:25 +01:00
|
|
|
default = false;
|
2009-11-15 12:48:42 +00:00
|
|
|
description = "Whether to enable the ACPI daemon.";
|
2009-09-29 10:52:25 +01:00
|
|
|
};
|
2010-04-08 16:27:20 +01:00
|
|
|
|
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
|
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
config = mkIf config.services.acpid.enable {
|
2009-03-06 12:25:25 +00:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
systemd.services.acpid = {
|
|
|
|
description = "ACPI Daemon";
|
2009-09-29 10:52:25 +01:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "systemd-udev-settle.service" ];
|
2009-09-29 10:52:25 +01:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
path = [ pkgs.acpid ];
|
2011-11-25 16:32:54 +00:00
|
|
|
|
2016-01-06 06:50:18 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
};
|
2012-10-05 04:26:01 +01:00
|
|
|
|
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
|
|
|
script = "acpid --confdir ${acpiConfDir}";
|
|
|
|
};
|
|
|
|
|
2009-03-06 12:25:25 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-02-22 16:08:22 +00:00
|
|
|
}
|