Configuring Caps Lock as Control on Console

I want to change the caps lock to control on console (not in X11 or Wayland).

Normally on X11, I would set console.useXkbConfig = true and set services.xserver.xkbOptions = "ctrl:nocaps" but that didn’t work here, probably because services.xserver.enable = false since I am using Sway and don’t want X11. Key mapping is working in Sway [1] so I am only asking the “NixOS way” to map on console without X11. Bonus points if it can be done in Home Manager without changing configuration.nix.

[1] for the benefit of others, I configured caps lock as control in Sway by adding input * xkb_options "ctrl:nocaps" to wayland.windowManager.sway.extraConfig in Home Manager.

1 Like

Note: I am also familiar with the incantation dumpkeys | sed 's/\s*58\s*=\s*Caps_Lock/ 58 = Control/' | loadkeys although I don’t know how to do it in NixOS.

I do this with services.udev.extraHwdb:

  KEYBOARD_KEY_70039=leftctrl     # CAPSLOCK

The b0003v258Ap0001e0110 and 70039 are specific to my keyboard, so you’ll have to find out which values to use for yours. To find those you will need the evtest package. Then run it with sudo evtest >evtest.out, select the input devide corresponding to your keyboard, press the key you want to map (caps lock in your case) and exit with ctrl-c. Now open evtest.out in your text editor. At the top of the file there will be a line like

Input device ID: bus 0x3 vendor 0x258a product 0x1 version 0x110

Those are the numbers you will have to plug in to b0003v258Ap0001e0110, pad the numbers with 0s so that they are 4 digits long and uppercase any letters.

Now at the bottom of the file there should be an entry like

Event: time 1602105240.379263, -------------- SYN_REPORT ------------ 
Event: time 1602105240.483256, type 4 (EV_MSC), code 4 (MSC_SCAN), value 70039                                   
Event: time 1602105240.483256, type 1 (EV_KEY), code 29 (KEY_CAPSLOCK), value 0 
Event: time 1602105240.379263, -------------- SYN_REPORT ------------ 

The value on the MSC_SCAN line (70039 in my case) is the code to put after KEYBOARD_KEY_.

PS after switching to the new configuration you may need to reboot for it to take effect.


I use

interception-tools.enable = true;

In my NixOS configuration to get Capslock as Control + Escape verywhere.


Thank you both. I went with @danieldk’s solution as it does exactly what I need quite easily but I squirreled away @olmokramer’s solution too as someday I might need to do something at that level. Once again thanks!

1 Like