From 2d3bf20086bf1b8d15f60c92f34d6e4b8565f07a Mon Sep 17 00:00:00 2001 From: woojiq Date: Thu, 13 Jul 2023 16:03:48 +0300 Subject: [PATCH] nixos/keyd: add support for multi-file configuration Add `keyboards` option to define different configurations for different IDs. This creates the appropriate files in `/etc/keyd` instead of just `default.conf` as before. Add `23.11` release note entry. Add `mkRemovedOptionModule` for the old API with a note on how to revert the old behavior. --- .../manual/release-notes/rl-2311.section.md | 2 + nixos/modules/services/hardware/keyd.nix | 84 ++++++++++++++----- nixos/tests/keyd.nix | 2 +- 3 files changed, 68 insertions(+), 20 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2311.section.md b/nixos/doc/manual/release-notes/rl-2311.section.md index 29ee2ec5aa6..58a7b44cb42 100644 --- a/nixos/doc/manual/release-notes/rl-2311.section.md +++ b/nixos/doc/manual/release-notes/rl-2311.section.md @@ -60,6 +60,8 @@ - `util-linux` is now supported on Darwin and is no longer an alias to `unixtools`. Use the `unixtools.util-linux` package for access to the Apple variants of the utilities. +- `services.keyd` changed API. Now you can create multiple configuration files. + - `services.ddclient` has been removed on the request of the upstream maintainer because it is unmaintained and has bugs. Please switch to a different software like `inadyn` or `knsupdate`. - The `vlock` program from the `kbd` package has been moved into its own package output and should now be referenced explicitly as `kbd.vlock` or replaced with an alternative such as the standalone `vlock` package or `physlock`. diff --git a/nixos/modules/services/hardware/keyd.nix b/nixos/modules/services/hardware/keyd.nix index d17b0e4303e..969383fd4dc 100644 --- a/nixos/modules/services/hardware/keyd.nix +++ b/nixos/modules/services/hardware/keyd.nix @@ -3,12 +3,9 @@ with lib; let cfg = config.services.keyd; settingsFormat = pkgs.formats.ini { }; -in -{ - options = { - services.keyd = { - enable = mkEnableOption (lib.mdDoc "keyd, a key remapping daemon"); + keyboardOptions = { ... }: { + options = { ids = mkOption { type = types.listOf types.string; default = [ "*" ]; @@ -35,24 +32,71 @@ in }; }; description = lib.mdDoc '' - Configuration, except `ids` section, that is written to {file}`/etc/keyd/default.conf`. + Configuration, except `ids` section, that is written to {file}`/etc/keyd/.conf`. + Appropriate names can be used to write non-alpha keys, for example "equal" instead of "=" sign (see ). See how to configure. ''; }; }; }; +in +{ + imports = [ + (mkRemovedOptionModule [ "services" "keyd" "ids" ] + ''Use keyboards..ids instead. If you don't need a multi-file configuration, just add keyboards.default before the ids. See https://github.com/NixOS/nixpkgs/pull/243271.'') + (mkRemovedOptionModule [ "services" "keyd" "settings" ] + ''Use keyboards..settings instead. If you don't need a multi-file configuration, just add keyboards.default before the settings. See https://github.com/NixOS/nixpkgs/pull/243271.'') + ]; + + options.services.keyd = { + enable = mkEnableOption (lib.mdDoc "keyd, a key remapping daemon"); + + keyboards = mkOption { + type = types.attrsOf (types.submodule keyboardOptions); + default = { }; + example = literalExpression '' + { + default = { + ids = [ "*" ]; + settings = { + main = { + capslock = "overload(control, esc)"; + }; + }; + }; + externalKeyboard = { + ids = [ "1ea7:0907" ]; + settings = { + main = { + esc = capslock; + }; + }; + }; + } + ''; + description = mdDoc '' + Configuration for one or more device IDs. Corresponding files in the /etc/keyd/ directory are created according to the name of the keys (like `default` or `externalKeyboard`). + ''; + }; + }; config = mkIf cfg.enable { - environment.etc."keyd/default.conf".source = pkgs.runCommand "default.conf" - { - ids = '' - [ids] - ${concatStringsSep "\n" cfg.ids} - ''; - passAsFile = [ "ids" ]; - } '' - cat $idsPath <(echo) ${settingsFormat.generate "keyd-main.conf" cfg.settings} >$out - ''; + # Creates separate files in the `/etc/keyd/` directory for each key in the dictionary + environment.etc = mapAttrs' + (name: options: + nameValuePair "keyd/${name}.conf" { + source = pkgs.runCommand "${name}.conf" + { + ids = '' + [ids] + ${concatStringsSep "\n" options.ids} + ''; + passAsFile = [ "ids" ]; + } '' + cat $idsPath <(echo) ${settingsFormat.generate "keyd-${name}.conf" options.settings} >$out + ''; + }) + cfg.keyboards; hardware.uinput.enable = lib.mkDefault true; @@ -62,9 +106,11 @@ in wantedBy = [ "multi-user.target" ]; - restartTriggers = [ - config.environment.etc."keyd/default.conf".source - ]; + restartTriggers = mapAttrsToList + (name: options: + config.environment.etc."keyd/${name}.conf".source + ) + cfg.keyboards; # this is configurable in 2.4.2, later versions seem to remove this option. # post-2.4.2 may need to set makeFlags in the derivation: diff --git a/nixos/tests/keyd.nix b/nixos/tests/keyd.nix index d492cc19489..1ee08b4101f 100644 --- a/nixos/tests/keyd.nix +++ b/nixos/tests/keyd.nix @@ -32,7 +32,7 @@ let nodes.machine = { services.keyd = { enable = true; - inherit settings; + keyboards.default = { inherit settings; }; }; };