Skip to content

Commit

Permalink
Merge pull request #243271 from woojiq/keyd-support-multiple-configs
Browse files Browse the repository at this point in the history
nixos/keyd: add support for multiple configuration in different files
  • Loading branch information
pennae authored Jul 15, 2023
2 parents 1e70f3a + 2d3bf20 commit 45ae0ef
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
2 changes: 2 additions & 0 deletions nixos/doc/manual/release-notes/rl-2311.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
84 changes: 65 additions & 19 deletions nixos/modules/services/hardware/keyd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [ "*" ];
Expand All @@ -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;

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion nixos/tests/keyd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let
nodes.machine = {
services.keyd = {
enable = true;
inherit settings;
keyboards.default = { inherit settings; };
};
};

Expand Down

0 comments on commit 45ae0ef

Please sign in to comment.