syncthing: support legacy config dir

Syncthing still supports existing Linux installations that keep config.xml under XDG_CONFIG_HOME/syncthing. Resolve the runtime directory from the generated scripts so Home Manager waits on and copies keys into the existing legacy directory when no state-dir config exists.

Fixes #6933
This commit is contained in:
Austin Horstman
2026-05-18 13:14:50 -05:00
committed by Robert Helgesson
parent 3ee415b292
commit f968ed5961
2 changed files with 28 additions and 5 deletions

View File

@@ -19,12 +19,29 @@ let
isUnixGui = (builtins.substring 0 1 cfg.guiAddress) == "/";
# syncthing's configuration directory (see https://docs.syncthing.net/users/config.html)
syncthing_dir =
syncthingDir =
if pkgs.stdenv.isDarwin then
"$HOME/Library/Application Support/Syncthing"
else
"\${XDG_STATE_HOME:-$HOME/.local/state}/syncthing";
syncthingDirShell =
if pkgs.stdenv.isDarwin then
''
syncthing_dir="${syncthingDir}"
''
else
''
syncthing_state_dir="${syncthingDir}"
syncthing_config_dir="''${XDG_CONFIG_HOME:-$HOME/.config}/syncthing"
if [[ -e "$syncthing_state_dir/config.xml" || ! -e "$syncthing_config_dir/config.xml" ]]; then
syncthing_dir="$syncthing_state_dir"
else
syncthing_dir="$syncthing_config_dir"
fi
'';
defaultGuiAddress = "127.0.0.1:8384";
hasCustomGuiAddress = cfg.guiAddress != defaultGuiAddress;
@@ -75,16 +92,20 @@ let
mkpasswd = lib.getExe pkgs.mkpasswd;
copyKeys = pkgs.writers.writeBash "syncthing-copy-keys" ''
${install} -dm700 "${syncthing_dir}"
${syncthingDirShell}
${install} -dm700 "$syncthing_dir"
${lib.optionalString (cfg.cert != null) ''
${install} -Dm400 ${toString cfg.cert} "${syncthing_dir}/cert.pem"
${install} -Dm400 ${toString cfg.cert} "$syncthing_dir/cert.pem"
''}
${lib.optionalString (cfg.key != null) ''
${install} -Dm400 ${toString cfg.key} "${syncthing_dir}/key.pem"
${install} -Dm400 ${toString cfg.key} "$syncthing_dir/key.pem"
''}
'';
curlShellFunction = ''
${syncthingDirShell}
# systemd sets and creates RUNTIME_DIRECTORY on Linux
# on Darwin, we create it manually via mktemp
RUNTIME_DIRECTORY="''${RUNTIME_DIRECTORY:=$(${mktemp} -d)}"
@@ -94,7 +115,7 @@ let
while
! ${pkgs.libxml2}/bin/xmllint \
--xpath 'string(configuration/gui/apikey)' \
"${syncthing_dir}/config.xml" \
"$syncthing_dir/config.xml" \
>"$RUNTIME_DIRECTORY/api_key"
do ${sleep} 1; done
(${printf} "X-API-Key: "; ${cat} "$RUNTIME_DIRECTORY/api_key") >"$RUNTIME_DIRECTORY/headers"

View File

@@ -12,5 +12,7 @@
updateScript=$(grep -o '/nix/store/[^ ]*-merge-syncthing-config' "$TESTED/$serviceFile")
assertFileContains "$updateScript" "127.0.0.1:8385/rest/config/gui"
assertFileContains "$updateScript" 'syncthing_config_dir="''${XDG_CONFIG_HOME:-$HOME/.config}/syncthing"'
assertFileContains "$updateScript" 'syncthing_dir="$syncthing_config_dir"'
'';
}