nixos/services.kanata: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-27 20:43:19 +02:00
parent f5c96d88c1
commit 3b7622d31b

View File

@ -1,7 +1,4 @@
{ config, lib, pkgs, utils, ... }:
with lib;
let
cfg = config.services.kanata;
@ -9,8 +6,8 @@ let
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 = ''
@ -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)
@ -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 = ''
@ -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.";
@ -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 = ''
@ -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";
@ -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}
'';
DynamicUser = true;
@ -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;
@ -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" ];
@ -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}
@ -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 ];
}