Setting custom keyboard layout in configuration.nix

I tried to activate my custom keyboard layout via the method explained in the Advanced section of Keyboard Layout Customization - NixOS Wiki.
Unfortunately it didn’t work.
If I execute the xkbcomp command directly on my terminal with the same layout.xkb file it works.

What I have currently in my configuration.nix is this:

  displayManager.sessionCommands = ''
    ${pkgs.xorg.xkbcomp}/bin/xkbcomp ${/configuration/layout.xkb} $DISPLAY
  '';

My layout.xkb file looks like this:

xkb_keymap {
xkb_keycodes { include “evdev+aliases(qwerty)” };
xkb_types { include “complete” };
xkb_compat { include “complete” };

partial alphanumeric_keys
xkb_symbols "thomad" {
    include "pc+at+us:2+inet(evdev)+terminate(ctrl_alt_bksp)"

    name[Group1] = "German (Austria, thomad)";

    key.type[Group1] = "FOUR_LEVEL";
    key <AC01> { [      a,      A,      braceleft,      NoSymbol ] };
    key <AC02> { [      s,      S,      braceright,     NoSymbol ] };
    key <AC03> { [      d,      D,      bracketleft,    NoSymbol ] };
    key <AC04> { [      f,      F,      bracketright,   NoSymbol ] };
    key <AC05> { [      g,      G,      backslash,      NoSymbol ] };
};

xkb_geometry  { include "pc(pc104)"	};

};

I also tried Custom Keyboard Layout on NixOS, revisited which I found quite nice because it’s better integrated. But the rebuild is too time consuming and it didn’t work for me either.

2 Likes

Gnome and KDE like to control and override that type of settings. It might depend on the DE that you’re using.

Run some extremely simple window manager and work from there to test.

Also just add “echo test > /tmp/mytest” to that expression to know that it’s getting executed. You probably also want to check exactly when that’s being executed. (I don’t know all the answers, but that’s the very least you should do). If you find a bug, please report it in nixpkgs.

I had this exact problem a while back. I ended up using a daughterboard to sidestep it :joy:
Report back if you have any breakthroughs.

@ThomasMader I had a similar problem to yours and I managed to set a custom keyboard layout. Here’s how I did: Configure custom keyboard layout · dmarcoux/dotfiles@272eb7e · GitHub

I know it’s a bit late, but it might help other people.

2 Likes

Thanks for posting this. I’ll be looking at adding a custom keyboard soon. It’s a shame that still this many years later there’s not an easier way.

You can use this now: NixOS 23.11 manual | Nix & NixOS

Just in case anyone comes to this thread looking for a nice general way to add a custom layout (although this doesn’t answer the original question about using XKB), NixOS unstable has kmonad: GitHub - kmonad/kmonad: An advanced keyboard manager