-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmodule.nix
More file actions
275 lines (232 loc) · 8.61 KB
/
module.nix
File metadata and controls
275 lines (232 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# NixOS module for maccel mouse acceleration driver
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.hardware.maccel;
# Extract version from PKGBUILD
pkgbuildContent = builtins.readFile ./PKGBUILD;
kernelModuleVersion = builtins.head (builtins.match ".*pkgver=([^[:space:]]+).*" pkgbuildContent);
# Extract version from cli/Cargo.toml
cliCargoToml = builtins.fromTOML (builtins.readFile ./cli/Cargo.toml);
cliVersion = cliCargoToml.package.version;
# Convert float to fixed-point integer (64-bit, 32 fractional bits)
fixedPointScale = 4294967296; # 2^32
toFixedPoint = value: builtins.floor (value * fixedPointScale + 0.5);
# Mode enum mapping (from driver/accel/mode.h)
modeMap = {
linear = 0;
natural = 1;
synchronous = 2;
no_accel = 3;
};
# Parameter mapping (from driver/params.h)
parameterMap = {
# Common parameters
SENS_MULT = cfg.parameters.sensMultiplier;
YX_RATIO = cfg.parameters.yxRatio;
INPUT_DPI = cfg.parameters.inputDpi;
ANGLE_ROTATION = cfg.parameters.angleRotation;
MODE = cfg.parameters.mode;
# Linear mode parameters
ACCEL = cfg.parameters.acceleration;
OFFSET = cfg.parameters.offset;
OUTPUT_CAP = cfg.parameters.outputCap;
# Natural mode parameters
DECAY_RATE = cfg.parameters.decayRate;
LIMIT = cfg.parameters.limit;
# Synchronous mode parameters
GAMMA = cfg.parameters.gamma;
SMOOTH = cfg.parameters.smooth;
MOTIVITY = cfg.parameters.motivity;
SYNC_SPEED = cfg.parameters.syncSpeed;
};
# Generate modprobe parameter string
kernelModuleParams = let
validParams = filterAttrs (_: v: v != null) parameterMap;
formatParam = name: value:
if name == "MODE"
then "${name}=${toString (modeMap.${value})}"
else "${name}=${toString (toFixedPoint value)}";
in
concatStringsSep " " (mapAttrsToList formatParam validParams);
# Build kernel module
maccel-kernel-module = config.boot.kernelPackages.callPackage ({
lib,
stdenv,
kernel,
}:
stdenv.mkDerivation rec {
pname = "maccel-dkms";
version = kernelModuleVersion;
src = ./.;
nativeBuildInputs = kernel.moduleBuildDependencies;
makeFlags =
[
"KVER=${kernel.modDirVersion}"
"KDIR=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
"DRIVER_CFLAGS=-DFIXEDPT_BITS=64"
]
++ optionals cfg.debug ["DRIVER_CFLAGS+=-g -DDEBUG"];
preBuild = "cd driver";
installPhase = ''
mkdir -p $out/lib/modules/${kernel.modDirVersion}/kernel/drivers/usb
cp maccel.ko $out/lib/modules/${kernel.modDirVersion}/kernel/drivers/usb/
'';
meta = with lib; {
description = "Mouse acceleration driver and kernel module for Linux.";
homepage = "https://www.maccel.org/";
license = licenses.gpl2Plus;
platforms = platforms.linux;
};
}) {};
# Optional CLI tools
maccel-cli = pkgs.rustPlatform.buildRustPackage rec {
pname = "maccel-cli";
version = cliVersion;
src = ./.;
cargoLock.lockFile = "${src}/Cargo.lock";
cargoBuildFlags = ["--bin" "maccel"];
meta = with lib; {
description = "CLI and TUI tools for configuring maccel.";
homepage = "https://www.maccel.org/";
license = licenses.gpl2Plus;
platforms = platforms.linux;
};
};
in {
options.hardware.maccel = {
enable = mkEnableOption "Enable maccel mouse acceleration driver (kernel module). Parameters must be configured via `hardware.maccel.parameters`.";
debug = mkOption {
type = types.bool;
default = false;
description = "Enable debug build of the kernel module.";
};
enableCli = mkOption {
type = types.bool;
default = false;
description = "Install CLI and TUI tools for real-time parameter tuning. You may add your user to the `maccel` group using `users.groups.maccel.members = [\"your_username\"];` to run maccel CLI/TUI without sudo. Note: Changes made via CLI/TUI are temporary and do not persist across reboots. Use this to discover optimal parameter values, then apply them permanently via `hardware.maccel.parameters`.";
};
parameters = {
# Common parameters
sensMultiplier = mkOption {
type = types.nullOr types.float;
default = null;
description = "Sensitivity multiplier applied after acceleration calculation.";
};
yxRatio = mkOption {
type = types.nullOr types.float;
default = null;
description = "Y/X ratio - factor by which Y-axis sensitivity is multiplied.";
};
inputDpi = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x > 0.0)
// {description = "positive float";});
default = null;
description = "DPI of the mouse, used to normalize effective DPI. Must be positive.";
};
angleRotation = mkOption {
type = types.nullOr types.float;
default = null;
description = "Apply rotation in degrees to mouse movement input.";
};
mode = mkOption {
type = types.nullOr (types.enum ["linear" "natural" "synchronous" "no_accel"]);
default = null;
description = "Acceleration mode.";
};
# Linear mode parameters
acceleration = mkOption {
type = types.nullOr types.float;
default = null;
description = "Linear acceleration factor.";
};
offset = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x >= 0.0)
// {description = "non-negative float";});
default = null;
description = "Input speed past which to allow acceleration. Cannot be negative.";
};
outputCap = mkOption {
type = types.nullOr types.float;
default = null;
description = "Maximum sensitivity multiplier cap.";
};
# Natural mode parameters
decayRate = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x > 0.0)
// {description = "positive float";});
default = null;
description = "Decay rate of the Natural acceleration curve. Must be positive.";
};
limit = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x >= 1.0)
// {description = "float >= 1.0";});
default = null;
description = "Limit of the Natural acceleration curve. Cannot be less than 1.";
};
# Synchronous mode parameters
gamma = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x > 0.0)
// {description = "positive float";});
default = null;
description = "Controls how fast you get from low to fast around the midpoint. Must be positive.";
};
smooth = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x >= 0.0 && x <= 1.0)
// {description = "float between 0.0 and 1.0";});
default = null;
description = "Controls the suddenness of the sensitivity increase. Must be between 0 and 1.";
};
motivity = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x > 1.0)
// {description = "float > 1.0";});
default = null;
description = "Sets max sensitivity while setting min to 1/motivity. Must be greater than 1.";
};
syncSpeed = mkOption {
type =
types.nullOr (types.addCheck types.float (x: x > 0.0)
// {description = "positive float";});
default = null;
description = "Sets the middle sensitivity between min and max sensitivity. Must be positive.";
};
};
};
config = mkIf cfg.enable {
# Add kernel module
boot.extraModulePackages = [maccel-kernel-module];
# Load module with parameters
boot.kernelModules = ["maccel"];
boot.extraModprobeConfig = mkIf (kernelModuleParams != "") ''
options maccel ${kernelModuleParams}
'';
# Create maccel group
users.groups.maccel = {};
# Install CLI tools if requested
environment.systemPackages = mkIf cfg.enableCli [maccel-cli];
# Create reset scripts directory
systemd.tmpfiles.rules = mkIf cfg.enableCli [
"d /var/opt/maccel/resets 0775 root maccel"
];
# Add udev rules
services.udev.extraRules = mkIf cfg.enableCli ''
# Set sysfs parameter permissions
ACTION=="add", SUBSYSTEM=="module", DEVPATH=="/module/maccel", \
RUN+="${pkgs.coreutils}/bin/chgrp -R maccel /sys/module/maccel/parameters"
# Set /dev/maccel character device permissions
ACTION=="add", KERNEL=="maccel", \
GROUP="maccel", MODE="0640"
'';
};
}