@@ -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 "us-greek"
185+{
186+ include "us(basic)" // includes the base US keys
187+ include "level3(ralt_switch)" // configures right alt as a third level switch
188+
189+ key <LatA> { [ a, A, Greek_alpha ] };
190+ key <LatB> { [ b, B, Greek_beta ] };
191+ key <LatG> { [ g, G, Greek_gamma ] };
192+ key <LatD> { [ d, D, Greek_delta ] };
193+ key <LatZ> { [ 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><alt>+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 "media"
237+{
238+ <volUp> = 123;
239+ <volDown> = 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 "media"
247+{
248+ key.type = "ONE_LEVEL";
249+ key <volUp> { [ XF86AudioLowerVolume ] };
250+ key <volDown> { [ 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 <filename> <content>
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>