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