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 = "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
82in
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 environment.sessionVariables = {
162 # runtime override supported by multiple libraries e. g. libxkbcommon
163 # https://xkbcommon.org/doc/current/group__include-path.html
164 XKB_CONFIG_ROOT = "${pkgs.xkb_patched}/etc/X11/xkb";
165 };
166
167 services.xserver = {
168 xkbDir = "${pkgs.xkb_patched}/etc/X11/xkb";
169 exportConfiguration = config.services.xserver.displayManager.startx.enable;
170 };
171
172 };
173
174}