Keyboard Layout Customization: Difference between revisions

imported>Dmarcoux
Add line about home-manager for home.keyboard = null
Rhendric (talk | contribs)
Relevant other options: Update xkb options
 
(13 intermediate revisions by 12 users not shown)
Line 1: Line 1:
== 20.09 and later ==
In 20.09 there's [https://nixos.org/manual/nixos/stable/#custom-xkb-layouts services.xserver.extraLayouts] for this.
== Using xkbcomp ==
== Using xkbcomp ==


Line 5: Line 9:
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.
 
=====Example:=====
 
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>
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>
Line 20: Line 40:
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.
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">
<syntaxhighlight lang="nix">
let
let
   compiledLayout = pkgs.runCommand "keyboard-layout" {} ''
   compiledLayout = pkgs.runCommand "keyboard-layout" {} ''
Line 27: Line 47:
in
in
   services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xkbcomp}/bin/xkbcomp ${compiledLayout} $DISPLAY";
   services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xkbcomp}/bin/xkbcomp ${compiledLayout} $DISPLAY";
</source>
</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.
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.
Line 34: Line 54:


* <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 ===
=== Configs ===


* https://github.com/Infinisil/system/blob/master/config/new-modules/keylayout.nix
* https://github.com/infinisil/system/blob/94852ed690fccfdda27c2e3985be84c51f1eac8e/new-modules/keylayout.nix
 
=== Advanced configuration with xmodmap ===


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


Since it is much simpler to use xmodmap and also because I couldn|t get the xkbcomp way to work on my NixOS machine I decided to keep using xmodmap:
<syntaxhighlight lang="nix">


<source lang="nix">
cat /etc/nixos/configuration.nix
  cat /etc/nixos/configuration.nix
 
  ...
services.xserver.displayManager.sessionCommands =
   let
   ${pkgs.xorg.xmodmap}/bin/xmodmap "${pkgs.writeText "xkb-layout" ''
    myCustomLayout = pkgs.writeText "xkb-layout" ''
    ! Map umlauts to RIGHT ALT + <key>
      ! Map umlauts to RIGHT ALT + <key>
       keycode 108 = Mode_switch
       keycode 108 = Mode_switch
       keysym e = e E EuroSign
       keysym e = e E EuroSign
Line 61: Line 82:
       ! disable capslock
       ! disable capslock
       ! remove Lock = Caps_Lock
       ! remove Lock = Caps_Lock
    '';
  ''}"
  in
  ...
  services.xserver.displayManager.sessionCommands = "${pkgs.xorg.xmodmap}/bin/xmodmap ${myCustomLayout}";


</source>
</syntaxhighlight>


Works after boot and after suspend/resume.
Works after boot and after suspend/resume.
You may need to add some delay to make xmodmap command work.
<syntaxhighlight lang="nix">
  services.xserver.displayManager.sessionCommands = "sleep 5 && ${pkgs.xorg.xmodmap}/bin/xmodmap -e 'keycode 43 = h H Left H' &";
</syntaxhighlight>
[[Category:Desktop]]
[[Category:Hardware]]