Keyboard Layout Customization: Difference between revisions

From NixOS Wiki
imported>Infinisil
Created page with "= Keyboard Layout Customization = == Simple == The easiest way to customize your keyboard layout on NixOS is with these options: * <code>services.xserver.layout</code>: Key..."
 
→‎Relevant other options: Update xkb options
 
(20 intermediate revisions by 17 users not shown)
Line 1: Line 1:
= Keyboard Layout Customization =
== 20.09 and later ==


== Simple ==
In 20.09 there's [https://nixos.org/manual/nixos/stable/#custom-xkb-layouts services.xserver.extraLayouts] for this.
 
== Using xkbcomp ==
 
=== Simple ===


The easiest way to customize your keyboard layout on NixOS is with these options:
The easiest way to customize your keyboard layout on NixOS is with these options:


* <code>services.xserver.layout</code>: Keyboard layout, or multiple keyboard layouts separated by commas.
* <code>services.xserver.xkb.layout</code>: Keyboard layout, or multiple keyboard layouts separated by commas.
* <code>services.xserver.xkbVariant</code>: X keyboard variant
* <code>services.xserver.xkb.variant</code>: X keyboard variant or multiple variants separated by commas (a variant can be empty).
* <code>services.xserver.xkbModel</code>: Keyboard model.
* <code>services.xserver.xkb.model</code>: Keyboard model.
* <code>services.xserver.xkbOptions</code>: X keyboard options; layout switching goes here.
* <code>services.xserver.xkb.options</code>: X keyboard options; layout switching goes here.


You can find valid values for these options in <code>$(nix-build --no-out-link '&lt;nixpkgs&gt;' -A xorg.xkeyboardconfig)/etc/X11/xkb/rules/base.lst</code>
=====Example:=====


== Advanced ==
For desktop:
<syntaxhighlight lang="nix">
services.xserver.xkb = {
  layout = "us,ru";
  variant = "workman,";
  options = "grp:win_space_toggle";
};
</syntaxhighlight>
 
For console:
<syntaxhighlight lang="nix">
console.keyMap = "us";
</syntaxhighlight>
 
You can find valid values for these options in <code>$(nix-build --no-out-link '&lt;nixpkgs&gt;' -A xkeyboard_config)/etc/X11/xkb/rules/base.lst</code>
 
=== Advanced ===


If the above options aren't enough, you can instead create your own keyboard layout by going through xkb. To get started, install <code>xorg.xkbcomp</code> and run <code>setxkbmap -print &gt; layout.xkb</code> to get an initial file. This corresponds to your current layout. Use <code>xkbcomp layout.xkb $DISPLAY</code> to load the file as your new layout. Refer to https://wiki.archlinux.org/index.php/X_KeyBoard_extension on how to edit this file.
If the above options aren't enough, you can instead create your own keyboard layout by going through xkb. To get started, install <code>xorg.xkbcomp</code> and run <code>setxkbmap -print &gt; layout.xkb</code> to get an initial file. This corresponds to your current layout. Use <code>xkbcomp layout.xkb $DISPLAY</code> to load the file as your new layout. Refer to https://wiki.archlinux.org/index.php/X_KeyBoard_extension on how to edit this file.
Line 18: Line 38:
Lots of examples can be found in <code>$(nix-build --no-out-link '&lt;nixpkgs&gt;' -A xorg.xkeyboardconfig)/etc/X11/xkb/</code>. For available key symbols, see <code>$(nix-build --no-out-link '&lt;nixpkgs&gt;' -A xorg.xproto)/include/X11/keysymdef.h</code>.
Lots of examples can be found in <code>$(nix-build --no-out-link '&lt;nixpkgs&gt;' -A xorg.xkeyboardconfig)/etc/X11/xkb/</code>. For available key symbols, see <code>$(nix-build --no-out-link '&lt;nixpkgs&gt;' -A xorg.xproto)/include/X11/keysymdef.h</code>.


To load this file at the start of the X session, add this to your <code>configuration.nix</code>:
To load this file at the start of the X session, add the following to your <code>configuration.nix</code>. The extra compilation step (<code>xkbcomp</code>) helps catching layout errors at build time.


<source lang="nix">services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xkbcomp}/bin/xkbcomp ${/path/to/layout.xkb} $DISPLAY";
<syntaxhighlight lang="nix">
</source>
let
== Relevant other options ==
  compiledLayout = pkgs.runCommand "keyboard-layout" {} ''
    ${pkgs.xorg.xkbcomp}/bin/xkbcomp ${./path/to/layout.xkb} $out
  '';
in
  services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xkbcomp}/bin/xkbcomp ${compiledLayout} $DISPLAY";
</syntaxhighlight>
 
If you are using home-manager, you also need to prevent home-manager from managing the keyboard by having <code>home.keyboard = null;</code> in your home-manager configuration.
 
=== Relevant other options ===


* <code>services.xserver.exportConfiguration</code>: Makes it so the above mentioned xkb directory (and the <code>xorg.conf</code> file) gets exported to <code>/etc/X11/xkb</code>, which is useful if you have to often look stuff up in it.
* <code>services.xserver.exportConfiguration</code>: Makes it so the above mentioned xkb directory (and the <code>xorg.conf</code> file) gets exported to <code>/etc/X11/xkb</code>, which is useful if you have to often look stuff up in it.
* <code>services.xserver.xkbDir</code>: Allows you to set a different xkb directory altogether. All the above mentioned things will use this instead of the default one in regards to xkb stuff.
* <code>services.xserver.xkb.dir</code>: Allows you to set a different xkb directory altogether. All the above mentioned things will use this instead of the default one in regards to xkb stuff.
* <code>i18n.consoleUseXkbConfig</code>: Makes it so the tty console has about the same layout as the one configured in the <code>services.xserver</code> options.
* <code>console.useXkbConfig</code>: Makes it so the tty console has about the same layout as the one configured in the <code>services.xserver</code> options.
 
