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