2022-08-12 12:06:08 +00:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.kanata;
|
|
|
|
|
|
|
|
keyboard = {
|
|
|
|
options = {
|
|
|
|
devices = mkOption {
|
2023-02-22 10:55:15 +00:00
|
|
|
type = types.listOf types.str;
|
2023-10-09 19:29:22 +00:00
|
|
|
default = [ ];
|
2022-08-12 12:06:08 +00:00
|
|
|
example = [ "/dev/input/by-id/usb-0000_0000-event-kbd" ];
|
2023-10-09 19:29:22 +00:00
|
|
|
description = mdDoc ''
|
|
|
|
Paths to keyboard devices.
|
|
|
|
|
|
|
|
An empty list, the default value, lets kanata detect which
|
|
|
|
input devices are keyboards and intercept them all.
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
};
|
|
|
|
config = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
example = ''
|
|
|
|
(defsrc
|
|
|
|
grv 1 2 3 4 5 6 7 8 9 0 - = bspc
|
|
|
|
tab q w e r t y u i o p [ ] \
|
|
|
|
caps a s d f g h j k l ; ' ret
|
|
|
|
lsft z x c v b n m , . / rsft
|
|
|
|
lctl lmet lalt spc ralt rmet rctl)
|
|
|
|
|
|
|
|
(deflayer qwerty
|
|
|
|
grv 1 2 3 4 5 6 7 8 9 0 - = bspc
|
|
|
|
tab q w e r t y u i o p [ ] \
|
|
|
|
@cap a s d f g h j k l ; ' ret
|
|
|
|
lsft z x c v b n m , . / rsft
|
|
|
|
lctl lmet lalt spc ralt rmet rctl)
|
|
|
|
|
|
|
|
(defalias
|
|
|
|
;; tap within 100ms for capslk, hold more than 100ms for lctl
|
|
|
|
cap (tap-hold 100 100 caps lctl))
|
|
|
|
'';
|
|
|
|
description = mdDoc ''
|
2023-02-22 10:55:15 +00:00
|
|
|
Configuration other than `defcfg`.
|
|
|
|
|
|
|
|
See [example config files](https://github.com/jtroo/kanata)
|
|
|
|
for more information.
|
2022-08-12 12:06:08 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraDefCfg = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "danger-enable-cmd yes";
|
|
|
|
description = mdDoc ''
|
2023-02-22 10:55:15 +00:00
|
|
|
Configuration of `defcfg` other than `linux-dev` (generated
|
|
|
|
from the devices option) and
|
|
|
|
`linux-continue-if-no-devs-found` (hardcoded to be yes).
|
|
|
|
|
|
|
|
See [example config files](https://github.com/jtroo/kanata)
|
|
|
|
for more information.
|
2022-08-12 12:06:08 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
description = mdDoc "Extra command line arguments passed to kanata.";
|
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
type = types.nullOr types.port;
|
|
|
|
default = null;
|
|
|
|
example = 6666;
|
|
|
|
description = mdDoc ''
|
2023-02-22 10:55:15 +00:00
|
|
|
Port to run the TCP server on. `null` will not run the server.
|
2022-08-12 12:06:08 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkName = name: "kanata-${name}";
|
|
|
|
|
2023-02-22 10:55:15 +00:00
|
|
|
mkDevices = devices:
|
|
|
|
optionalString ((length devices) > 0) "linux-dev ${concatStringsSep ":" devices}";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
|
|
mkConfig = name: keyboard: pkgs.writeText "${mkName name}-config.kdb" ''
|
|
|
|
(defcfg
|
|
|
|
${keyboard.extraDefCfg}
|
2023-02-22 10:55:15 +00:00
|
|
|
${mkDevices keyboard.devices}
|
|
|
|
linux-continue-if-no-devs-found yes)
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
|
|
${keyboard.config}
|
|
|
|
'';
|
|
|
|
|
|
|
|
mkService = name: keyboard: nameValuePair (mkName name) {
|
2023-02-22 10:55:15 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
serviceConfig = {
|
2023-03-27 19:17:25 +00:00
|
|
|
Type = "notify";
|
2022-08-12 12:06:08 +00:00
|
|
|
ExecStart = ''
|
2023-02-22 10:55:15 +00:00
|
|
|
${getExe cfg.package} \
|
2022-08-12 12:06:08 +00:00
|
|
|
--cfg ${mkConfig name keyboard} \
|
|
|
|
--symlink-path ''${RUNTIME_DIRECTORY}/${name} \
|
|
|
|
${optionalString (keyboard.port != null) "--port ${toString keyboard.port}"} \
|
|
|
|
${utils.escapeSystemdExecArgs keyboard.extraArgs}
|
|
|
|
'';
|
|
|
|
|
|
|
|
DynamicUser = true;
|
|
|
|
RuntimeDirectory = mkName name;
|
|
|
|
SupplementaryGroups = with config.users.groups; [
|
|
|
|
input.name
|
|
|
|
uinput.name
|
|
|
|
];
|
|
|
|
|
|
|
|
# hardening
|
|
|
|
DeviceAllow = [
|
|
|
|
"/dev/uinput rw"
|
|
|
|
"char-input r"
|
|
|
|
];
|
|
|
|
CapabilityBoundingSet = [ "" ];
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
IPAddressAllow = optional (keyboard.port != null) "localhost";
|
|
|
|
IPAddressDeny = [ "any" ];
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
PrivateNetwork = keyboard.port == null;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
2023-03-27 19:17:25 +00:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" ] ++ optional (keyboard.port != null) "AF_INET";
|
2022-08-12 12:06:08 +00:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
SystemCallArchitectures = [ "native" ];
|
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
"~@privileged"
|
|
|
|
"~@resources"
|
|
|
|
];
|
|
|
|
UMask = "0077";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.kanata = {
|
2023-02-22 10:55:15 +00:00
|
|
|
enable = mkEnableOption (mdDoc "kanata");
|
2022-08-12 12:06:08 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.kanata;
|
2022-09-09 14:08:57 +00:00
|
|
|
defaultText = literalExpression "pkgs.kanata";
|
|
|
|
example = literalExpression "pkgs.kanata-with-cmd";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = mdDoc ''
|
|
|
|
The kanata package to use.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
If `danger-enable-cmd` is enabled in any of the keyboards, the
|
|
|
|
`kanata-with-cmd` package should be used.
|
|
|
|
:::
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
keyboards = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule keyboard);
|
|
|
|
default = { };
|
|
|
|
description = mdDoc "Keyboard configurations.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
config = mkIf cfg.enable {
|
2023-10-09 19:29:22 +00:00
|
|
|
warnings =
|
|
|
|
let
|
|
|
|
keyboardsWithEmptyDevices = filterAttrs (name: keyboard: keyboard.devices == [ ]) cfg.keyboards;
|
|
|
|
existEmptyDevices = length (attrNames keyboardsWithEmptyDevices) > 0;
|
|
|
|
moreThanOneKeyboard = length (attrNames cfg.keyboards) > 1;
|
|
|
|
in
|
|
|
|
optional (existEmptyDevices && moreThanOneKeyboard) "One device can only be intercepted by one kanata instance. Setting services.kanata.keyboards.${head (attrNames keyboardsWithEmptyDevices)}.devices = [ ] and using more than one services.kanata.keyboards may cause a race condition.";
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
hardware.uinput.enable = true;
|
|
|
|
|
2023-02-22 10:55:15 +00:00
|
|
|
systemd.services = mapAttrs' mkService cfg.keyboards;
|
2022-08-12 12:06:08 +00:00
|
|
|
};
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
meta.maintainers = with maintainers; [ linj ];
|
2022-08-12 12:06:08 +00:00
|
|
|
}
|