|
18 | 18 | }; |
19 | 19 | in |
20 | 20 | { |
| 21 | + imports = [ |
| 22 | + (lib.mkRenamedOptionModule |
| 23 | + [ "services" "klipper" "mutableConfigFolder" ] |
| 24 | + [ "services" "klipper" "configDir" ] |
| 25 | + ) |
| 26 | + ]; |
| 27 | + |
21 | 28 | ##### interface |
22 | 29 | options = { |
23 | 30 | services.klipper = { |
|
52 | 59 | default = false; |
53 | 60 | example = true; |
54 | 61 | description = '' |
55 | | - Whether to copy the config to a mutable directory instead of using the one directly from the nix store. |
56 | | - This will only copy the config if the file at `services.klipper.mutableConfigPath` doesn't exist. |
| 62 | + Whether to manage the config outside of NixOS. |
| 63 | +
|
| 64 | + It will still be initialized with the defined NixOS config if the file doesn't already exist. |
57 | 65 | ''; |
58 | 66 | }; |
59 | 67 |
|
60 | | - mutableConfigFolder = lib.mkOption { |
| 68 | + configDir = lib.mkOption { |
61 | 69 | type = lib.types.path; |
62 | 70 | default = "/var/lib/klipper"; |
63 | | - description = "Path to mutable Klipper config file."; |
| 71 | + description = "Path to Klipper config file."; |
64 | 72 | }; |
65 | 73 |
|
66 | 74 | configFile = lib.mkOption { |
67 | 75 | type = lib.types.nullOr lib.types.path; |
68 | 76 | default = null; |
69 | | - description = '' |
70 | | - Path to default Klipper config. |
71 | | - ''; |
| 77 | + description = "Path to default Klipper config."; |
72 | 78 | }; |
73 | 79 |
|
74 | 80 | octoprintIntegration = lib.mkOption { |
|
162 | 168 | } |
163 | 169 | ]; |
164 | 170 |
|
165 | | - environment.etc = lib.mkIf (!cfg.mutableConfig) { |
166 | | - "klipper.cfg".source = |
167 | | - if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile; |
168 | | - }; |
169 | | - |
170 | 171 | services.klipper = lib.mkIf cfg.octoprintIntegration { |
171 | 172 | user = config.services.octoprint.user; |
172 | 173 | group = config.services.octoprint.group; |
|
178 | 179 | "--input-tty=${cfg.inputTTY}" |
179 | 180 | + lib.optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}" |
180 | 181 | + lib.optionalString (cfg.logFile != null) " --logfile=${cfg.logFile}"; |
181 | | - printerConfigPath = |
182 | | - if cfg.mutableConfig then cfg.mutableConfigFolder + "/printer.cfg" else "/etc/klipper.cfg"; |
183 | | - printerConfigFile = |
| 182 | + printerConfig = |
184 | 183 | if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile; |
185 | 184 | in |
186 | 185 | { |
187 | 186 | description = "Klipper 3D Printer Firmware"; |
188 | 187 | wantedBy = [ "multi-user.target" ]; |
189 | 188 | after = [ "network.target" ]; |
190 | 189 | preStart = '' |
191 | | - mkdir -p ${cfg.mutableConfigFolder} |
192 | | - ${lib.optionalString (cfg.mutableConfig) '' |
193 | | - [ -e ${printerConfigPath} ] || { |
194 | | - cp ${printerConfigFile} ${printerConfigPath} |
195 | | - chmod +w ${printerConfigPath} |
| 190 | + mkdir -p ${cfg.configDir} |
| 191 | + pushd ${cfg.configDir} |
| 192 | + if [ -e printer.cfg ]; then |
| 193 | + ${ |
| 194 | + if cfg.mutableConfig then |
| 195 | + ":" |
| 196 | + else |
| 197 | + '' |
| 198 | + # Backup existing config using the same date format klipper uses for SAVE_CONFIG |
| 199 | + old_config="printer-$(date +"%Y%m%d_%H%M%S").cfg" |
| 200 | + mv printer.cfg "$old_config" |
| 201 | + # Preserve SAVE_CONFIG section from the existing config |
| 202 | + cat ${printerConfig} <(printf "\n") <(sed -n '/#*# <---------------------- SAVE_CONFIG ---------------------->/,$p' "$old_config") > printer.cfg |
| 203 | + ${pkgs.diffutils}/bin/cmp printer.cfg "$old_config" && rm "$old_config" |
| 204 | + '' |
196 | 205 | } |
197 | | - ''} |
198 | | - mkdir -p ${cfg.mutableConfigFolder}/gcodes |
| 206 | + else |
| 207 | + cat ${printerConfig} > printer.cfg |
| 208 | + fi |
| 209 | + popd |
199 | 210 | ''; |
200 | 211 |
|
201 | 212 | serviceConfig = |
202 | 213 | { |
203 | | - ExecStart = "${cfg.package}/bin/klippy ${klippyArgs} ${printerConfigPath}"; |
| 214 | + ExecStart = "${cfg.package}/bin/klippy ${klippyArgs} ${cfg.configDir}/printer.cfg"; |
204 | 215 | RuntimeDirectory = "klipper"; |
205 | 216 | StateDirectory = "klipper"; |
206 | 217 | SupplementaryGroups = [ "dialout" ]; |
|
0 commit comments