nixpkgs/nixos/modules/services/hardware/kanata.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

186 lines
5.9 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, utils, ... }:
2022-07-24 17:49:20 +02:00
with lib;
let
cfg = config.services.kanata;
keyboard = {
options = {
devices = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "/dev/input/by-id/usb-0000_0000-event-kbd" ];
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))
'';
description = mdDoc ''
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";
description = mdDoc ''
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.
'';
};
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 ''
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}";
mkDevices = devices:
optionalString ((length devices) > 0) "linux-dev ${concatStringsSep ":" devices}";
2022-07-24 17:49:20 +02:00
mkConfig = name: keyboard: pkgs.writeText "${mkName name}-config.kdb" ''
(defcfg
${keyboard.extraDefCfg}
${mkDevices keyboard.devices}
linux-continue-if-no-devs-found yes)
2022-07-24 17:49:20 +02:00
${keyboard.config}
'';
mkService = name: keyboard: nameValuePair (mkName name) {
wantedBy = [ "multi-user.target" ];
2022-07-24 17:49:20 +02:00
serviceConfig = {
Type = "notify";
2022-07-24 17:49:20 +02:00
ExecStart = ''
${getExe cfg.package} \
--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;
RuntimeDirectory = mkName name;
2022-07-24 17:49:20 +02:00
SupplementaryGroups = with config.users.groups; [
input.name
uinput.name
];
# hardening
DeviceAllow = [
"/dev/uinput rw"
2022-07-24 17:49:20 +02:00
"char-input r"
];
CapabilityBoundingSet = [ "" ];
2022-07-24 17:49:20 +02:00
DevicePolicy = "closed";
IPAddressAllow = optional (keyboard.port != null) "localhost";
IPAddressDeny = [ "any" ];
2022-07-24 17:49:20 +02:00
LockPersonality = true;
MemoryDenyWriteExecute = true;
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";
RestrictAddressFamilies = [ "AF_UNIX" ] ++ optional (keyboard.port != null) "AF_INET";
2022-07-24 17:49:20 +02:00
RestrictNamespaces = true;
RestrictRealtime = true;
SystemCallArchitectures = [ "native" ];
2022-07-24 17:49:20 +02:00
SystemCallFilter = [
"@system-service"
"~@privileged"
"~@resources"
];
UMask = "0077";
};
};
in
{
options.services.kanata = {
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";
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 = { };
description = mdDoc "Keyboard configurations.";
2022-07-24 17:49:20 +02:00
};
};
2022-08-21 03:07:14 +02:00
config = mkIf cfg.enable {
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;
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
}