From 16677264aeb734f2fdbd47106f9dde02bb6c9944 Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 10:10:00 +0800 Subject: [PATCH 1/8] nixos/kmonad: add a basic nixos test --- nixos/tests/all-tests.nix | 1 + nixos/tests/kmonad.nix | 47 +++++++++++++++++++ .../haskell-modules/configuration-nix.nix | 9 +++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 nixos/tests/kmonad.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 613ff1314488a..147e9c11612e3 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -520,6 +520,7 @@ in { keyd = handleTest ./keyd.nix {}; keymap = handleTest ./keymap.nix {}; kimai = handleTest ./kimai.nix {}; + kmonad = runTest ./kmonad.nix; knot = handleTest ./knot.nix {}; komga = handleTest ./komga.nix {}; krb5 = discoverTests (import ./krb5); diff --git a/nixos/tests/kmonad.nix b/nixos/tests/kmonad.nix new file mode 100644 index 0000000000000..e0150cb99f183 --- /dev/null +++ b/nixos/tests/kmonad.nix @@ -0,0 +1,47 @@ +{ lib, ... }: + +{ + name = "kmonad"; + + meta = { + maintainers = with lib.maintainers; [ linj ]; + }; + + nodes = { + machine = { + services.kmonad = { + enable = true; + keyboards = { + defaultKbd = { + device = "/dev/input/by-id/vm-default-kbd"; + defcfg = { + enable = true; + fallthrough = true; + }; + config = '' + (defsrc :name default-src + 1) + (deflayer default-layer :source default-src + @T2) + (defalias + T2 2) + ''; + }; + }; + }; + + # make a determinate symlink to the default vm keyboard for kmonad to use + services.udev.extraRules = '' + ACTION=="add", KERNEL=="event*", SUBSYSTEM=="input", ATTRS{name}=="QEMU Virtio Keyboard", ATTRS{id/product}=="0001", ATTRS{id/vendor}=="0627", SYMLINK+="input/by-id/vm-default-kbd" + ''; + }; + }; + + testScript = '' + service_name = "kmonad-defaultKbd" + machine.wait_for_unit(f"{service_name}.service") + + with subtest("kmonad is running"): + machine.succeed(f"systemctl status {service_name}") + ''; +} diff --git a/pkgs/development/haskell-modules/configuration-nix.nix b/pkgs/development/haskell-modules/configuration-nix.nix index 2a92ef16dc6fb..9bf3d526ab874 100644 --- a/pkgs/development/haskell-modules/configuration-nix.nix +++ b/pkgs/development/haskell-modules/configuration-nix.nix @@ -1478,7 +1478,14 @@ self: super: builtins.intersectAttrs super { # cause actual problems in dependent packages, see https://github.com/lehins/pvar/issues/4 pvar = dontCheck super.pvar; - kmonad = enableSeparateBinOutput super.kmonad; + kmonad = lib.pipe super.kmonad [ + enableSeparateBinOutput + (overrideCabal (drv: { + passthru = lib.recursiveUpdate + drv.passthru or { } + { tests.nixos = pkgs.nixosTests.kmonad; }; + })) + ]; xmobar = enableSeparateBinOutput super.xmobar; From ffea31ccbb8405044c909750dbff0db60c4d7e6d Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 07:11:12 +0800 Subject: [PATCH 2/8] nixos/kmonad: add meta.maintainers --- nixos/modules/services/hardware/kmonad.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index fc548d51603ee..f59ee69c580b7 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -188,4 +188,11 @@ in services = lib.mapAttrs' (_: mkService) cfg.keyboards; }; }; + + meta = { + maintainers = with lib.maintainers; [ + linj + rvdp + ]; + }; } From cbf4f6a86dbe355dbdf4f817c9a204fa16e28305 Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 06:48:34 +0800 Subject: [PATCH 3/8] nixos/kmonad: improve doc --- nixos/modules/services/hardware/kmonad.nix | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index f59ee69c580b7..2b5157afb6b9e 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -35,16 +35,16 @@ let Since KMonad runs as an unprivileged user, it may sometimes need extra permissions in order to read the keyboard device file. If your keyboard's device file isn't in the input - group you'll need to list its group in this option. + group, you'll need to list its group in this option. ''; }; defcfg = { enable = lib.mkEnableOption '' - Automatically generate the defcfg block. + automatic generation of the defcfg block. - When this is option is set to true the config option for - this keyboard should not include a defcfg block. + When this option is set to true, the config option for + this keyboard should not include a defcfg block ''; compose = { @@ -61,9 +61,9 @@ let }; }; - fallthrough = lib.mkEnableOption "Re-emit unhandled key events."; + fallthrough = lib.mkEnableOption "re-emitting unhandled key events"; - allowCommands = lib.mkEnableOption "Allow keys to run shell commands."; + allowCommands = lib.mkEnableOption "keys to run shell commands"; }; config = lib.mkOption { @@ -159,9 +159,9 @@ let in { options.services.kmonad = { - enable = lib.mkEnableOption "KMonad: An advanced keyboard manager."; + enable = lib.mkEnableOption "KMonad: an advanced keyboard manager"; - package = lib.mkPackageOption pkgs "kmonad" { }; + package = lib.mkPackageOption pkgs "KMonad" { default = "kmonad"; }; keyboards = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule keyboard); From 23768e2a1233f9695eda51e00c3054439e4ab33f Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 06:49:07 +0800 Subject: [PATCH 4/8] nixos/kmonad: simplify config for default keyboard name --- nixos/modules/services/hardware/kmonad.nix | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index 2b5157afb6b9e..8fa721a5f2339 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -15,6 +15,7 @@ let options = { name = lib.mkOption { type = lib.types.str; + default = name; example = "laptop-internal"; description = "Keyboard name."; }; @@ -71,10 +72,6 @@ let description = "Keyboard configuration."; }; }; - - config = { - name = lib.mkDefault name; - }; }; # Create a complete KMonad configuration file: From 657ff03cc341d2de5afa0b99c55c75e7f64b85e2 Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 06:49:34 +0800 Subject: [PATCH 5/8] nixos/kmonad: make type of delay more strict --- nixos/modules/services/hardware/kmonad.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index 8fa721a5f2339..197a4752ed161 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -56,7 +56,7 @@ let }; delay = lib.mkOption { - type = lib.types.int; + type = lib.types.ints.unsigned; default = 5; description = "The delay (in milliseconds) between compose key sequences."; }; From 28a1740fd1d3d397a322113c99a8a7a5f3a64d5f Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 07:01:45 +0800 Subject: [PATCH 6/8] nixos/kmonad: introduce a new helper function mkName --- nixos/modules/services/hardware/kmonad.nix | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index 197a4752ed161..41e4ffe2a6a9c 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -74,6 +74,8 @@ let }; }; + mkName = name: "kmonad-" + name; + # Create a complete KMonad configuration file: mkCfg = keyboard: @@ -81,7 +83,7 @@ let defcfg = '' (defcfg input (device-file "${keyboard.device}") - output (uinput-sink "kmonad-${keyboard.name}") + output (uinput-sink "${mkName keyboard.name}") ${lib.optionalString (keyboard.defcfg.compose.key != null) '' cmp-seq ${keyboard.defcfg.compose.key} cmp-seq-delay ${toString keyboard.defcfg.compose.delay} @@ -92,7 +94,7 @@ let ''; in pkgs.writeTextFile { - name = "kmonad-${keyboard.name}.cfg"; + name = "${mkName keyboard.name}.cfg"; text = lib.optionalString keyboard.defcfg.enable (defcfg + "\n") + keyboard.config; checkPhase = "${cfg.package}/bin/kmonad -d $out"; }; @@ -102,7 +104,7 @@ let mkPath = keyboard: let - name = "kmonad-${keyboard.name}"; + name = mkName keyboard.name; in lib.nameValuePair name { description = "KMonad trigger for ${keyboard.device}"; @@ -126,7 +128,7 @@ let ++ cfg.extraArgs ++ [ "${mkCfg keyboard}" ]; in - lib.nameValuePair "kmonad-${keyboard.name}" { + lib.nameValuePair (mkName keyboard.name) { description = "KMonad for ${keyboard.device}"; script = lib.escapeShellArgs cmd; unitConfig = { From f83da7d9bd14f07581cc6d8d9e744e49e0aa6203 Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 07:02:22 +0800 Subject: [PATCH 7/8] nixos/kmonad: use the official suffix for config file Both the official Emacs[1] and Vim[2] plugins use kbd as the config file suffix. [1]: https://github.com/kmonad/kbd-mode/blob/b07f3e16043fbb268b3e47fb49abc419e8b7e2d7/kbd-mode.el#L275 [2]: https://github.com/kmonad/kmonad-vim/blob/37978445197ab00edeb5b731e9ca90c2b141723f/ftdetect/kbd.vim#L18 --- nixos/modules/services/hardware/kmonad.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index 41e4ffe2a6a9c..cea9aafb377f9 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -94,7 +94,7 @@ let ''; in pkgs.writeTextFile { - name = "${mkName keyboard.name}.cfg"; + name = "${mkName keyboard.name}.kbd"; text = lib.optionalString keyboard.defcfg.enable (defcfg + "\n") + keyboard.config; checkPhase = "${cfg.package}/bin/kmonad -d $out"; }; From 8463300edfea65e290d17babd35ab50ab2c06a41 Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Wed, 1 Jan 2025 07:14:25 +0800 Subject: [PATCH 8/8] nixos/kmonad: use lib.getExe --- nixos/modules/services/hardware/kmonad.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/hardware/kmonad.nix b/nixos/modules/services/hardware/kmonad.nix index cea9aafb377f9..db526eb506b8c 100644 --- a/nixos/modules/services/hardware/kmonad.nix +++ b/nixos/modules/services/hardware/kmonad.nix @@ -96,7 +96,7 @@ let pkgs.writeTextFile { name = "${mkName keyboard.name}.kbd"; text = lib.optionalString keyboard.defcfg.enable (defcfg + "\n") + keyboard.config; - checkPhase = "${cfg.package}/bin/kmonad -d $out"; + checkPhase = "${lib.getExe cfg.package} -d $out"; }; # Build a systemd path config that starts the service below when a