Per-device keymap?

I have a laptop keyboard and an external one, that I’d like to have running different keymaps (specifically different xkbOptions).

Is there a way to configure this declaratively?

I’d like the laptop keyboard to have ctrl:swapcaps, but not the other keyboard.

1 Like

Use the following snippet:

services.xserver.displayManager.sessionCommands = ''
    ${pkgs.xorg.xinput}/bin/xinput |
      sed -nE 's/.*?MY IDENTIFIER.*?id=([0-9]+).*?master keyboard.*/\1/p'
  if [[ $keyboardId ]]; then
    ${pkgs.xorg.setxkbmap}/bin/setxkbmap -device $keyboardId -option ctrl:swapcaps

Replace MY IDENTIFIER with a pattern that identifies your keyboard.

Here is a complete VM example which swaps Caps Lock and Escape for a specific keyboard.


Thanks @earvstedt, that’s really helpful.

Unfortunately, I’m on GNOME wayland, and it looks like the setxkbmap program has no effect for me.

As far as I can tell the recommended way to set this under GNOME wayland is via dconf / gsettings, which calls libinput, and there doesn’t seem to be support for per-device settings at the moment.

Unless anyone knows a way around this, I might go back to X11, and use @earvstedt’s answer.

1 Like