1 { config, lib, pkgs, ... }:
4 cfg = config.services.triggerhappy;
6 socket = "/run/thd.socket";
8 configFile = pkgs.writeText "triggerhappy.conf" ''
9 ${lib.concatMapStringsSep "\n"
10 ({ keys, event, cmd, ... }:
11 ''${lib.concatMapStringsSep "+" (x: "KEY_" + x) keys} ${toString { press = 1; hold = 2; release = 0; }.${event}} ${cmd}''
17 bindingCfg = { ... }: {
21 type = lib.types.listOf lib.types.str;
22 description = "List of keys to match. Key names as defined in linux/input-event-codes.h";
25 event = lib.mkOption {
26 type = lib.types.enum ["press" "hold" "release"];
28 description = "Event to match.";
33 description = "What to run.";
47 services.triggerhappy = {
49 enable = lib.mkOption {
50 type = lib.types.bool;
53 Whether to enable the {command}`triggerhappy` hotkey daemon.
62 User account under which {command}`triggerhappy` runs.
66 bindings = lib.mkOption {
67 type = lib.types.listOf (lib.types.submodule bindingCfg);
69 example = lib.literalExpression ''
70 [ { keys = ["PLAYPAUSE"]; cmd = "''${pkgs.mpc-cli}/bin/mpc -q toggle"; } ]
73 Key bindings for {command}`triggerhappy`.
77 extraConfig = lib.mkOption {
78 type = lib.types.lines;
81 Literal contents to append to the end of {command}`triggerhappy` configuration file.
92 config = lib.mkIf cfg.enable {
94 systemd.sockets.triggerhappy = {
95 description = "Triggerhappy Socket";
96 wantedBy = [ "sockets.target" ];
97 socketConfig.ListenDatagram = socket;
100 systemd.services.triggerhappy = {
101 wantedBy = [ "multi-user.target" ];
102 description = "Global hotkey daemon";
104 ExecStart = "${pkgs.triggerhappy}/bin/thd ${lib.optionalString (cfg.user != "root") "--user ${cfg.user}"} --socket ${socket} --triggers ${configFile} --deviceglob /dev/input/event*";
108 services.udev.packages = lib.singleton (pkgs.writeTextFile {
109 name = "triggerhappy-udev-rules";
110 destination = "/etc/udev/rules.d/61-triggerhappy.rules";
112 ACTION=="add", SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}!="triggerhappy", \
113 RUN+="${pkgs.triggerhappy}/bin/th-cmd --socket ${socket} --passfd --udev"