15

I followed the instructions at this email thread, and placed

services.xserver.xkbOptions = "grp:alt_space_toggle, ctrl:swapcaps";

in my /etc/nixos/configuration.nix file, but even after rebuilding with $ nixos-rebuild switch, and rebooting with nixos-rebuild boot and reboot, my caps lock key is not remapped.

How to map caps-lock to ctrl in nixos?

mherzl
  • 1,509
  • 2
    services.xserver.xkbOptions should work, I am currently using this to set my caps <-> esc with caps:swapescape. maybe it is a bug. – wizzup Jul 11 '17 at 13:03
  • 1
    Note that option is only for the Xserver (Xorg). Did you test it within X windows? – Emmanuel Rosa Jul 12 '17 at 19:08
  • @EmmanuelRosa sorry but I am not clear on the difference. How can I know whether my window is in X window or Xorg? – mherzl Jul 12 '17 at 22:32
  • 1
    If you have a GUI, your in Xorg. Does the mapping work in say... your web browser? – Emmanuel Rosa Jul 13 '17 at 01:07
  • @EmmanualRosa yes I have a GUI using gnome. The mapping does not work in my web browser. – mherzl Jul 13 '17 at 01:21
  • @mherzl Is this only a problem in your web browser, or does this affect all graphical programs? – gmarmstrong Jul 08 '19 at 12:31
  • 1
    @gmarmstrong I have the same problem. I'm new to NixOS. I stuck services.xserver.xkbOptions = "ctrl:swapcaps"; into my configuration.nix, and after a reboot and back into Gnome Desktop (on X11, not Wayland), capslock is still capslock and ctrl is still ctrl. Is this a bug? – trusktr Jan 13 '20 at 23:54

4 Answers4

8

As you already tried, and as grwlf suggests, you can enable this in X11 by adding services.xserver.xkbOptions = "ctrl:swapcaps"; to /etc/nixos/configuration.nix.

To apply this setting in outside of X11, you can add console.useXkbConfig = true; to the configuration. This will apply the X keymap to the console keymap, which affects virtual consoles such as tty.

UPDATE: i18n.consoleUseXkbConfig was renamed to console.useXkbConfig in NixOS version 20.03

gmarmstrong
  • 1,233
  • 1
  • 16
  • 36
6

Let's say you have a configuration.nix like this.

  services.xserver.enable = true;
  services.xserver.layout = "pl";
  services.xserver.xkbOptions = "ctrl:nocaps";
  services.xserver.displayManager.gdm.enable = true;
  services.xserver.displayManager.gdm.wayland = false; # or true

Build it normally with nixos-rebuild switch.

Then run these commands:

gsettings reset org.gnome.desktop.input-sources xkb-options
gsettings reset org.gnome.desktop.input-sources sources

Now reboot.

Looks like the gsettings options are built once and don't pick up the changes to your configuration.nix. The commands above should reset the settings causing the values from configuration.nix to be picked up.

Source: https://discourse.nixos.org/t/problem-with-xkboptions-it-doesnt-seem-to-take-effect/5269/2?u=yeewe4

425nesp
  • 446
  • This worked for me, thank you so much ! Note that after those 2 commands, my keyboard reverted to qwerty which made the sudo reboot awkward. After reboot all is fine. – Eric Mar 05 '24 at 21:07
4

In my /etc/nixos/configuration.nix config,

  services.xserver = {
    ...
    xkbOptions = "ctrl:swapcaps";
    ...
  };

ctrl:swapcaps option swaps Capslock and Control.

Grwlf
  • 151
  • 3
    I stuck services.xserver.xkbOptions = "ctrl:swapcaps"; into my configuration.nix, and after a reboot and signin to Gnome Desktop (on X11, not Wayland), capslock is still capslock and ctrl is still ctrl. – trusktr Jan 13 '20 at 23:57
  • 1
    Did you run nixos-rebuild switch or analog to apply the configuration? In NixOS editing the config is not enough. – Grwlf Mar 16 '21 at 18:33
0

By default, ctrl:swapcaps only swaps Left Ctrl and Caps Lock, which is perhaps is not what you need.

A dirty solution is

services.xserver.xkbDir = "/usr/share/X11/xkb"

then

mkdir -p /usr/share/X11 && cp /run/current-system/sw/share/X11/xkb /usr/share/X11

and edit it to reach your perpose. Note, you need backup /usr/share/X11/xkb by yourself!

AdminBee
  • 22,803