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