1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let cfg = config.services.xserver.libinput;
6
7 xorgBool = v: if v then "on" else "off";
8
9 mkConfigForDevice = deviceType: {
10 dev = mkOption {
11 type = types.nullOr types.str;
12 default = null;
13 example = "/dev/input/event0";
14 description =
15 lib.mdDoc ''
16 Path for ${deviceType} device. Set to `null` to apply to any
17 auto-detected ${deviceType}.
18 '';
19 };
20
21 accelProfile = mkOption {
22 type = types.enum [ "flat" "adaptive" ];
23 default = "adaptive";
24 example = "flat";
25 description =
26 lib.mdDoc ''
27 Sets the pointer acceleration profile to the given profile.
28 Permitted values are `adaptive`, `flat`.
29 Not all devices support this option or all profiles.
30 If a profile is unsupported, the default profile for this is used.
31 `flat`: Pointer motion is accelerated by a constant
32 (device-specific) factor, depending on the current speed.
33 `adaptive`: Pointer acceleration depends on the input speed.
34 This is the default profile for most devices.
35 '';
36 };
37
38 accelSpeed = mkOption {
39 type = types.nullOr types.str;
40 default = null;
41 example = "-0.5";
42 description = lib.mdDoc "Cursor acceleration (how fast speed increases from minSpeed to maxSpeed).";
43 };
44
45 buttonMapping = mkOption {
46 type = types.nullOr types.str;
47 default = null;
48 example = "1 6 3 4 5 0 7";
49 description =
50 lib.mdDoc ''
51 Sets the logical button mapping for this device, see XSetPointerMapping(3). The string must
52 be a space-separated list of button mappings in the order of the logical buttons on the
53 device, starting with button 1. The default mapping is "1 2 3 ... 32". A mapping of 0 deac‐
54 tivates the button. Multiple buttons can have the same mapping. Invalid mapping strings are
55 discarded and the default mapping is used for all buttons. Buttons not specified in the
56 user's mapping use the default mapping. See section BUTTON MAPPING for more details.
57 '';
58 };
59
60 calibrationMatrix = mkOption {
61 type = types.nullOr types.str;
62 default = null;
63 example = "0.5 0 0 0 0.8 0.1 0 0 1";
64 description =
65 lib.mdDoc ''
66 A string of 9 space-separated floating point numbers. Sets the calibration matrix to the
67 3x3 matrix where the first row is (abc), the second row is (def) and the third row is (ghi).
68 '';
69 };
70
71 clickMethod = mkOption {
72 type = types.nullOr (types.enum [ "none" "buttonareas" "clickfinger" ]);
73 default = null;
74 example = "buttonareas";
75 description =
76 lib.mdDoc ''
77 Enables a click method. Permitted values are `none`,
78 `buttonareas`, `clickfinger`.
79 Not all devices support all methods, if an option is unsupported,
80 the default click method for this device is used.
81 '';
82 };
83
84 leftHanded = mkOption {
85 type = types.bool;
86 default = false;
87 description = lib.mdDoc "Enables left-handed button orientation, i.e. swapping left and right buttons.";
88 };
89
90 middleEmulation = mkOption {
91 type = types.bool;
92 default = true;
93 description =
94 lib.mdDoc ''
95 Enables middle button emulation. When enabled, pressing the left and right buttons
96 simultaneously produces a middle mouse button click.
97 '';
98 };
99
100 naturalScrolling = mkOption {
101 type = types.bool;
102 default = false;
103 description = lib.mdDoc "Enables or disables natural scrolling behavior.";
104 };
105
106 scrollButton = mkOption {
107 type = types.nullOr types.int;
108 default = null;
109 example = 1;
110 description =
111 lib.mdDoc ''
112 Designates a button as scroll button. If the ScrollMethod is button and the button is logically
113 held down, x/y axis movement is converted into scroll events.
114 '';
115 };
116
117 scrollMethod = mkOption {
118 type = types.enum [ "twofinger" "edge" "button" "none" ];
119 default = "twofinger";
120 example = "edge";
121 description =
122 lib.mdDoc ''
123 Specify the scrolling method: `twofinger`, `edge`,
124 `button`, or `none`
125 '';
126 };
127
128 horizontalScrolling = mkOption {
129 type = types.bool;
130 default = true;
131 description =
132 lib.mdDoc ''
133 Disables horizontal scrolling. When disabled, this driver will discard any horizontal scroll
134 events from libinput. Note that this does not disable horizontal scrolling, it merely
135 discards the horizontal axis from any scroll events.
136 '';
137 };
138
139 sendEventsMode = mkOption {
140 type = types.enum [ "disabled" "enabled" "disabled-on-external-mouse" ];
141 default = "enabled";
142 example = "disabled";
143 description =
144 lib.mdDoc ''
145 Sets the send events mode to `disabled`, `enabled`,
146 or `disabled-on-external-mouse`
147 '';
148 };
149
150 tapping = mkOption {
151 type = types.bool;
152 default = true;
153 description =
154 lib.mdDoc ''
155 Enables or disables tap-to-click behavior.
156 '';
157 };
158
159 tappingButtonMap = mkOption {
160 type = types.nullOr (types.enum [ "lrm" "lmr" ]);
161 default = null;
162 description = lib.mdDoc ''
163 Set the button mapping for 1/2/3-finger taps to left/right/middle or left/middle/right, respectively.
164 '';
165 };
166
167 tappingDragLock = mkOption {
168 type = types.bool;
169 default = true;
170 description =
171 lib.mdDoc ''
172 Enables or disables drag lock during tapping behavior. When enabled, a finger up during tap-
173 and-drag will not immediately release the button. If the finger is set down again within the
174 timeout, the draging process continues.
175 '';
176 };
177
178 transformationMatrix = mkOption {
179 type = types.nullOr types.str;
180 default = null;
181 example = "0.5 0 0 0 0.8 0.1 0 0 1";
182 description = lib.mdDoc ''
183 A string of 9 space-separated floating point numbers. Sets the transformation matrix to
184 the 3x3 matrix where the first row is (abc), the second row is (def) and the third row is (ghi).
185 '';
186 };
187
188 disableWhileTyping = mkOption {
189 type = types.bool;
190 default = false;
191 description =
192 lib.mdDoc ''
193 Disable input method while typing.
194 '';
195 };
196
197 additionalOptions = mkOption {
198 type = types.lines;
199 default = "";
200 example =
201 ''
202 Option "DragLockButtons" "L1 B1 L2 B2"
203 '';
204 description = lib.mdDoc ''
205 Additional options for libinput ${deviceType} driver. See
206 {manpage}`libinput(4)`
207 for available options.";
208 '';
209 };
210 };
211
212 mkX11ConfigForDevice = deviceType: matchIs: ''
213 Identifier "libinput ${deviceType} configuration"
214 MatchDriver "libinput"
215 MatchIs${matchIs} "${xorgBool true}"
216 ${optionalString (cfg.${deviceType}.dev != null) ''MatchDevicePath "${cfg.${deviceType}.dev}"''}
217 Option "AccelProfile" "${cfg.${deviceType}.accelProfile}"
218 ${optionalString (cfg.${deviceType}.accelSpeed != null) ''Option "AccelSpeed" "${cfg.${deviceType}.accelSpeed}"''}
219 ${optionalString (cfg.${deviceType}.buttonMapping != null) ''Option "ButtonMapping" "${cfg.${deviceType}.buttonMapping}"''}
220 ${optionalString (cfg.${deviceType}.calibrationMatrix != null) ''Option "CalibrationMatrix" "${cfg.${deviceType}.calibrationMatrix}"''}
221 ${optionalString (cfg.${deviceType}.transformationMatrix != null) ''Option "TransformationMatrix" "${cfg.${deviceType}.transformationMatrix}"''}
222 ${optionalString (cfg.${deviceType}.clickMethod != null) ''Option "ClickMethod" "${cfg.${deviceType}.clickMethod}"''}
223 Option "LeftHanded" "${xorgBool cfg.${deviceType}.leftHanded}"
224 Option "MiddleEmulation" "${xorgBool cfg.${deviceType}.middleEmulation}"
225 Option "NaturalScrolling" "${xorgBool cfg.${deviceType}.naturalScrolling}"
226 ${optionalString (cfg.${deviceType}.scrollButton != null) ''Option "ScrollButton" "${toString cfg.${deviceType}.scrollButton}"''}
227 Option "ScrollMethod" "${cfg.${deviceType}.scrollMethod}"
228 Option "HorizontalScrolling" "${xorgBool cfg.${deviceType}.horizontalScrolling}"
229 Option "SendEventsMode" "${cfg.${deviceType}.sendEventsMode}"
230 Option "Tapping" "${xorgBool cfg.${deviceType}.tapping}"
231 ${optionalString (cfg.${deviceType}.tappingButtonMap != null) ''Option "TappingButtonMap" "${cfg.${deviceType}.tappingButtonMap}"''}
232 Option "TappingDragLock" "${xorgBool cfg.${deviceType}.tappingDragLock}"
233 Option "DisableWhileTyping" "${xorgBool cfg.${deviceType}.disableWhileTyping}"
234 ${cfg.${deviceType}.additionalOptions}
235 '';
236in {
237
238 imports =
239 (map (option: mkRenamedOptionModule ([ "services" "xserver" "libinput" option ]) [ "services" "xserver" "libinput" "touchpad" option ]) [
240 "accelProfile"
241 "accelSpeed"
242 "buttonMapping"
243 "calibrationMatrix"
244 "clickMethod"
245 "leftHanded"
246 "middleEmulation"
247 "naturalScrolling"
248 "scrollButton"
249 "scrollMethod"
250 "horizontalScrolling"
251 "sendEventsMode"
252 "tapping"
253 "tappingButtonMap"
254 "tappingDragLock"
255 "transformationMatrix"
256 "disableWhileTyping"
257 "additionalOptions"
258 ]);
259
260 options = {
261
262 services.xserver.libinput = {
263 enable = mkEnableOption (lib.mdDoc "libinput");
264 mouse = mkConfigForDevice "mouse";
265 touchpad = mkConfigForDevice "touchpad";
266 };
267 };
268
269
270 config = mkIf cfg.enable {
271
272 services.xserver.modules = [ pkgs.xorg.xf86inputlibinput ];
273
274 environment.systemPackages = [ pkgs.xorg.xf86inputlibinput ];
275
276 environment.etc =
277 let cfgPath = "X11/xorg.conf.d/40-libinput.conf";
278 in {
279 ${cfgPath} = {
280 source = pkgs.xorg.xf86inputlibinput.out + "/share/" + cfgPath;
281 };
282 };
283
284 services.udev.packages = [ pkgs.libinput.out ];
285
286 services.xserver.inputClassSections = [
287 (mkX11ConfigForDevice "mouse" "Pointer")
288 (mkX11ConfigForDevice "touchpad" "Touchpad")
289 ];
290
291 assertions = [
292 # already present in synaptics.nix
293 /* {
294 assertion = !config.services.xserver.synaptics.enable;
295 message = "Synaptics and libinput are incompatible, you cannot enable both (in services.xserver).";
296 } */
297 ];
298
299 };
300
301}