=== Configs ===
 
* https://github.com/infinisil/system/blob/94852ed690fccfdda27c2e3985be84c51f1eac8e/new-modules/keylayout.nix
 
=== Advanced configuration with xmodmap ===
 
Some users have found xmodmap to be a helpful tool although reports of successful implementation are varied.
 
<syntaxhighlight lang="nix">
 
cat /etc/nixos/configuration.nix
 
services.xserver.displayManager.sessionCommands =
  ${pkgs.xorg.xmodmap}/bin/xmodmap "${pkgs.writeText  "xkb-layout" ''
    ! Map umlauts to RIGHT ALT + <key>
      keycode 108 = Mode_switch
      keysym e = e E EuroSign
      keysym c = c C cent
      keysym a = a A adiaeresis Adiaeresis
      keysym o = o O odiaeresis Odiaeresis
      keysym u = u U udiaeresis Udiaeresis
      keysym s = s S ssharp
   
      ! disable capslock
      ! remove Lock = Caps_Lock
  ''}"
 
</syntaxhighlight>
 
Works after boot and after suspend/resume.
 
You may need to add some delay to make xmodmap command work.


== Configs ==
<syntaxhighlight lang="nix">
  services.xserver.displayManager.sessionCommands = "sleep 5 && ${pkgs.xorg.xmodmap}/bin/xmodmap -e 'keycode 43 = h H Left H' &";
</syntaxhighlight>


* https://github.com/Infinisil/system/blob/master/config/new-modules/keylayout.nix
[[Category:Desktop]]
[[Category:Hardware]]

Latest revision as of 18:35, 6 June 2024

20.09 and later

In 20.09 there's services.xserver.extraLayouts for this.

Using xkbcomp

Simple

The easiest way to customize your keyboard layout on NixOS is with these options:

  • services.xserver.xkb.layout: Keyboard layout, or multiple keyboard layouts separated by commas.
  • services.xserver.xkb.variant: X keyboard variant or multiple variants separated by commas (a variant can be empty).
  • services.xserver.xkb.model: Keyboard model.
  • services.xserver.xkb.options: X keyboard options; layout switching goes here.
Example:

For desktop:

services.xserver.xkb = {
  layout = "us,ru";
  variant = "workman,";
  options = "grp:win_space_toggle";
};

For console:

console.keyMap = "us";

You can find valid values for these options in $(nix-build --no-out-link '<nixpkgs>' -A xkeyboard_config)/etc/X11/xkb/rules/base.lst

Advanced

If the above options aren't enough, you can instead create your own keyboard layout by going through xkb. To get started, install xorg.xkbcomp and run setxkbmap -print > layout.xkb to get an initial file. This corresponds to your current layout. Use xkbcomp layout.xkb $DISPLAY to load the file as your new layout. Refer to https://wiki.archlinux.org/index.php/X_KeyBoard_extension on how to edit this file.

Lots of examples can be found in $(nix-build --no-out-link '<nixpkgs>' -A xorg.xkeyboardconfig)/etc/X11/xkb/. For available key symbols, see $(nix-build --no-out-link '<nixpkgs>' -A xorg.xproto)/include/X11/keysymdef.h.

To load this file at the start of the X session, add the following to your configuration.nix. The extra compilation step (xkbcomp) helps catching layout errors at build time.

let
  compiledLayout = pkgs.runCommand "keyboard-layout" {} ''
    ${pkgs.xorg.xkbcomp}/bin/xkbcomp ${./path/to/layout.xkb} $out
  '';
in
  services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xkbcomp}/bin/xkbcomp ${compiledLayout} $DISPLAY";

If you are using home-manager, you also need to prevent home-manager from managing the keyboard by having home.keyboard = null; in your home-manager configuration.

Relevant other options

  • services.xserver.exportConfiguration: Makes it so the above mentioned xkb directory (and the xorg.conf file) gets exported to /etc/X11/xkb, which is useful if you have to often look stuff up in it.
  • services.xserver.xkb.dir: Allows you to set a different xkb directory altogether. All the above mentioned things will use this instead of the default one in regards to xkb stuff.
  • console.useXkbConfig: Makes it so the tty console has about the same layout as the one configured in the services.xserver options.

Configs

Advanced configuration with xmodmap

Some users have found xmodmap to be a helpful tool although reports of successful implementation are varied.

cat /etc/nixos/configuration.nix

services.xserver.displayManager.sessionCommands =
  ${pkgs.xorg.xmodmap}/bin/xmodmap "${pkgs.writeText  "xkb-layout" ''
    ! Map umlauts to RIGHT ALT + <key>
      keycode 108 = Mode_switch
      keysym e = e E EuroSign
      keysym c = c C cent
      keysym a = a A adiaeresis Adiaeresis
      keysym o = o O odiaeresis Odiaeresis
      keysym u = u U udiaeresis Udiaeresis
      keysym s = s S ssharp
    
      ! disable capslock
      ! remove Lock = Caps_Lock
  ''}"

Works after boot and after suspend/resume.

You may need to add some delay to make xmodmap command work.

  services.xserver.displayManager.sessionCommands = "sleep 5 && ${pkgs.xorg.xmodmap}/bin/xmodmap -e 'keycode 43 = h H Left H' &";