1 { config, lib, pkgs, ... }:
7 cfg = config.services.triggerhappy;
9 socket = "/run/thd.socket";
11 configFile = pkgs.writeText "triggerhappy.conf" ''
12 ${concatMapStringsSep "\n"
13 ({ keys, event, cmd, ... }:
14 ''${concatMapStringsSep "+" (x: "KEY_" + x) keys} ${toString { press = 1; hold = 2; release = 0; }.${event}} ${cmd}''
20 bindingCfg = { ... }: {
24 type = types.listOf types.str;
25 description = lib.mdDoc "List of keys to match. Key names as defined in linux/input-event-codes.h";
29 type = types.enum ["press" "hold" "release"];
31 description = lib.mdDoc "Event to match.";
36 description = lib.mdDoc "What to run.";
50 services.triggerhappy = {
55 description = lib.mdDoc ''
56 Whether to enable the {command}`triggerhappy` hotkey daemon.
64 description = lib.mdDoc ''
65 User account under which {command}`triggerhappy` runs.
70 type = types.listOf (types.submodule bindingCfg);
72 example = lib.literalExpression ''
73 [ { keys = ["PLAYPAUSE"]; cmd = "''${pkgs.mpc-cli}/bin/mpc -q toggle"; } ]
75 description = lib.mdDoc ''
76 Key bindings for {command}`triggerhappy`.
80 extraConfig = mkOption {
83 description = lib.mdDoc ''
84 Literal contents to append to the end of {command}`triggerhappy` configuration file.
95 config = mkIf cfg.enable {
97 systemd.sockets.triggerhappy = {
98 description = "Triggerhappy Socket";
99 wantedBy = [ "sockets.target" ];
100 socketConfig.ListenDatagram = socket;
103 systemd.services.triggerhappy = {
104 wantedBy = [ "multi-user.target" ];
105 description = "Global hotkey daemon";
107 ExecStart = "${pkgs.triggerhappy}/bin/thd ${optionalString (cfg.user != "root") "--user ${cfg.user}"} --socket ${socket} --triggers ${configFile} --deviceglob /dev/input/event*";
111 services.udev.packages = lib.singleton (pkgs.writeTextFile {
112 name = "triggerhappy-udev-rules";
113 destination = "/etc/udev/rules.d/61-triggerhappy.rules";
115 ACTION=="add", SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}!="triggerhappy", \
116 RUN+="${pkgs.triggerhappy}/bin/th-cmd --socket ${socket} --passfd --udev"