summary refs log tree commit diff
path: root/nixos/modules/services/hardware/triggerhappy.nix
blob: 81d4a1ae65bf13ccd6bd1dd4e321b5cc94e86d4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{ 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 = { config, ... }: {
    options = {

      keys = mkOption {
        type = types.listOf types.str;
        description = "List of keys to match.  Key names as defined in linux/input-event-codes.h";
      };

      event = mkOption {
        type = types.enum ["press" "hold" "release"];
        default = "press";
        description = "Event to match.";
      };

      cmd = mkOption {
        type = types.str;
        description = "What to run.";
      };

    };
  };

in

{

  ###### interface

  options = {

    services.triggerhappy = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to enable the <command>triggerhappy</command> hotkey daemon.
        '';
      };

      bindings = mkOption {
        type = types.listOf (types.submodule bindingCfg);
        default = [];
        example = lib.literalExample ''
          [ { keys = ["PLAYPAUSE"];  cmd = "''${pkgs.mpc_cli}/bin/mpc -q toggle"; } ]
        '';
        description = ''
          Key bindings for <command>triggerhappy</command>.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Literal contents to append to the end of <command>triggerhappy</command> configuration file.
        '';
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    systemd.sockets.triggerhappy = {
      description = "Triggerhappy Socket";
      wantedBy = [ "sockets.target" ];
      socketConfig.ListenDatagram = socket;
    };

    systemd.services.triggerhappy = {
      wantedBy = [ "multi-user.target" ];
      after = [ "local-fs.target" ];
      description = "Global hotkey daemon";
      serviceConfig = {
        ExecStart = "${pkgs.triggerhappy}/bin/thd --user nobody --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"
      '';
    });

  };

}