2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.triggerhappy;
|
|
|
|
|
|
|
|
socket = "/run/thd.socket";
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "triggerhappy.conf" ''
|
|
|
|
${concatMapStringsSep "\n"
|
|
|
|
({ keys, event, cmd, ... }:
|
|
|
|
''${concatMapStringsSep "+" (x: "KEY_" + x) keys} ${toString { press = 1; hold = 2; release = 0; }.${event}} ${cmd}''
|
|
|
|
)
|
|
|
|
cfg.bindings}
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
|
|
|
bindingCfg = { ... }: {
|
|
|
|
options = {
|
|
|
|
|
|
|
|
keys = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "List of keys to match. Key names as defined in linux/input-event-codes.h";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
event = mkOption {
|
|
|
|
type = types.enum ["press" "hold" "release"];
|
|
|
|
default = "press";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Event to match.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cmd = mkOption {
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "What to run.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.triggerhappy = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Whether to enable the {command}`triggerhappy` hotkey daemon.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "nobody";
|
|
|
|
example = "root";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
User account under which {command}`triggerhappy` runs.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
bindings = mkOption {
|
|
|
|
type = types.listOf (types.submodule bindingCfg);
|
|
|
|
default = [];
|
2021-10-06 13:57:05 +00:00
|
|
|
example = lib.literalExpression ''
|
2022-01-26 04:04:25 +00:00
|
|
|
[ { keys = ["PLAYPAUSE"]; cmd = "''${pkgs.mpc-cli}/bin/mpc -q toggle"; } ]
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Key bindings for {command}`triggerhappy`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Literal contents to append to the end of {command}`triggerhappy` configuration file.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
systemd.sockets.triggerhappy = {
|
|
|
|
description = "Triggerhappy Socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
socketConfig.ListenDatagram = socket;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.triggerhappy = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
description = "Global hotkey daemon";
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.triggerhappy}/bin/thd ${optionalString (cfg.user != "root") "--user ${cfg.user}"} --socket ${socket} --triggers ${configFile} --deviceglob /dev/input/event*";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.udev.packages = lib.singleton (pkgs.writeTextFile {
|
|
|
|
name = "triggerhappy-udev-rules";
|
|
|
|
destination = "/etc/udev/rules.d/61-triggerhappy.rules";
|
|
|
|
text = ''
|
|
|
|
ACTION=="add", SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}!="triggerhappy", \
|
|
|
|
RUN+="${pkgs.triggerhappy}/bin/th-cmd --socket ${socket} --passfd --udev"
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|