about summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorpennae <82953136+pennae@users.noreply.github.com>2023-07-15 13:59:57 +0200
committerGitHub <noreply@github.com>2023-07-15 13:59:57 +0200
commit45ae0efbbce2aada6d5e8de6ace0c803b08ac9c7 (patch)
tree20e3a15417262a84aac6a718ae275b029559b78a /nixos
parent1e70f3a30ace51d9e5e6791a1ad4327dfcdf215b (diff)
parent2d3bf20086bf1b8d15f60c92f34d6e4b8565f07a (diff)
Merge pull request #243271 from woojiq/keyd-support-multiple-configs
nixos/keyd: add support for multiple configuration in different files
Diffstat (limited to 'nixos')
-rw-r--r--nixos/doc/manual/release-notes/rl-2311.section.md2
-rw-r--r--nixos/modules/services/hardware/keyd.nix84
-rw-r--r--nixos/tests/keyd.nix2
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 8cb00f1407ee1..587a54d736bc3 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 d17b0e4303ef7..969383fd4dc78 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/<keyboard>.conf`.
+          Appropriate names can be used to write non-alpha keys, for example "equal" instead of "=" sign (see <https://github.com/NixOS/nixpkgs/issues/236622>).
           See <https://github.com/rvaiya/keyd> how to configure.
         '';
       };
     };
   };
+in
+{
+  imports = [
+    (mkRemovedOptionModule [ "services" "keyd" "ids" ]
+      ''Use keyboards.<filename>.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.<filename>.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 d492cc194895c..1ee08b4101f72 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; };
       };
     };