1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let cfg = config.services.xserver.synaptics;
6 tapConfig = if cfg.tapButtons then enabledTapConfig else disabledTapConfig;
7 enabledTapConfig = ''
8 Option "MaxTapTime" "180"
9 Option "MaxTapMove" "220"
10 Option "TapButton1" "${builtins.elemAt cfg.fingersMap 0}"
11 Option "TapButton2" "${builtins.elemAt cfg.fingersMap 1}"
12 Option "TapButton3" "${builtins.elemAt cfg.fingersMap 2}"
13 '';
14 disabledTapConfig = ''
15 Option "MaxTapTime" "0"
16 Option "MaxTapMove" "0"
17 Option "TapButton1" "0"
18 Option "TapButton2" "0"
19 Option "TapButton3" "0"
20 '';
21 pkg = pkgs.xorg.xf86inputsynaptics;
22 etcFile = "X11/xorg.conf.d/50-synaptics.conf";
23in {
24
25 options = {
26
27 services.xserver.synaptics = {
28
29 enable = mkOption {
30 type = types.bool;
31 default = false;
32 example = true;
33 description = "Whether to enable touchpad support.";
34 };
35
36 dev = mkOption {
37 type = types.nullOr types.str;
38 default = null;
39 example = "/dev/input/event0";
40 description =
41 ''
42 Path for touchpad device. Set to null to apply to any
43 auto-detected touchpad.
44 '';
45 };
46
47 accelFactor = mkOption {
48 type = types.nullOr types.string;
49 default = "0.001";
50 description = "Cursor acceleration (how fast speed increases from minSpeed to maxSpeed).";
51 };
52
53 minSpeed = mkOption {
54 type = types.nullOr types.string;
55 default = "0.6";
56 description = "Cursor speed factor for precision finger motion.";
57 };
58
59 maxSpeed = mkOption {
60 type = types.nullOr types.string;
61 default = "1.0";
62 description = "Cursor speed factor for highest-speed finger motion.";
63 };
64
65 scrollDelta = mkOption {
66 type = types.nullOr types.int;
67 default = null;
68 example = 75;
69 description = "Move distance of the finger for a scroll event.";
70 };
71
72 twoFingerScroll = mkOption {
73 type = types.bool;
74 default = false;
75 description = "Whether to enable two-finger drag-scrolling. Overridden by horizTwoFingerScroll and vertTwoFingerScroll.";
76 };
77
78 horizTwoFingerScroll = mkOption {
79 type = types.bool;
80 default = cfg.twoFingerScroll;
81 description = "Whether to enable horizontal two-finger drag-scrolling.";
82 };
83
84 vertTwoFingerScroll = mkOption {
85 type = types.bool;
86 default = cfg.twoFingerScroll;
87 description = "Whether to enable vertical two-finger drag-scrolling.";
88 };
89
90 horizEdgeScroll = mkOption {
91 type = types.bool;
92 default = ! cfg.horizTwoFingerScroll;
93 description = "Whether to enable horizontal edge drag-scrolling.";
94 };
95
96 vertEdgeScroll = mkOption {
97 type = types.bool;
98 default = ! cfg.vertTwoFingerScroll;
99 description = "Whether to enable vertical edge drag-scrolling.";
100 };
101
102 tapButtons = mkOption {
103 type = types.bool;
104 default = true;
105 example = false;
106 description = "Whether to enable tap buttons.";
107 };
108
109 buttonsMap = mkOption {
110 type = types.listOf types.int;
111 default = [1 2 3];
112 example = [1 3 2];
113 description = "Remap touchpad buttons.";
114 apply = map toString;
115 };
116
117 fingersMap = mkOption {
118 type = types.listOf types.int;
119 default = [1 2 3];
120 example = [1 3 2];
121 description = "Remap several-fingers taps.";
122 apply = map toString;
123 };
124
125 palmDetect = mkOption {
126 type = types.bool;
127 default = false;
128 example = true;
129 description = "Whether to enable palm detection (hardware support required)";
130 };
131
132 palmMinWidth = mkOption {
133 type = types.nullOr types.int;
134 default = null;
135 example = 5;
136 description = "Minimum finger width at which touch is considered a palm";
137 };
138
139 palmMinZ = mkOption {
140 type = types.nullOr types.int;
141 default = null;
142 example = 20;
143 description = "Minimum finger pressure at which touch is considered a palm";
144 };
145
146 horizontalScroll = mkOption {
147 type = types.bool;
148 default = true;
149 example = false;
150 description = "Whether to enable horizontal scrolling (on touchpad)";
151 };
152
153 additionalOptions = mkOption {
154 type = types.str;
155 default = "";
156 example = ''
157 Option "RTCornerButton" "2"
158 Option "RBCornerButton" "3"
159 '';
160 description = ''
161 Additional options for synaptics touchpad driver.
162 '';
163 };
164
165 };
166
167 };
168
169
170 config = mkIf cfg.enable {
171
172 services.xserver.modules = [ pkg.out ];
173
174 environment.etc."${etcFile}".source =
175 "${pkg.out}/share/X11/xorg.conf.d/50-synaptics.conf";
176
177 environment.systemPackages = [ pkg ];
178
179 services.xserver.config =
180 ''
181 # Automatically enable the synaptics driver for all touchpads.
182 Section "InputClass"
183 Identifier "synaptics touchpad catchall"
184 MatchIsTouchpad "on"
185 ${optionalString (cfg.dev != null) ''MatchDevicePath "${cfg.dev}"''}
186 Driver "synaptics"
187 ${optionalString (cfg.minSpeed != null) ''Option "MinSpeed" "${cfg.minSpeed}"''}
188 ${optionalString (cfg.maxSpeed != null) ''Option "MaxSpeed" "${cfg.maxSpeed}"''}
189 ${optionalString (cfg.accelFactor != null) ''Option "AccelFactor" "${cfg.accelFactor}"''}
190 ${optionalString cfg.tapButtons tapConfig}
191 Option "ClickFinger1" "${builtins.elemAt cfg.buttonsMap 0}"
192 Option "ClickFinger2" "${builtins.elemAt cfg.buttonsMap 1}"
193 Option "ClickFinger3" "${builtins.elemAt cfg.buttonsMap 2}"
194 Option "VertTwoFingerScroll" "${if cfg.vertTwoFingerScroll then "1" else "0"}"
195 Option "HorizTwoFingerScroll" "${if cfg.horizTwoFingerScroll then "1" else "0"}"
196 Option "VertEdgeScroll" "${if cfg.vertEdgeScroll then "1" else "0"}"
197 Option "HorizEdgeScroll" "${if cfg.horizEdgeScroll then "1" else "0"}"
198 ${optionalString cfg.palmDetect ''Option "PalmDetect" "1"''}
199 ${optionalString (cfg.palmMinWidth != null) ''Option "PalmMinWidth" "${toString cfg.palmMinWidth}"''}
200 ${optionalString (cfg.palmMinZ != null) ''Option "PalmMinZ" "${toString cfg.palmMinZ}"''}
201 ${optionalString (cfg.scrollDelta != null) ''Option "VertScrollDelta" "${toString cfg.scrollDelta}"''}
202 ${if !cfg.horizontalScroll then ''Option "HorizScrollDelta" "0"''
203 else (optionalString (cfg.scrollDelta != null) ''Option "HorizScrollDelta" "${toString cfg.scrollDelta}"'')}
204 ${cfg.additionalOptions}
205 EndSection
206 '';
207
208 assertions = [
209 {
210 assertion = !config.services.xserver.libinput.enable;
211 message = "Synaptics and libinput are incompatible, you cannot enable both (in services.xserver).";
212 }
213 ];
214
215 };
216
217}