syncthing: avoid init for default gui address

The gui address option always has a default value, so #8644 ended up
making syncthing-init run for every enabled Syncthing setup.

Treat the default gui address as unset for updater purposes so the init
unit is only generated when Home Manager is actually managing Syncthing
configuration. Add regression tests for the default and explicit
guiAddress cases.
This commit is contained in:
Austin Horstman
2026-04-20 08:22:51 -05:00
parent e82d4a4ecd
commit 4883af6edb
4 changed files with 27 additions and 3 deletions

View File

@@ -25,6 +25,10 @@ let
else
"\${XDG_STATE_HOME:-$HOME/.local/state}/syncthing";
defaultGuiAddress = "127.0.0.1:8384";
hasCustomGuiAddress = cfg.guiAddress != defaultGuiAddress;
# Syncthing supports serving the GUI over Unix sockets. If that happens, the
# API is served over the Unix socket as well. This function returns the correct
# curl arguments for the address portion of the curl command for both network
@@ -288,7 +292,7 @@ let
''))
(lib.concatStringsSep "\n")
])
+ lib.optionalString (cfg.guiAddress != null) ''
+ lib.optionalString hasCustomGuiAddress ''
curl -X PATCH -d '{"address": "'${cfg.guiAddress}'"}' ${curlAddressArgs "/rest/config/gui"}
''
+ ''
@@ -300,7 +304,7 @@ let
''
);
doUpdateConfig = cleanedConfig != { } || cfg.guiCredentials != null || cfg.guiAddress != null;
doUpdateConfig = cleanedConfig != { } || cfg.guiCredentials != null || hasCustomGuiAddress;
defaultSyncthingArgs = [
"${syncthing}"
@@ -771,7 +775,7 @@ in
guiAddress = mkOption {
type = types.str;
default = "127.0.0.1:8384";
default = defaultGuiAddress;
description = ''
The address to serve the web interface at.
'';

View File

@@ -18,6 +18,8 @@ lib.mkMerge [
(lib.mkIf pkgs.stdenv.isLinux {
nmt.script = ''
assertFileExists home-files/.config/systemd/user/syncthing.service
assertPathNotExists home-files/.config/systemd/user/syncthing-init.service
assertPathNotExists home-files/.config/systemd/user/default.target.wants/syncthing-init.service
assertFileContains home-files/.config/systemd/user/syncthing.service \
"ExecStart=@syncthing@/bin/syncthing serve --no-browser --no-restart --no-upgrade '--gui-address=127.0.0.1:8384' -foo '-bar \"baz\"'"
'';
@@ -27,6 +29,7 @@ lib.mkMerge [
nmt.script = ''
serviceFile=LaunchAgents/org.nix-community.home.syncthing.plist
assertFileExists "$serviceFile"
assertPathNotExists LaunchAgents/org.nix-community.home.syncthing-init.plist
assertFileContent "$serviceFile" ${./expected-agent.plist}
'';
})

View File

@@ -1,3 +1,4 @@
{
syncthing-gui-address-init = ./gui-address-init.nix;
syncthing-tray = ./tray.nix;
}

View File

@@ -0,0 +1,16 @@
{
services.syncthing = {
enable = true;
guiAddress = "127.0.0.1:8385";
};
nmt.script = ''
serviceFile=home-files/.config/systemd/user/syncthing-init.service
assertFileExists "$serviceFile"
assertFileExists home-files/.config/systemd/user/default.target.wants/syncthing-init.service
assertFileContains "$serviceFile" "ExecStart="
updateScript=$(grep -o '/nix/store/[^ ]*-merge-syncthing-config' "$TESTED/$serviceFile")
assertFileContains "$updateScript" "127.0.0.1:8385/rest/config/gui"
'';
}