Skip to content

Commit

Permalink
nixos/services.kanata: remove with lib;
Browse files Browse the repository at this point in the history
  • Loading branch information
Stunkymonkey committed Aug 29, 2024
1 parent f5c96d8 commit 3b7622d
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions nixos/modules/services/hardware/kanata.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{ config, lib, pkgs, utils, ... }:

with lib;

let
cfg = config.services.kanata;

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.";

keyboard = { name, config, ... }: {
options = {
devices = mkOption {
type = types.listOf types.str;
devices = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "/dev/input/by-id/usb-0000_0000-event-kbd" ];
description = ''
Expand All @@ -20,8 +17,8 @@ let
input devices are keyboards and intercept them all.
'';
};
config = mkOption {
type = types.lines;
config = lib.mkOption {
type = lib.types.lines;
example = ''
(defsrc
caps)
Expand All @@ -36,8 +33,8 @@ let
${upstreamDoc}
'';
};
extraDefCfg = mkOption {
type = types.lines;
extraDefCfg = lib.mkOption {
type = lib.types.lines;
default = "";
example = "danger-enable-cmd yes";
description = ''
Expand All @@ -48,8 +45,8 @@ let
${upstreamDoc}
'';
};
configFile = mkOption {
type = types.path;
configFile = lib.mkOption {
type = lib.types.path;
default = mkConfig name config;
defaultText =
"A config file generated by values from other kanata module options.";
Expand All @@ -63,13 +60,13 @@ let
overrides all other kanata module options. ${upstreamDoc}
'';
};
extraArgs = mkOption {
type = types.listOf types.str;
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Extra command line arguments passed to kanata.";
};
port = mkOption {
type = types.nullOr types.port;
port = lib.mkOption {
type = lib.types.nullOr lib.types.port;
default = null;
example = 6666;
description = ''
Expand All @@ -83,12 +80,12 @@ let

mkDevices = devices:
let
devicesString = pipe devices [
devicesString = lib.pipe devices [
(map (device: "\"" + device + "\""))
(concatStringsSep " ")
(lib.concatStringsSep " ")
];
in
optionalString ((length devices) > 0) "linux-dev (${devicesString})";
lib.optionalString ((lib.length devices) > 0) "linux-dev (${devicesString})";

mkConfig = name: keyboard: pkgs.writeTextFile {
name = "${mkName name}-config.kdb";
Expand All @@ -105,20 +102,20 @@ let
# at build time. I think this is a good balance between module
# complexity and functionality.
checkPhase = ''
${getExe cfg.package} --cfg "$target" --check --debug
${lib.getExe cfg.package} --cfg "$target" --check --debug
'';
};

mkService = name: keyboard: nameValuePair (mkName name) {
mkService = name: keyboard: lib.nameValuePair (mkName name) {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "notify";
ExecStart = ''
${getExe cfg.package} \
${lib.getExe cfg.package} \
--cfg ${keyboard.configFile} \
--symlink-path ''${RUNTIME_DIRECTORY}/${name} \
${optionalString (keyboard.port != null) "--port ${toString keyboard.port}"} \
${utils.escapeSystemdExecArgs keyboard.extraArgs}
${lib.optionalString (keyboard.port != null) "--port ${toString keyboard.port}"} \
${utils.lib.escapeSystemdExecArgs keyboard.extraArgs}

This comment has been minimized.

Copy link
@Ma27

Ma27 Sep 1, 2024

Member
'';

DynamicUser = true;
Expand All @@ -135,7 +132,7 @@ let
];
CapabilityBoundingSet = [ "" ];
DevicePolicy = "closed";
IPAddressAllow = optional (keyboard.port != null) "localhost";
IPAddressAllow = lib.optional (keyboard.port != null) "localhost";
IPAddressDeny = [ "any" ];
LockPersonality = true;
MemoryDenyWriteExecute = true;
Expand All @@ -150,7 +147,7 @@ let
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "invisible";
RestrictAddressFamilies = [ "AF_UNIX" ] ++ optional (keyboard.port != null) "AF_INET";
RestrictAddressFamilies = [ "AF_UNIX" ] ++ lib.optional (keyboard.port != null) "AF_INET";
RestrictNamespaces = true;
RestrictRealtime = true;
SystemCallArchitectures = [ "native" ];
Expand All @@ -165,8 +162,8 @@ let
in
{
options.services.kanata = {
enable = mkEnableOption "kanata, a tool to improve keyboard comfort and usability with advanced customization";
package = mkPackageOption pkgs "kanata" {
enable = lib.mkEnableOption "kanata, a tool to improve keyboard comfort and usability with advanced customization";
package = lib.mkPackageOption pkgs "kanata" {
example = [ "kanata-with-cmd" ];
extraDescription = ''
::: {.note}
Expand All @@ -175,26 +172,26 @@ in
:::
'';
};
keyboards = mkOption {
type = types.attrsOf (types.submodule keyboard);
keyboards = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule keyboard);
default = { };
description = "Keyboard configurations.";
};
};

config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {
warnings =
let
keyboardsWithEmptyDevices = filterAttrs (name: keyboard: keyboard.devices == [ ]) cfg.keyboards;
existEmptyDevices = length (attrNames keyboardsWithEmptyDevices) > 0;
moreThanOneKeyboard = length (attrNames cfg.keyboards) > 1;
keyboardsWithEmptyDevices = lib.filterAttrs (name: keyboard: keyboard.devices == [ ]) cfg.keyboards;
existEmptyDevices = lib.length (lib.attrNames keyboardsWithEmptyDevices) > 0;
moreThanOneKeyboard = lib.length (lib.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.";
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.";

hardware.uinput.enable = true;

systemd.services = mapAttrs' mkService cfg.keyboards;
systemd.services = lib.mapAttrs' mkService cfg.keyboards;
};

meta.maintainers = with maintainers; [ linj ];
meta.maintainers = with lib.maintainers; [ linj ];
}

1 comment on commit 3b7622d

@nixos-discourse
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/services-kanata-attribute-lib-missing/51476/2

Please sign in to comment.