diff --git a/modules/services/syncthing.nix b/modules/services/syncthing.nix index 839ec9b16..125b05f3c 100644 --- a/modules/services/syncthing.nix +++ b/modules/services/syncthing.nix @@ -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. ''; diff --git a/tests/modules/services/syncthing/extra-options.nix b/tests/modules/services/syncthing/extra-options.nix index 8f80b6aa4..e59b440ab 100644 --- a/tests/modules/services/syncthing/extra-options.nix +++ b/tests/modules/services/syncthing/extra-options.nix @@ -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} ''; }) diff --git a/tests/modules/services/syncthing/linux/default.nix b/tests/modules/services/syncthing/linux/default.nix index da4505040..cd3c7ab59 100644 --- a/tests/modules/services/syncthing/linux/default.nix +++ b/tests/modules/services/syncthing/linux/default.nix @@ -1,3 +1,4 @@ { + syncthing-gui-address-init = ./gui-address-init.nix; syncthing-tray = ./tray.nix; } diff --git a/tests/modules/services/syncthing/linux/gui-address-init.nix b/tests/modules/services/syncthing/linux/gui-address-init.nix new file mode 100644 index 000000000..24cbd9a2c --- /dev/null +++ b/tests/modules/services/syncthing/linux/gui-address-init.nix @@ -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" + ''; +}