2022-08-01 09:35:34 +02:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2022-07-24 17:49:20 +02:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.kanata;
|
|
|
|
|
|
|
|
keyboard = {
|
|
|
|
options = {
|
2022-08-01 09:35:34 +02:00
|
|
|
devices = mkOption {
|
2023-02-17 17:24:05 +01:00
|
|
|
type = types.listOf types.str;
|
2023-09-18 07:54:21 +02:00
|
|
|
default = [ ];
|
2022-08-01 09:35:34 +02:00
|
|
|
example = [ "/dev/input/by-id/usb-0000_0000-event-kbd" ];
|
2023-09-18 07:54:21 +02: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-07-24 17:49:20 +02: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))
|
|
|
|
'';
|
2022-08-01 09:35:34 +02:00
|
|
|
description = mdDoc ''
|
2023-02-17 17:27:54 +01:00
|
|
|
Configuration other than `defcfg`.
|
|
|
|
|
|
|
|
See [example config files](https://github.com/jtroo/kanata)
|
|
|
|
for more information.
|
2022-07-24 17:49:20 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraDefCfg = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "danger-enable-cmd yes";
|
2022-08-01 09:35:34 +02:00
|
|
|
description = mdDoc ''
|
2023-02-17 17:08:49 +01: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-01 09:35:34 +02: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-17 17:27:54 +01:00
|
|
|
Port to run the TCP server on. `null` will not run the server.
|
2022-07-24 17:49:20 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkName = name: "kanata-${name}";
|
|
|
|
|
2023-02-17 17:24:05 +01:00
|
|
|
mkDevices = devices:
|
|
|
|
optionalString ((length devices) > 0) "linux-dev ${concatStringsSep ":" devices}";
|
2022-08-01 09:35:34 +02:00
|
|
|
|
2022-07-24 17:49:20 +02:00
|
|
|
mkConfig = name: keyboard: pkgs.writeText "${mkName name}-config.kdb" ''
|
|
|
|
(defcfg
|
|
|
|
${keyboard.extraDefCfg}
|
2023-02-17 17:24:05 +01:00
|
|
|
${mkDevices keyboard.devices}
|
2023-02-17 17:08:49 +01:00
|
|
|
linux-continue-if-no-devs-found yes)
|
2022-07-24 17:49:20 +02:00
|
|
|
|
|
|
|
${keyboard.config}
|
|
|
|
'';
|
|
|
|
|
|
|
|
mkService = name: keyboard: nameValuePair (mkName name) {
|
2023-02-17 17:08:49 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-07-24 17:49:20 +02:00
|
|
|
serviceConfig = {
|
2023-02-16 13:59:43 +01:00
|
|
|
Type = "notify";
|
2022-07-24 17:49:20 +02:00
|
|
|
ExecStart = ''
|
2023-02-17 17:27:54 +01:00
|
|
|
${getExe cfg.package} \
|
2022-08-01 09:35:34 +02:00
|
|
|
--cfg ${mkConfig name keyboard} \
|
|
|
|
--symlink-path ''${RUNTIME_DIRECTORY}/${name} \
|
|
|
|
${optionalString (keyboard.port != null) "--port ${toString keyboard.port}"} \
|
|
|
|
${utils.escapeSystemdExecArgs keyboard.extraArgs}
|
2022-07-24 17:49:20 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
DynamicUser = true;
|
2022-08-01 09:35:34 +02:00
|
|
|
RuntimeDirectory = mkName name;
|
2022-07-24 17:49:20 +02:00
|
|
|
SupplementaryGroups = with config.users.groups; [
|
|
|
|
input.name
|
|
|
|
uinput.name
|
|
|
|
];
|
|
|
|
|
|
|
|
# hardening
|
|
|
|
DeviceAllow = [
|
2022-08-01 09:35:34 +02:00
|
|
|
"/dev/uinput rw"
|
2022-07-24 17:49:20 +02:00
|
|
|
"char-input r"
|
|
|
|
];
|
2022-08-01 09:35:34 +02:00
|
|
|
CapabilityBoundingSet = [ "" ];
|
2022-07-24 17:49:20 +02:00
|
|
|
DevicePolicy = "closed";
|
2022-08-01 09:35:34 +02:00
|
|
|
IPAddressAllow = optional (keyboard.port != null) "localhost";
|
|
|
|
IPAddressDeny = [ "any" ];
|
2022-07-24 17:49:20 +02:00
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
2022-08-01 09:35:34 +02:00
|
|
|
PrivateNetwork = keyboard.port == null;
|
2022-07-24 17:49:20 +02:00
|
|
|
PrivateUsers = true;
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
2023-02-16 13:59:43 +01:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" ] ++ optional (keyboard.port != null) "AF_INET";
|
2022-07-24 17:49:20 +02:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
2022-08-01 09:35:34 +02:00
|
|
|
SystemCallArchitectures = [ "native" ];
|
2022-07-24 17:49:20 +02:00
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
"~@privileged"
|
|
|
|
"~@resources"
|
|
|
|
];
|
|
|
|
UMask = "0077";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.kanata = {
|
2023-02-17 17:27:54 +01:00
|
|
|
enable = mkEnableOption (mdDoc "kanata");
|
2022-07-24 17:49:20 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.kanata;
|
2022-08-21 03:07:14 +02:00
|
|
|
defaultText = literalExpression "pkgs.kanata";
|
|
|
|
example = literalExpression "pkgs.kanata-with-cmd";
|
2022-08-01 09:35:34 +02: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.
|
|
|
|
:::
|
2022-07-24 17:49:20 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
keyboards = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule keyboard);
|
|
|
|
default = { };
|
2022-08-01 09:35:34 +02:00
|
|
|
description = mdDoc "Keyboard configurations.";
|
2022-07-24 17:49:20 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-21 03:07:14 +02:00
|
|
|
config = mkIf cfg.enable {
|
2023-09-18 07:54:21 +02: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-07-24 17:49:20 +02:00
|
|
|
hardware.uinput.enable = true;
|
|
|
|
|
2023-02-17 17:08:49 +01:00
|
|
|
systemd.services = mapAttrs' mkService cfg.keyboards;
|
2022-07-24 17:49:20 +02:00
|
|
|
};
|
|
|
|
|
2022-08-21 03:07:14 +02:00
|
|
|
meta.maintainers = with maintainers; [ linj ];
|
2022-07-24 17:49:20 +02:00
|
|
|
}
|