git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/xserver: add option to install custom xkb layouts (#47764)

nixos/xserver: add option to install custom xkb layouts

commitd3dfe06c38d5849ffc628e59e1b36fd796367ae9 authorSilvan Mosberger <contact@infinisil.com> dateFri 26 Jul 2019, 20:43 (7 years ago) parent4ad627ee91f0c3
4 files changed+379−2
nixos/doc/manual/configuration/x-windows.xml+133−1 nixos/modules/module-list.nix+1 nixos/modules/services/x11/extra-layouts.nix+165 pkgs/servers/x11/xorg/overrides.nix+80−1
nixos/doc/manual/configuration/x-windows.xml+133−1
@@ -125,10 +125,12 @@
125125 You will need to reboot after enabling this driver to prevent a clash with
126126 other kernel modules.
127127 </para>
128+ <note>
128129 <para>
129− Note: for recent AMD GPUs you most likely want to keep either the defaults
130+ For recent AMD GPUs you most likely want to keep either the defaults
130131 or <literal>"amdgpu"</literal> (both free).
131132 </para>
133+ </note>
132134 </simplesect>
133135 <simplesect xml:id="sec-x11-touchpads">
134136 <title>Touchpads</title>
@@ -157,4 +159,134 @@
157159 versions.
158160 </para>
159161 </simplesect>
162+ <simplesect xml:id="custom-xkb-layouts">
163+ <title>Custom XKB layouts</title>
164+ <para>
165+ It is possible to install custom
166+ <link xlink:href="https://en.wikipedia.org/wiki/X_keyboard_extension">
167+ XKB
168+ </link>
169+ keyboard layouts using the option
170+ <option>
171+ <link linkend="opt-services.xserver.extraLayouts">
172+ services.xserver.extraLayouts
173+ </link>
174+ </option>.
175+ As a first example, we are going to create a layout based on the basic US
176+ layout, with an additional layer to type some greek symbols by pressing the
177+ right-alt key.
178+ </para>
179+ <para>
180+ To do this we are going to create a <literal>us-greek</literal> file
181+ with a <literal>xkb_symbols</literal> section.
182+ </para>
183+<programlisting>
184+xkb_symbols &quot;us-greek&quot;
185+{
186+ include &quot;us(basic)&quot; // includes the base US keys
187+ include &quot;level3(ralt_switch)&quot; // configures right alt as a third level switch
188+
189+ key &lt;LatA&gt; { [ a, A, Greek_alpha ] };
190+ key &lt;LatB&gt; { [ b, B, Greek_beta ] };
191+ key &lt;LatG&gt; { [ g, G, Greek_gamma ] };
192+ key &lt;LatD&gt; { [ d, D, Greek_delta ] };
193+ key &lt;LatZ&gt; { [ z, Z, Greek_zeta ] };
194+};
195+</programlisting>
196+ <para>
197+ To install the layout, the filepath, a description and the list of
198+ languages must be given:
199+ </para>
200+<programlisting>
201+<xref linkend="opt-services.xserver.extraLayouts"/>.us-greek = {
202+ description = "US layout with alt-gr greek";
203+ languages = [ "eng" ];
204+ symbolsFile = /path/to/us-greek;
205+}
206+</programlisting>
207+ <note>
208+ <para>
209+ The name should match the one given to the
210+ <literal>xkb_symbols</literal> block.
211+ </para>
212+ </note>
213+ <para>
214+ The layout should now be installed and ready to use: try it by
215+ running <literal>setxkbmap us-greek</literal> and type
216+ <literal>&lt;alt&gt;+a</literal>. To change the default the usual
217+ <option>
218+ <link linkend="opt-services.xserver.layout">
219+ services.xserver.layout
220+ </link>
221+ </option>
222+ option can still be used.
223+ </para>
224+ <para>
225+ A layout can have several other components besides
226+ <literal>xkb_symbols</literal>, for example we will define new
227+ keycodes for some multimedia key and bind these to some symbol.
228+ </para>
229+ <para>
230+ Use the <emphasis>xev</emphasis> utility from
231+ <literal>pkgs.xorg.xev</literal> to find the codes of the keys of
232+ interest, then create a <literal>media-key</literal> file to hold
233+ the keycodes definitions
234+ </para>
235+<programlisting>
236+xkb_keycodes &quot;media&quot;
237+{
238+ &lt;volUp&gt; = 123;
239+ &lt;volDown&gt; = 456;
240+}
241+</programlisting>
242+ <para>
243+ Now use the newly define keycodes in <literal>media-sym</literal>:
244+ </para>
245+<programlisting>
246+xkb_symbols &quot;media&quot;
247+{
248+ key.type = &quot;ONE_LEVEL&quot;;
249+ key &lt;volUp&gt; { [ XF86AudioLowerVolume ] };
250+ key &lt;volDown&gt; { [ XF86AudioRaiseVolume ] };
251+}
252+</programlisting>
253+ <para>
254+ As before, to install the layout do
255+ </para>
256+<programlisting>
257+<xref linkend="opt-services.xserver.extraLayouts"/>.media = {
258+ description = "Multimedia keys remapping";
259+ languages = [ "eng" ];
260+ symbolsFile = /path/to/media-key;
261+ keycodesFile = /path/to/media-sym;
262+};
263+</programlisting>
264+ <note>
265+ <para>
266+ The function <literal>pkgs.writeText &lt;filename&gt; &lt;content&gt;
267+ </literal> can be useful if you prefer to keep the layout definitions
268+ inside the NixOS configuration.
269+ </para>
270+ </note>
271+ <para>
272+ Unfortunately, the Xorg server does not (currently) support setting a
273+ keymap directly but relies instead on XKB rules to select the matching
274+ components (keycodes, types, ...) of a layout. This means that components
275+ other than symbols won't be loaded by default. As a workaround, you
276+ can set the keymap using <literal>setxkbmap</literal> at the start of the
277+ session with:
278+ </para>
279+<programlisting>
280+<xref linkend="opt-services.xserver.displayManager.sessionCommands"/> = "setxkbmap -keycodes media";
281+</programlisting>
282+ <para>
283+ To learn how to write layouts take a look at the XKB
284+ <link xlink:href="https://www.x.org/releases/current/doc/xorg-docs/input/XKB-Enhancing.html#Defining_New_Layouts">
285+ documentation
286+ </link>. More example layouts can also be found
287+ <link xlink:href="https://wiki.archlinux.org/index.php/X_KeyBoard_extension#Basic_examples">
288+ here
289+ </link>.
290+ </para>
291+</simplesect>
160292</chapter>
nixos/modules/module-list.nix+1
@@ -815,6 +815,7 @@
815815 ./services/web-servers/uwsgi.nix
816816 ./services/web-servers/varnish/default.nix
817817 ./services/web-servers/zope2.nix
818+ ./services/x11/extra-layouts.nix
818819 ./services/x11/colord.nix
819820 ./services/x11/compton.nix
820821 ./services/x11/unclutter.nix
nixos/modules/services/x11/extra-layouts.nixnew file+165
@@ -0,0 +1,165 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ layouts = config.services.xserver.extraLayouts;
7+
8+ layoutOpts = {
9+ options = {
10+ description = mkOption {
11+ type = types.str;
12+ description = "A short description of the layout.";
13+ };
14+
15+ languages = mkOption {
16+ type = types.listOf types.str;
17+ description =
18+ ''
19+ A list of languages provided by the layout.
20+ (Use ISO 639-2 codes, for example: "eng" for english)
21+ '';
22+ };
23+
24+ compatFile = mkOption {
25+ type = types.nullOr types.path;
26+ default = null;
27+ description = ''
28+ The path to the xkb compat file.
29+ This file sets the compatibility state, used to preserve
30+ compatibility with xkb-unaware programs.
31+ It must contain a <literal>xkb_compat "name" { ... }</literal> block.
32+ '';
33+ };
34+
35+ geometryFile = mkOption {
36+ type = types.nullOr types.path;
37+ default = null;
38+ description = ''
39+ The path to the xkb geometry file.
40+ This (completely optional) file describes the physical layout of
41+ keyboard, which maybe be used by programs to depict it.
42+ It must contain a <literal>xkb_geometry "name" { ... }</literal> block.
43+ '';
44+ };
45+
46+ keycodesFile = mkOption {
47+ type = types.nullOr types.path;
48+ default = null;
49+ description = ''
50+ The path to the xkb keycodes file.
51+ This file specifies the range and the interpretation of the raw
52+ keycodes sent by the keyboard.
53+ It must contain a <literal>xkb_keycodes "name" { ... }</literal> block.
54+ '';
55+ };
56+
57+ symbolsFile = mkOption {
58+ type = types.nullOr types.path;
59+ default = null;
60+ description = ''
61+ The path to the xkb symbols file.
62+ This is the most important file: it defines which symbol or action
63+ maps to each key and must contain a
64+ <literal>xkb_symbols "name" { ... }</literal> block.
65+ '';
66+ };
67+
68+ typesFile = mkOption {
69+ type = types.nullOr types.path;
70+ default = null;
71+ description = ''
72+ The path to the xkb types file.
73+ This file specifies the key types that can be associated with
74+ the various keyboard keys.
75+ It must contain a <literal>xkb_types "name" { ... }</literal> block.
76+ '';
77+ };
78+
79+ };
80+ };
81+
82+in
83+
84+{
85+
86+ ###### interface
87+
88+ options.services.xserver = {
89+ extraLayouts = mkOption {
90+ type = types.attrsOf (types.submodule layoutOpts);
91+ default = {};
92+ example = literalExample
93+ ''
94+ {
95+ mine = {
96+ description = "My custom xkb layout.";
97+ languages = [ "eng" ];
98+ symbolsFile = /path/to/my/layout;
99+ };
100+ }
101+ '';
102+ description = ''
103+ Extra custom layouts that will be included in the xkb configuration.
104+ Information on how to create a new layout can be found here:
105+ <link xlink:href="https://www.x.org/releases/current/doc/xorg-docs/input/XKB-Enhancing.html#Defining_New_Layouts"></link>.
106+ For more examples see
107+ <link xlink:href="https://wiki.archlinux.org/index.php/X_KeyBoard_extension#Basic_examples"></link>
108+ '';
109+ };
110+
111+ };
112+
113+ ###### implementation
114+
115+ config = mkIf (layouts != { }) {
116+
117+ # We don't override xkeyboard_config directly to
118+ # reduce the amount of packages to be recompiled.
119+ # Only the following packages are necessary to set
120+ # a custom layout anyway:
121+ nixpkgs.overlays = lib.singleton (self: super: {
122+
123+ xkb_patched = self.xorg.xkeyboardconfig_custom {
124+ layouts = config.services.xserver.extraLayouts;
125+ };
126+
127+ xorg = super.xorg // {
128+ xorgserver = super.xorg.xorgserver.overrideAttrs (old: {
129+ configureFlags = old.configureFlags ++ [
130+ "--with-xkb-bin-directory=${self.xorg.xkbcomp}/bin"
131+ "--with-xkb-path=${self.xkb_patched}/share/X11/xkb"
132+ ];
133+ });
134+
135+ setxkbmap = super.xorg.setxkbmap.overrideAttrs (old: {
136+ postInstall =
137+ ''
138+ mkdir -p $out/share
139+ ln -sfn ${self.xkb_patched}/etc/X11 $out/share/X11
140+ '';
141+ });
142+
143+ xkbcomp = super.xorg.xkbcomp.overrideAttrs (old: {
144+ configureFlags = "--with-xkb-config-root=${self.xkb_patched}/share/X11/xkb";
145+ });
146+
147+ };
148+
149+ ckbcomp = super.ckbcomp.override {
150+ xkeyboard_config = self.xkb_patched;
151+ };
152+
153+ xkbvalidate = super.xkbvalidate.override {
154+ libxkbcommon = self.libxkbcommon.override {
155+ xkeyboard_config = self.xkb_patched;
156+ };
157+ };
158+
159+ });
160+
161+ services.xserver.xkbDir = "${pkgs.xkb_patched}/etc/X11/xkb";
162+
163+ };
164+
165+}
pkgs/servers/x11/xorg/overrides.nix+80−1
@@ -1,7 +1,7 @@
11{ abiCompat ? null,
22 stdenv, makeWrapper, fetchurl, fetchpatch, buildPackages,
33 automake, autoconf, gettext, libiconv, libtool, intltool,
4− freetype, tradcpp, fontconfig, meson, ninja,
4+ freetype, tradcpp, fontconfig, meson, ninja, ed,
55 libGL, spice-protocol, zlib, libGLU, dbus, libunwind, libdrm,
66 mesa, udev, bootstrap_cmds, bison, flex, clangStdenv, autoreconfHook,
77 mcpp, epoxy, openssl, pkgconfig, llvm_6,
@@ -423,6 +423,85 @@ self: super:
423423 '';
424424 });
425425
426+ # xkeyboardconfig variant extensible with custom layouts.
427+ # See nixos/modules/services/x11/extra-layouts.nix
428+ xkeyboardconfig_custom = { layouts ? { } }:
429+ let
430+ patchIn = name: layout:
431+ with layout;
432+ with lib;
433+ ''
434+ # install layout files
435+ ${optionalString (compatFile != null) "cp '${compatFile}' 'compat/${name}'"}
436+ ${optionalString (geometryFile != null) "cp '${geometryFile}' 'geometry/${name}'"}
437+ ${optionalString (keycodesFile != null) "cp '${keycodesFile}' 'keycodes/${name}'"}
438+ ${optionalString (symbolsFile != null) "cp '${symbolsFile}' 'symbols/${name}'"}
439+ ${optionalString (typesFile != null) "cp '${typesFile}' 'types/${name}'"}
440+
441+ # patch makefiles
442+ for type in compat geometry keycodes symbols types; do
443+ if ! test -f "$type/${name}"; then
444+ continue
445+ fi
446+ test "$type" = geometry && type_name=geom || type_name=$type
447+ ${ed}/bin/ed -v $type/Makefile.am <<EOF
448+ /''${type_name}_DATA =
449+ a
450+ ${name} \\
451+ .
452+ w
453+ EOF
454+ ${ed}/bin/ed -v $type/Makefile.in <<EOF
455+ /''${type_name}_DATA =
456+ a
457+ ${name} \\
458+ .
459+ w
460+ EOF
461+ done
462+
463+ # add model description
464+ ${ed}/bin/ed -v rules/base.xml <<EOF
465+ /<\/modelList>
466+ -
467+ a
468+ <model>
469+ <configItem>
470+ <name>${name}</name>
471+ <_description>${layout.description}</_description>
472+ <vendor>${layout.description}</vendor>
473+ </configItem>
474+ </model>
475+ .
476+ w
477+ EOF
478+
479+ # add layout description
480+ ${ed}/bin/ed -v rules/base.xml <<EOF
481+ /<\/layoutList>
482+ -
483+ a
484+ <layout>
485+ <configItem>
486+ <name>${name}</name>
487+ <_shortDescription>${name}</_shortDescription>
488+ <_description>${layout.description}</_description>
489+ <languageList>
490+ ${concatMapStrings (lang: "<iso639Id>${lang}</iso639Id>\n") layout.languages}
491+ </languageList>
492+ </configItem>
493+ <variantList/>
494+ </layout>
495+ .
496+ w
497+ EOF
498+ '';
499+ in
500+ self.xkeyboardconfig.overrideAttrs (old: {
501+ buildInputs = old.buildInputs ++ [ automake ];
502+ postPatch = with lib; concatStrings (mapAttrsToList patchIn layouts);
503+ });
504+
426505 xload = super.xload.overrideAttrs (attrs: {
427506 nativeBuildInputs = attrs.nativeBuildInputs ++ [ gettext ];
428507 });