2022-08-01 07:35:34 +00:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2022-07-24 15:49:20 +00:00
|
|
|
let
|
|
|
|
cfg = config.services.kanata;
|
|
|
|
|
2024-05-06 16:51:40 +00:00
|
|
|
upstreamDoc = "See [the upstream documentation](https://github.com/jtroo/kanata/blob/main/docs/config.adoc) and [example config files](https://github.com/jtroo/kanata/tree/main/cfg_samples) for more information.";
|
|
|
|
|
2024-05-22 22:06:57 +00:00
|
|
|
keyboard = { name, config, ... }: {
|
2022-07-24 15:49:20 +00:00
|
|
|
options = {
|
2024-08-27 18:43:19 +00:00
|
|
|
devices = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2023-09-18 05:54:21 +00:00
|
|
|
default = [ ];
|
2022-08-01 07:35:34 +00:00
|
|
|
example = [ "/dev/input/by-id/usb-0000_0000-event-kbd" ];
|
2023-09-18 05:54:21 +00:00
|
|
|
description = ''
|
|
|
|
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 15:49:20 +00:00
|
|
|
};
|
2024-08-27 18:43:19 +00:00
|
|
|
config = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2022-07-24 15:49:20 +00:00
|
|
|
example = ''
|
|
|
|
(defsrc
|
2024-05-06 17:23:31 +00:00
|
|
|
caps)
|
2022-07-24 15:49:20 +00:00
|
|
|
|
2024-05-06 17:23:31 +00:00
|
|
|
(deflayermap (default-layer)
|
|
|
|
;; tap caps lock as caps lock, hold caps lock as left control
|
|
|
|
caps (tap-hold 100 100 caps lctl))
|
2022-07-24 15:49:20 +00:00
|
|
|
'';
|
2022-08-01 07:35:34 +00:00
|
|
|
description = ''
|
2023-02-17 16:27:54 +00:00
|
|
|
Configuration other than `defcfg`.
|
|
|
|
|
2024-05-06 16:51:40 +00:00
|
|
|
${upstreamDoc}
|
2022-07-24 15:49:20 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-08-27 18:43:19 +00:00
|
|
|
extraDefCfg = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2022-07-24 15:49:20 +00:00
|
|
|
default = "";
|
|
|
|
example = "danger-enable-cmd yes";
|
2022-08-01 07:35:34 +00:00
|
|
|
description = ''
|
2023-02-17 16:08:49 +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).
|
|
|
|
|
2024-05-06 16:51:40 +00:00
|
|
|
${upstreamDoc}
|
2022-08-01 07:35:34 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-08-27 18:43:19 +00:00
|
|
|
configFile = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2024-05-22 22:06:57 +00:00
|
|
|
default = mkConfig name config;
|
|
|
|
defaultText =
|
|
|
|
"A config file generated by values from other kanata module options.";
|
|
|
|
description = ''
|
|
|
|
The config file.
|
|
|
|
|
|
|
|
By default, it is generated by values from other kanata
|
|
|
|
module options.
|
|
|
|
|
|
|
|
You can also set it to your own full config file which
|
|
|
|
overrides all other kanata module options. ${upstreamDoc}
|
|
|
|
'';
|
|
|
|
};
|
2024-08-27 18:43:19 +00:00
|
|
|
extraArgs = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2022-08-01 07:35:34 +00:00
|
|
|
default = [ ];
|
|
|
|
description = "Extra command line arguments passed to kanata.";
|
|
|
|
};
|
2024-08-27 18:43:19 +00:00
|
|
|
port = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.port;
|
2022-08-01 07:35:34 +00:00
|
|
|
default = null;
|
|
|
|
example = 6666;
|
|
|
|
description = ''
|
2023-02-17 16:27:54 +00:00
|
|
|
Port to run the TCP server on. `null` will not run the server.
|
2022-07-24 15:49:20 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkName = name: "kanata-${name}";
|
|
|
|
|
2023-02-17 16:24:05 +00:00
|
|
|
mkDevices = devices:
|
2023-12-23 00:08:19 +00:00
|
|
|
let
|
2024-08-27 18:43:19 +00:00
|
|
|
devicesString = lib.pipe devices [
|
2023-12-23 00:08:19 +00:00
|
|
|
(map (device: "\"" + device + "\""))
|
2024-08-27 18:43:19 +00:00
|
|
|
(lib.concatStringsSep " ")
|
2023-12-23 00:08:19 +00:00
|
|
|
];
|
|
|
|
in
|
2024-08-27 18:43:19 +00:00
|
|
|
lib.optionalString ((lib.length devices) > 0) "linux-dev (${devicesString})";
|
2022-08-01 07:35:34 +00:00
|
|
|
|
2024-05-06 11:39:20 +00:00
|
|
|
mkConfig = name: keyboard: pkgs.writeTextFile {
|
|
|
|
name = "${mkName name}-config.kdb";
|
|
|
|
text = ''
|
|
|
|
(defcfg
|
|
|
|
${keyboard.extraDefCfg}
|
|
|
|
${mkDevices keyboard.devices}
|
|
|
|
linux-continue-if-no-devs-found yes)
|
2022-07-24 15:49:20 +00:00
|
|
|
|
2024-05-06 11:39:20 +00:00
|
|
|
${keyboard.config}
|
|
|
|
'';
|
2024-05-22 22:06:57 +00:00
|
|
|
# Only the config file generated by this module is checked. A
|
|
|
|
# user-provided one is not checked because it may not be available
|
|
|
|
# at build time. I think this is a good balance between module
|
|
|
|
# complexity and functionality.
|
2024-05-06 11:39:20 +00:00
|
|
|
checkPhase = ''
|
2024-08-27 18:43:19 +00:00
|
|
|
${lib.getExe cfg.package} --cfg "$target" --check --debug
|
2024-05-06 11:39:20 +00:00
|
|
|
'';
|
|
|
|
};
|
2022-07-24 15:49:20 +00:00
|
|
|
|
2024-08-27 18:43:19 +00:00
|
|
|
mkService = name: keyboard: lib.nameValuePair (mkName name) {
|
2023-02-17 16:08:49 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-07-24 15:49:20 +00:00
|
|
|
serviceConfig = {
|
2023-02-16 12:59:43 +00:00
|
|
|
Type = "notify";
|
2022-07-24 15:49:20 +00:00
|
|
|
ExecStart = ''
|
2024-08-27 18:43:19 +00:00
|
|
|
${lib.getExe cfg.package} \
|
2024-05-22 22:06:57 +00:00
|
|
|
--cfg ${keyboard.configFile} \
|
2022-08-01 07:35:34 +00:00
|
|
|
--symlink-path ''${RUNTIME_DIRECTORY}/${name} \
|
2024-08-27 18:43:19 +00:00
|
|
|
${lib.optionalString (keyboard.port != null) "--port ${toString keyboard.port}"} \
|
2024-09-01 17:42:03 +00:00
|
|
|
${utils.escapeSystemdExecArgs keyboard.extraArgs}
|
2022-07-24 15:49:20 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
DynamicUser = true;
|
2022-08-01 07:35:34 +00:00
|
|
|
RuntimeDirectory = mkName name;
|
2022-07-24 15:49:20 +00:00
|
|
|
SupplementaryGroups = with config.users.groups; [
|
|
|
|
input.name
|
|
|
|
uinput.name
|
|
|
|
];
|
|
|
|
|
|
|
|
# hardening
|
|
|
|
DeviceAllow = [
|
2022-08-01 07:35:34 +00:00
|
|
|
"/dev/uinput rw"
|
2022-07-24 15:49:20 +00:00
|
|
|
"char-input r"
|
|
|
|
];
|
2022-08-01 07:35:34 +00:00
|
|
|
CapabilityBoundingSet = [ "" ];
|
2022-07-24 15:49:20 +00:00
|
|
|
DevicePolicy = "closed";
|
2024-08-27 18:43:19 +00:00
|
|
|
IPAddressAllow = lib.optional (keyboard.port != null) "localhost";
|
2022-08-01 07:35:34 +00:00
|
|
|
IPAddressDeny = [ "any" ];
|
2022-07-24 15:49:20 +00:00
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
2022-08-01 07:35:34 +00:00
|
|
|
PrivateNetwork = keyboard.port == null;
|
2022-07-24 15:49:20 +00:00
|
|
|
PrivateUsers = true;
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
2024-08-27 18:43:19 +00:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" ] ++ lib.optional (keyboard.port != null) "AF_INET";
|
2022-07-24 15:49:20 +00:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
2022-08-01 07:35:34 +00:00
|
|
|
SystemCallArchitectures = [ "native" ];
|
2022-07-24 15:49:20 +00:00
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
"~@privileged"
|
|
|
|
"~@resources"
|
|
|
|
];
|
|
|
|
UMask = "0077";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.kanata = {
|
2024-08-27 18:43:19 +00:00
|
|
|
enable = lib.mkEnableOption "kanata, a tool to improve keyboard comfort and usability with advanced customization";
|
|
|
|
package = lib.mkPackageOption pkgs "kanata" {
|
2024-05-06 16:41:03 +00:00
|
|
|
example = [ "kanata-with-cmd" ];
|
2023-11-27 00:19:27 +00:00
|
|
|
extraDescription = ''
|
2022-08-01 07:35:34 +00:00
|
|
|
::: {.note}
|
2023-11-27 00:19:27 +00:00
|
|
|
If {option}`danger-enable-cmd` is enabled in any of the keyboards, the
|
2022-08-01 07:35:34 +00:00
|
|
|
`kanata-with-cmd` package should be used.
|
|
|
|
:::
|
2022-07-24 15:49:20 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-08-27 18:43:19 +00:00
|
|
|
keyboards = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule keyboard);
|
2022-07-24 15:49:20 +00:00
|
|
|
default = { };
|
2022-08-01 07:35:34 +00:00
|
|
|
description = "Keyboard configurations.";
|
2022-07-24 15:49:20 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-27 18:43:19 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2023-09-18 05:54:21 +00:00
|
|
|
warnings =
|
|
|
|
let
|
2024-08-27 18:43:19 +00:00
|
|
|
keyboardsWithEmptyDevices = lib.filterAttrs (name: keyboard: keyboard.devices == [ ]) cfg.keyboards;
|
|
|
|
existEmptyDevices = lib.length (lib.attrNames keyboardsWithEmptyDevices) > 0;
|
|
|
|
moreThanOneKeyboard = lib.length (lib.attrNames cfg.keyboards) > 1;
|
2023-09-18 05:54:21 +00:00
|
|
|
in
|
2024-08-27 18:43:19 +00:00
|
|
|
lib.optional (existEmptyDevices && moreThanOneKeyboard) "One device can only be intercepted by one kanata instance. Setting services.kanata.keyboards.${lib.head (lib.attrNames keyboardsWithEmptyDevices)}.devices = [ ] and using more than one services.kanata.keyboards may cause a race condition.";
|
2023-09-18 05:54:21 +00:00
|
|
|
|
2022-07-24 15:49:20 +00:00
|
|
|
hardware.uinput.enable = true;
|
|
|
|
|
2024-08-27 18:43:19 +00:00
|
|
|
systemd.services = lib.mapAttrs' mkService cfg.keyboards;
|
2022-07-24 15:49:20 +00:00
|
|
|
};
|
|
|
|
|
2024-08-27 18:43:19 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ linj ];
|
2022-07-24 15:49:20 +00:00
|
|
|
}
|