@@ -157,4 +157,134 @@
157157 versions.
158158 </para>
159159 </simplesect>
160+ <simplesect xml:id="custom-xkb-layouts">
161+ <title>Custom XKB layouts</title>
162+ <para>
163+ It is possible to install custom
164+ <link xlink:href="https://en.wikipedia.org/wiki/X_keyboard_extension">
165+ XKB
166+ </link>
167+ keyboard layouts using the option
168+ <option>
169+ <link linkend="opt-services.xserver.extraLayouts">
170+ services.xserver.extraLayouts
171+ </link>
172+ </option>.
173+ As a first example, we are going to create a layout based on the basic US
174+ layout, with an additional layer to type some greek symbols by pressing the
175+ right-alt key.
176+ </para>
177+ <para>
178+ To do this we are going to create a <literal>us-greek</literal> file
179+ with a <literal>xkb_symbols</literal> section.
180+ </para>
181+<programlisting>
182+xkb_symbols "us-greek"
183+{
184+ include "us(basic)" // includes the base US keys
185+ include "level3(ralt_switch)" // configures right alt as a third level switch
186+
187+ key <LatA> { [ a, A, Greek_alpha ] };
188+ key <LatB> { [ b, B, Greek_beta ] };
189+ key <LatG> { [ g, G, Greek_gamma ] };
190+ key <LatD> { [ d, D, Greek_delta ] };
191+ key <LatZ> { [ z, Z, Greek_zeta ] };
192+};
193+</programlisting>
194+ <para>
195+ To install the layout, the filepath, a description and the list of
196+ languages must be given:
197+ </para>
198+<programlisting>
199+<xref linkend="opt-services.xserver.extraLayouts"/>.us-greek = {
200+ description = "US layout with alt-gr greek";
201+ languages = [ "eng" ];
202+ symbolsFile = /path/to/us-greek;
203+}
204+</programlisting>
205+ <note>
206+ <para>
207+ The name should match the one given to the
208+ <literal>xkb_symbols</literal> block.
209+ </para>
210+ </note>
211+ <para>
212+ The layout should now be installed and ready to use: try it by
213+ running <literal>setxkbmap us-greek</literal> and type
214+ <literal><alt>+a</literal>. To change the default the usual
215+ <option>
216+ <link linkend="opt-services.xserver.layout">
217+ services.xserver.layout
218+ </link>
219+ </option>
220+ option can still be used.
221+ </para>
222+ <para>
223+ A layout can have several other components besides
224+ <literal>xkb_symbols</literal>, for example we will define new
225+ keycodes for some multimedia key and bind these to some symbol.
226+ </para>
227+ <para>
228+ Use the <emphasis>xev</emphasis> utility from
229+ <literal>pkgs.xorg.xev</literal> to find the codes of the keys of
230+ interest, then create a <literal>media-key</literal> file to hold
231+ the keycodes definitions
232+ </para>
233+<programlisting>
234+xkb_keycodes "media"
235+{
236+ <volUp> = 123;
237+ <volDown> = 456;
238+}
239+</programlisting>
240+ <para>
241+ Now use the newly define keycodes in <literal>media-sym</literal>:
242+ </para>
243+<programlisting>
244+xkb_symbols "media"
245+{
246+ key.type = "ONE_LEVEL";
247+ key <volUp> { [ XF86AudioLowerVolume ] };
248+ key <volDown> { [ XF86AudioRaiseVolume ] };
249+}
250+</programlisting>
251+ <para>
252+ As before, to install the layout do
253+ </para>
254+<programlisting>
255+<xref linkend="opt-services.xserver.extraLayouts"/>.media = {
256+ description = "Multimedia keys remapping";
257+ languages = [ "eng" ];
258+ symbolsFile = /path/to/media-key;
259+ keycodesFile = /path/to/media-sym;
260+};
261+</programlisting>
262+ <note>
263+ <para>
264+ The function <literal>pkgs.writeText <filename> <content>
265+ </literal> can be useful if you prefer to keep the layout definitions
266+ inside the NixOS configuration.
267+ </para>
268+ </note>
269+ <para>
270+ Unfortunately, the Xorg server does not (currently) support setting a
271+ keymap directly but relies instead on XKB rules to select the matching
272+ components (keycodes, types, ...) of a layout. This means that components
273+ other than symbols won't be loaded by default. As a workaround, you
274+ can set the keymap using <literal>setxkbmap</literal> at the start of the
275+ session with:
276+ </para>
277+<programlisting>
278+<xref linkend="opt-services.xserver.displayManager.sessionCommands"/> = "setxkbmap -keycodes media";
279+</programlisting>
280+ <para>
281+ To learn how to write layouts take a look at the XKB
282+ <link xlink:href="https://www.x.org/releases/current/doc/xorg-docs/input/XKB-Enhancing.html#Defining_New_Layouts">
283+ documentation
284+ </link>. More example layouts can also be found
285+ <link xlink:href="https://wiki.archlinux.org/index.php/X_KeyBoard_extension#Basic_examples">
286+ here
287+ </link>.
288+ </para>
289+</simplesect>
160290</chapter>