forked from kmonad/kmonad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nixos-module.nix
186 lines (160 loc) · 5.12 KB
/
nixos-module.nix
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{ config, lib, pkgs, ... }:
let
cfg = config.services.kmonad;
# Per-keyboard options:
keyboard = { name, ... }: {
options = {
name = lib.mkOption {
type = lib.types.str;
example = "laptop-internal";
description = "Keyboard name.";
};
device = lib.mkOption {
type = lib.types.path;
example = "/dev/input/by-id/some-dev";
description = "Path to the keyboard's device file.";
};
extraGroups = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "openrazer" ];
description = ''
Extra permission groups to attach to the KMonad instance for
this keyboard.
Since KMonad runs as an unprivileged user, it may sometimes
need extra permissions in order to read the keyboard device
file. If your keyboard's device file isn't in the input
group you'll need to list its group in this option.
'';
};
defcfg = {
enable = lib.mkEnableOption ''
Automatically generate the defcfg block.
When this is option is set to true the config option for
this keyboard should not include a defcfg block.
'';
compose = {
key = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = "ralt";
description = "The (optional) compose key to use.";
};
delay = lib.mkOption {
type = lib.types.int;
default = 5;
description = "The delay (in milliseconds) between compose key sequences.";
};
};
fallthrough = lib.mkEnableOption "Reemit unhandled key events.";
allowCommands = lib.mkEnableOption "Allow keys to run shell commands.";
};
config = lib.mkOption {
type = lib.types.lines;
description = "Keyboard configuration.";
};
};
config = {
name = lib.mkDefault name;
};
};
# Create a complete KMonad configuration file:
mkCfg = keyboard:
let defcfg = ''
(defcfg
input (device-file "${keyboard.device}")
output (uinput-sink "kmonad-${keyboard.name}")
'' +
lib.optionalString (keyboard.defcfg.compose.key != null) ''
cmp-seq ${keyboard.defcfg.compose.key}
cmp-seq-delay ${toString keyboard.defcfg.compose.delay}
'' + ''
fallthrough ${lib.boolToString keyboard.defcfg.fallthrough}
allow-cmd ${lib.boolToString keyboard.defcfg.allowCommands}
)
'';
in
pkgs.writeTextFile {
name = "kmonad-${keyboard.name}.cfg";
text = lib.optionalString keyboard.defcfg.enable (defcfg + "\n") + keyboard.config;
checkPhase = "${cfg.package}/bin/kmonad -d $out";
};
# Build a systemd path config that starts the service below when a
# keyboard device appears:
mkPath = keyboard: rec {
name = "kmonad-${keyboard.name}";
value = {
description = "KMonad trigger for ${keyboard.device}";
wantedBy = [ "default.target" ];
pathConfig.Unit = "${name}.service";
pathConfig.PathExists = keyboard.device;
};
};
# Build a systemd service that starts KMonad:
mkService = keyboard:
let
cmd = [
"${cfg.package}/bin/kmonad"
"--input"
''device-file "${keyboard.device}"''
] ++ cfg.extraArgs ++ [
"${mkCfg keyboard}"
];
groups = [
"input"
"uinput"
] ++ keyboard.extraGroups;
in
{
name = "kmonad-${keyboard.name}";
value = {
description = "KMonad for ${keyboard.device}";
script = lib.escapeShellArgs cmd;
serviceConfig.Restart = "always";
serviceConfig.User = "kmonad";
serviceConfig.SupplementaryGroups = groups;
serviceConfig.Nice = -20;
};
};
in
{
options.services.kmonad = {
enable = lib.mkEnableOption "KMonad: An advanced keyboard manager.";
package = lib.mkOption {
type = lib.types.package;
default = pkgs.kmonad;
example = "pkgs.haskellPackages.kmonad";
description = "The KMonad package to use.";
};
keyboards = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule keyboard);
default = { };
description = "Keyboard configuration.";
};
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "--log-level" "debug" ];
description = "Extra arguments to pass to KMonad.";
};
};
config = lib.mkIf cfg.enable {
environment.systemPackages = [ cfg.package ];
users.groups.uinput = { };
users.groups.kmonad = { };
users.users.kmonad = {
description = "KMonad system user.";
group = "kmonad";
isSystemUser = true;
};
services.udev.extraRules = ''
# KMonad user access to /dev/uinput
KERNEL=="uinput", MODE="0660", GROUP="uinput", OPTIONS+="static_node=uinput"
'';
systemd.paths =
builtins.listToAttrs
(map mkPath (builtins.attrValues cfg.keyboards));
systemd.services =
builtins.listToAttrs
(map mkService (builtins.attrValues cfg.keyboards));
};
}