Skip to content

Commit 809f978

Browse files
kira-bruneaucab404
authored andcommitted
nixos/klipper: preserve SAVE_CONFIG for nixos-managed config
Klipper macros that use `SAVE_CONFIG` (eg. bed mesh calibration, PID tuning, ...) don't currently work with a nixos-managed config. This can be worked around by using `services.klipper.mutableConfig = true`, but then you lose out on being able to configure klipper from NixOS. This changes the default behaviour so that: 1. The config is stored in `services.klipper.configDir` instead of `/etc` 2. The config is copied instead of symlinked (keeping a timestamped backup of the existing config) 3. The `SAVE_CONFIG` section from the backup is copied over into the new config 4. The backup is deleted if the final config is identical
1 parent 7093d69 commit 809f978

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

nixos/modules/services/misc/klipper.nix

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ let
1818
};
1919
in
2020
{
21+
imports = [
22+
(lib.mkRenamedOptionModule
23+
[ "services" "klipper" "mutableConfigFolder" ]
24+
[ "services" "klipper" "configDir" ]
25+
)
26+
];
27+
2128
##### interface
2229
options = {
2330
services.klipper = {
@@ -52,23 +59,22 @@ in
5259
default = false;
5360
example = true;
5461
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.
5765
'';
5866
};
5967

60-
mutableConfigFolder = lib.mkOption {
68+
configDir = lib.mkOption {
6169
type = lib.types.path;
6270
default = "/var/lib/klipper";
63-
description = "Path to mutable Klipper config file.";
71+
description = "Path to Klipper config file.";
6472
};
6573

6674
configFile = lib.mkOption {
6775
type = lib.types.nullOr lib.types.path;
6876
default = null;
69-
description = ''
70-
Path to default Klipper config.
71-
'';
77+
description = "Path to default Klipper config.";
7278
};
7379

7480
octoprintIntegration = lib.mkOption {
@@ -162,11 +168,6 @@ in
162168
}
163169
];
164170

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-
170171
services.klipper = lib.mkIf cfg.octoprintIntegration {
171172
user = config.services.octoprint.user;
172173
group = config.services.octoprint.group;
@@ -178,29 +179,39 @@ in
178179
"--input-tty=${cfg.inputTTY}"
179180
+ lib.optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}"
180181
+ 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 =
184183
if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile;
185184
in
186185
{
187186
description = "Klipper 3D Printer Firmware";
188187
wantedBy = [ "multi-user.target" ];
189188
after = [ "network.target" ];
190189
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+
''
196205
}
197-
''}
198-
mkdir -p ${cfg.mutableConfigFolder}/gcodes
206+
else
207+
cat ${printerConfig} > printer.cfg
208+
fi
209+
popd
199210
'';
200211

201212
serviceConfig =
202213
{
203-
ExecStart = "${cfg.package}/bin/klippy ${klippyArgs} ${printerConfigPath}";
214+
ExecStart = "${cfg.package}/bin/klippy ${klippyArgs} ${cfg.configDir}/printer.cfg";
204215
RuntimeDirectory = "klipper";
205216
StateDirectory = "klipper";
206217
SupplementaryGroups = [ "dialout" ];

0 commit comments

Comments
 (0)