..

Make Capslock Great Again: XKB Customization for Sway

As I’m on the unstable branch of NixOS, one day I woke up to a NixOS config that was failing to build. Specifically, the derivation for sway.conf wouldn’t build. The keyboard layout I use is special. I usually switch between two customized keyboard layouts. I use the Dvorak layout to write English, and Persian (with Persian numpad) as my second layout. All my attempts to use my previous config, where I just put two modified xkb_symbol files in ~/.config/xkb/ failed. I used to have some line like this in my Sway config: xkb_layout = "us_with_vim,ir_with_vim".

But now the only option that I could use without NixOS throwing an error at me was the xkb_file which, as explained in sway-input(5), this option overrides the two previous options that I was using.

It was hard to find an xkb_file example with two layouts in it on the web. So, here you are, a XKB keymap that I wrote that not only has two layouts in it, but also makes capslock great again by changing capslock+hjkl into arrow keys. Also, the numpad is never turned off.

xkb_keymap {
  xkb_keycodes  { include "evdev+aliases(qwerty)" };
  xkb_types     { include "complete+numpad(mac)" };
  xkb_compat    { include "complete" };
  xkb_symbols   {
    include "pc+us(dvorak)+inet(evdev)+group(alts_toggle)"
    name[Group1] = "dvorak with vim";
    
    include "ir(pes_keypad):2+group(alts_toggle):2"
    name[Group2] = "persian with vim";

    // Changes for us(dvorak)
    key <CAPS> {
      type= "TWO_LEVEL",
      symbols[Group1]= [ ISO_Level3_Shift ],
      symbols[Group2]= [ ISO_Level3_Shift ]
    };

    key <AC02> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ o, O, KP_Enter, KP_Enter ],
      symbols[Group2]= [ Arabic_seen, Arabic_hamzaonyeh, KP_Enter, KP_Enter ]
    };

    key <AC03> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ e, E, Next, Next ],
      symbols[Group2]= [ Farsi_yeh, Arabic_yeh, Arabic_alefmaksura ]
    };
    key <AC04> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ u, U, BackSpace, BackSpace ],
      symbols[Group2]= [ Arabic_beh, Arabic_hamzaunderalef, BackSpace, BackSpace ]
    };
    key <AC06> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ d, D, Left, Left ],
      symbols[Group2]= [ Arabic_alef, Arabic_maddaonalef, Left, Left ]
    };
    key <AC07> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ h, H, Down, Down ],
      symbols[Group2]= [ Arabic_teh, Arabic_tatweel, Down, Down ]
    };
    key <AC08> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ t, T, Up, Up ],
      symbols[Group2]= [ Arabic_noon, guillemotright, Up, Up ]
    };
    key <AC09> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ n, N, Right, Right ],
      symbols[Group2]= [ Arabic_meem, guillemotleft, Right, Right ]
    };
    key <AD07> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ g, G, Prior, Prior ],
      symbols[Group2]= [ Arabic_ain, Arabic_fatha, Prior, Prior ]
    };
    key <AD08> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ c, C, Delete, Delete ],
      symbols[Group2]= [ Arabic_heh, bracketright, Delete, Delete ]
    };
    key <AE03> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ 3, numbersign, Home, Home ],
      symbols[Group2]= [ Farsi_3, 0x100066b, Home, Home ]
    };
    key <AE04> {
      type= "FOUR_LEVEL",
      symbols[Group1]= [ 4, dollar, End, End ],
      symbols[Group2]= [ Farsi_4, 0x100fdfc, End, End ]
    };
   };
  xkb_geometry  { include "pc(pc105)" };
};