1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 layouts = config.services.xserver.extraLayouts;
7
8 layoutOpts = {
9 options = {
10 description = mkOption {
11 type = types.str;
12 description = lib.mdDoc "A short description of the layout.";
13 };
14
15 languages = mkOption {
16 type = types.listOf types.str;
17 description =
18 lib.mdDoc ''
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 = lib.mdDoc ''
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 `xkb_compat "name" { ... }` block.
32 '';
33 };
34
35 geometryFile = mkOption {
36 type = types.nullOr types.path;
37 default = null;
38 description = lib.mdDoc ''
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 `xkb_geometry "name" { ... }` block.
43 '';
44 };
45
46 keycodesFile = mkOption {
47 type = types.nullOr types.path;
48 default = null;
49 description = lib.mdDoc ''
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 `xkb_keycodes "name" { ... }` block.
54 '';
55 };
56
57 symbolsFile = mkOption {
58 type = types.nullOr types.path;
59 default = null;
60 description = lib.mdDoc ''
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 `xkb_symbols "name" { ... }` block.
65 '';
66 };
67
68 typesFile = mkOption {
69 type = types.nullOr types.path;
70 default = null;
71 description = lib.mdDoc ''
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 `xkb_types "name" { ... }` block.
76 '';
77 };
78
79 };
80 };
81
82 xkb_patched = pkgs.xorg.xkeyboardconfig_custom {
83 layouts = config.services.xserver.extraLayouts;
84 };
85
86in
87
88{
89
90 ###### interface
91
92 options.services.xserver = {
93 extraLayouts = mkOption {
94 type = types.attrsOf (types.submodule layoutOpts);
95 default = {};
96 example = literalExpression
97 ''
98 {
99 mine = {
100 description = "My custom xkb layout.";
101 languages = [ "eng" ];
102 symbolsFile = /path/to/my/layout;
103 };
104 }
105 '';
106 description = lib.mdDoc ''
107 Extra custom layouts that will be included in the xkb configuration.
108 Information on how to create a new layout can be found here:
109 <https://www.x.org/releases/current/doc/xorg-docs/input/XKB-Enhancing.html#Defining_New_Layouts>.
110 For more examples see
111 <https://wiki.archlinux.org/index.php/X_KeyBoard_extension#Basic_examples>
112 '';
113 };
114
115 };
116
117 ###### implementation
118
119 config = mkIf (layouts != { }) {
120
121 environment.sessionVariables = {
122 # runtime override supported by multiple libraries e. g. libxkbcommon
123 # https://xkbcommon.org/doc/current/group__include-path.html
124 XKB_CONFIG_ROOT = config.services.xserver.xkbDir;
125 };
126
127 services.xserver = {
128 xkbDir = "${xkb_patched}/etc/X11/xkb";
129 exportConfiguration = config.services.xserver.displayManager.startx.enable
130 || config.services.xserver.displayManager.sx.enable;
131 };
132
133 };
134
135}