1 { config, lib, pkgs, ... }:
3 cfg = config.services.klipper;
4 format = pkgs.formats.ini {
5 # https://github.com/NixOS/nixpkgs/pull/121613#issuecomment-885241996
7 if builtins.length l == 1 then lib.generators.mkValueStringDefault { } (lib.head l)
8 else lib.concatMapStrings (s: "\n ${lib.generators.mkValueStringDefault {} s}") l;
9 mkKeyValue = lib.generators.mkKeyValueDefault { } ":";
16 enable = lib.mkEnableOption "Klipper, the 3D printer firmware";
18 package = lib.mkPackageOption pkgs "klipper" { };
20 logFile = lib.mkOption {
21 type = lib.types.nullOr lib.types.path;
23 example = "/var/lib/klipper/klipper.log";
25 Path of the file Klipper should log to.
26 If `null`, it logs to stdout, which is not recommended by upstream.
30 inputTTY = lib.mkOption {
31 type = lib.types.path;
32 default = "/run/klipper/tty";
33 description = "Path of the virtual printer symlink to create.";
36 apiSocket = lib.mkOption {
37 type = lib.types.nullOr lib.types.path;
38 default = "/run/klipper/api";
39 description = "Path of the API socket to create.";
42 mutableConfig = lib.mkOption {
43 type = lib.types.bool;
47 Whether to copy the config to a mutable directory instead of using the one directly from the nix store.
48 This will only copy the config if the file at `services.klipper.mutableConfigPath` doesn't exist.
52 mutableConfigFolder = lib.mkOption {
53 type = lib.types.path;
54 default = "/var/lib/klipper";
55 description = "Path to mutable Klipper config file.";
58 configFile = lib.mkOption {
59 type = lib.types.nullOr lib.types.path;
62 Path to default Klipper config.
66 octoprintIntegration = lib.mkOption {
67 type = lib.types.bool;
69 description = "Allows Octoprint to control Klipper.";
73 type = lib.types.nullOr lib.types.str;
76 User account under which Klipper runs.
78 If null is specified (default), a temporary user will be created by systemd.
82 group = lib.mkOption {
83 type = lib.types.nullOr lib.types.str;
86 Group account under which Klipper runs.
88 If null is specified (default), a temporary user will be created by systemd.
92 settings = lib.mkOption {
93 type = lib.types.nullOr format.type;
96 Configuration for Klipper. See the [documentation](https://www.klipper3d.org/Overview.html#configuration-and-tuning-guides)
101 firmwares = lib.mkOption {
102 description = "Firmwares klipper should manage";
104 type = with lib.types; attrsOf
107 enable = lib.mkEnableOption ''
108 building of firmware for manual flashing
110 enableKlipperFlash = lib.mkEnableOption ''
111 flashings scripts for firmware. This will add `klipper-flash-$mcu` scripts to your environment which can be called to flash the firmware.
112 Please check the configs at [klipper](https://github.com/Klipper3d/klipper/tree/master/config) whether your board supports flashing via `make flash`
114 serial = lib.mkOption {
115 type = lib.types.nullOr path;
117 description = "Path to serial port this printer is connected to. Leave `null` to derive it from `service.klipper.settings`.";
119 configFile = lib.mkOption {
121 description = "Path to firmware config which is generated using `klipper-genconf`";
130 config = lib.mkIf cfg.enable {
133 assertion = cfg.octoprintIntegration -> config.services.octoprint.enable;
134 message = "Option services.klipper.octoprintIntegration requires Octoprint to be enabled on this system. Please enable services.octoprint to use it.";
137 assertion = cfg.user != null -> cfg.group != null;
138 message = "Option services.klipper.group is not set when services.klipper.user is specified.";
141 assertion = cfg.settings != null -> lib.foldl (a: b: a && b) true (lib.mapAttrsToList (mcu: _: mcu != null -> (lib.hasAttrByPath [ "${mcu}" "serial" ] cfg.settings)) cfg.firmwares);
142 message = "Option services.klipper.settings.$mcu.serial must be set when settings.klipper.firmware.$mcu is specified";
145 assertion = (cfg.configFile != null) != (cfg.settings != null);
146 message = "You need to either specify services.klipper.settings or services.klipper.configFile.";
150 environment.etc = lib.mkIf (!cfg.mutableConfig) {
151 "klipper.cfg".source = if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile;
154 services.klipper = lib.mkIf cfg.octoprintIntegration {
155 user = config.services.octoprint.user;
156 group = config.services.octoprint.group;
159 systemd.services.klipper =
161 klippyArgs = "--input-tty=${cfg.inputTTY}"
162 + lib.optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}"
163 + lib.optionalString (cfg.logFile != null) " --logfile=${cfg.logFile}"
167 then cfg.mutableConfigFolder + "/printer.cfg"
168 else "/etc/klipper.cfg";
170 if cfg.settings != null
171 then format.generate "klipper.cfg" cfg.settings
175 description = "Klipper 3D Printer Firmware";
176 wantedBy = [ "multi-user.target" ];
177 after = [ "network.target" ];
179 mkdir -p ${cfg.mutableConfigFolder}
180 ${lib.optionalString (cfg.mutableConfig) ''
181 [ -e ${printerConfigPath} ] || {
182 cp ${printerConfigFile} ${printerConfigPath}
183 chmod +w ${printerConfigPath}
186 mkdir -p ${cfg.mutableConfigFolder}/gcodes
190 ExecStart = "${cfg.package}/bin/klippy ${klippyArgs} ${printerConfigPath}";
191 RuntimeDirectory = "klipper";
192 StateDirectory = "klipper";
193 SupplementaryGroups = [ "dialout" ];
194 WorkingDirectory = "${cfg.package}/lib";
195 OOMScoreAdjust = "-999";
196 CPUSchedulingPolicy = "rr";
197 CPUSchedulingPriority = 99;
198 IOSchedulingClass = "realtime";
199 IOSchedulingPriority = 0;
201 } // (if cfg.user != null then {
210 environment.systemPackages =
213 default = a: b: if a != null then a else b;
214 firmwares = lib.filterAttrs (n: v: v != null) (lib.mapAttrs
215 (mcu: { enable, enableKlipperFlash, configFile, serial }:
217 pkgs.klipper-firmware.override
219 mcu = lib.strings.sanitizeDerivationName mcu;
220 firmwareConfig = configFile;
223 firmwareFlasher = lib.mapAttrsToList
224 (mcu: firmware: pkgs.klipper-flash.override {
225 mcu = lib.strings.sanitizeDerivationName mcu;
226 klipper-firmware = firmware;
227 flashDevice = default cfg.firmwares."${mcu}".serial cfg.settings."${mcu}".serial;
228 firmwareConfig = cfg.firmwares."${mcu}".configFile;
230 (lib.filterAttrs (mcu: firmware: cfg.firmwares."${mcu}".enableKlipperFlash) firmwares);
232 [ klipper-genconf ] ++ firmwareFlasher ++ lib.attrValues firmwares;
235 lib.maintainers.cab404