1{ config, lib, pkgs, ... }:
2with lib;
3let
4 cfg = config.services.klipper;
5 format = pkgs.formats.ini {
6 # https://github.com/NixOS/nixpkgs/pull/121613#issuecomment-885241996
7 listToValue = l:
8 if builtins.length l == 1 then generators.mkValueStringDefault { } (head l)
9 else lib.concatMapStrings (s: "\n ${generators.mkValueStringDefault {} s}") l;
10 mkKeyValue = generators.mkKeyValueDefault { } ":";
11 };
12in
13{
14 ##### interface
15 options = {
16 services.klipper = {
17 enable = mkEnableOption (lib.mdDoc "Klipper, the 3D printer firmware");
18
19 package = mkOption {
20 type = types.package;
21 default = pkgs.klipper;
22 defaultText = literalExpression "pkgs.klipper";
23 description = lib.mdDoc "The Klipper package.";
24 };
25
26 logFile = mkOption {
27 type = types.nullOr types.path;
28 default = null;
29 example = "/var/lib/klipper/klipper.log";
30 description = lib.mdDoc ''
31 Path of the file Klipper should log to.
32 If `null`, it logs to stdout, which is not recommended by upstream.
33 '';
34 };
35
36 inputTTY = mkOption {
37 type = types.path;
38 default = "/run/klipper/tty";
39 description = lib.mdDoc "Path of the virtual printer symlink to create.";
40 };
41
42 apiSocket = mkOption {
43 type = types.nullOr types.path;
44 default = "/run/klipper/api";
45 description = lib.mdDoc "Path of the API socket to create.";
46 };
47
48 mutableConfig = mkOption {
49 type = types.bool;
50 default = false;
51 example = true;
52 description = lib.mdDoc ''
53 Whether to copy the config to a mutable directory instead of using the one directly from the nix store.
54 This will only copy the config if the file at `services.klipper.mutableConfigPath` doesn't exist.
55 '';
56 };
57
58 mutableConfigFolder = mkOption {
59 type = types.path;
60 default = "/var/lib/klipper";
61 description = lib.mdDoc "Path to mutable Klipper config file.";
62 };
63
64 configFile = mkOption {
65 type = types.nullOr types.path;
66 default = null;
67 description = lib.mdDoc ''
68 Path to default Klipper config.
69 '';
70 };
71
72 octoprintIntegration = mkOption {
73 type = types.bool;
74 default = false;
75 description = lib.mdDoc "Allows Octoprint to control Klipper.";
76 };
77
78 user = mkOption {
79 type = types.nullOr types.str;
80 default = null;
81 description = lib.mdDoc ''
82 User account under which Klipper runs.
83
84 If null is specified (default), a temporary user will be created by systemd.
85 '';
86 };
87
88 group = mkOption {
89 type = types.nullOr types.str;
90 default = null;
91 description = lib.mdDoc ''
92 Group account under which Klipper runs.
93
94 If null is specified (default), a temporary user will be created by systemd.
95 '';
96 };
97
98 settings = mkOption {
99 type = types.nullOr format.type;
100 default = null;
101 description = lib.mdDoc ''
102 Configuration for Klipper. See the [documentation](https://www.klipper3d.org/Overview.html#configuration-and-tuning-guides)
103 for supported values.
104 '';
105 };
106
107 firmwares = mkOption {
108 description = lib.mdDoc "Firmwares klipper should manage";
109 default = { };
110 type = with types; attrsOf
111 (submodule {
112 options = {
113 enable = mkEnableOption (lib.mdDoc ''
114 building of firmware for manual flashing
115 '');
116 enableKlipperFlash = mkEnableOption (lib.mdDoc ''
117 flashings scripts for firmware. This will add `klipper-flash-$mcu` scripts to your environment which can be called to flash the firmware.
118 Please check the configs at [klipper](https://github.com/Klipper3d/klipper/tree/master/config) whether your board supports flashing via `make flash`
119 '');
120 serial = mkOption {
121 type = types.nullOr path;
122 description = lib.mdDoc "Path to serial port this printer is connected to. Leave `null` to derive it from `service.klipper.settings`.";
123 };
124 configFile = mkOption {
125 type = path;
126 description = lib.mdDoc "Path to firmware config which is generated using `klipper-genconf`";
127 };
128 };
129 });
130 };
131 };
132 };
133
134 ##### implementation
135 config = mkIf cfg.enable {
136 assertions = [
137 {
138 assertion = cfg.octoprintIntegration -> config.services.octoprint.enable;
139 message = "Option services.klipper.octoprintIntegration requires Octoprint to be enabled on this system. Please enable services.octoprint to use it.";
140 }
141 {
142 assertion = cfg.user != null -> cfg.group != null;
143 message = "Option services.klipper.group is not set when services.klipper.user is specified.";
144 }
145 {
146 assertion = cfg.settings != null -> foldl (a: b: a && b) true (mapAttrsToList (mcu: _: mcu != null -> (hasAttrByPath [ "${mcu}" "serial" ] cfg.settings)) cfg.firmwares);
147 message = "Option services.klipper.settings.$mcu.serial must be set when settings.klipper.firmware.$mcu is specified";
148 }
149 {
150 assertion = (cfg.configFile != null) != (cfg.settings != null);
151 message = "You need to either specify services.klipper.settings or services.klipper.configFile.";
152 }
153 ];
154
155 environment.etc = mkIf (!cfg.mutableConfig) {
156 "klipper.cfg".source = if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile;
157 };
158
159 services.klipper = mkIf cfg.octoprintIntegration {
160 user = config.services.octoprint.user;
161 group = config.services.octoprint.group;
162 };
163
164 systemd.services.klipper =
165 let
166 klippyArgs = "--input-tty=${cfg.inputTTY}"
167 + optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}"
168 + optionalString (cfg.logFile != null) " --logfile=${cfg.logFile}"
169 ;
170 printerConfigPath =
171 if cfg.mutableConfig
172 then cfg.mutableConfigFolder + "/printer.cfg"
173 else "/etc/klipper.cfg";
174 printerConfigFile =
175 if cfg.settings != null
176 then format.generate "klipper.cfg" cfg.settings
177 else cfg.configFile;
178 in
179 {
180 description = "Klipper 3D Printer Firmware";
181 wantedBy = [ "multi-user.target" ];
182 after = [ "network.target" ];
183 preStart = ''
184 mkdir -p ${cfg.mutableConfigFolder}
185 ${lib.optionalString (cfg.mutableConfig) ''
186 [ -e ${printerConfigPath} ] || {
187 cp ${printerConfigFile} ${printerConfigPath}
188 chmod +w ${printerConfigPath}
189 }
190 ''}
191 mkdir -p ${cfg.mutableConfigFolder}/gcodes
192 '';
193
194 serviceConfig = {
195 ExecStart = "${cfg.package}/bin/klippy ${klippyArgs} ${printerConfigPath}";
196 RuntimeDirectory = "klipper";
197 StateDirectory = "klipper";
198 SupplementaryGroups = [ "dialout" ];
199 WorkingDirectory = "${cfg.package}/lib";
200 OOMScoreAdjust = "-999";
201 CPUSchedulingPolicy = "rr";
202 CPUSchedulingPriority = 99;
203 IOSchedulingClass = "realtime";
204 IOSchedulingPriority = 0;
205 UMask = "0002";
206 } // (if cfg.user != null then {
207 Group = cfg.group;
208 User = cfg.user;
209 } else {
210 DynamicUser = true;
211 User = "klipper";
212 });
213 };
214
215 environment.systemPackages =
216 with pkgs;
217 let
218 default = a: b: if a != null then a else b;
219 firmwares = filterAttrs (n: v: v != null) (mapAttrs
220 (mcu: { enable, enableKlipperFlash, configFile, serial }:
221 if enable then
222 pkgs.klipper-firmware.override
223 {
224 mcu = lib.strings.sanitizeDerivationName mcu;
225 firmwareConfig = configFile;
226 } else null)
227 cfg.firmwares);
228 firmwareFlasher = mapAttrsToList
229 (mcu: firmware: pkgs.klipper-flash.override {
230 mcu = lib.strings.sanitizeDerivationName mcu;
231 klipper-firmware = firmware;
232 flashDevice = default cfg.firmwares."${mcu}".serial cfg.settings."${mcu}".serial;
233 firmwareConfig = cfg.firmwares."${mcu}".configFile;
234 })
235 (filterAttrs (mcu: firmware: cfg.firmwares."${mcu}".enableKlipperFlash) firmwares);
236 in
237 [ klipper-genconf ] ++ firmwareFlasher ++ attrValues firmwares;
238 };
239 meta.maintainers = [
240 maintainers.cab404
241 ];
242}