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;
|
2022-08-12 12:06:08 +00:00
|
|
|
example = [ "/dev/input/by-id/usb-0000_0000-event-kbd" ];
|
2023-02-22 10:55:15 +00:00
|
|
|
description = mdDoc "Paths to keyboard devices.";
|
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 {
|
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
|
|
|
}
|