From cadfe449aa5cbcfe1543c921526bf0a751e94eb3 Mon Sep 17 00:00:00 2001 From: Peter Kling <1018801+pitkling@users.noreply.github.com> Date: Thu, 17 Apr 2025 09:48:48 +0200 Subject: [PATCH] syncthing: more reliable syncthing launchd agent Starts the syncthing-init launchd agent (responsible for updating the configuration) as a oneshot agent instead of via WatchPaths (the latter is too unreliable). Ideally, the syncthing-init agent should be started after the syncthing agent started the Syncthing server, since syncthing-init updates the configuration using API calls to the Syncthing server. The Linux systemd service versions handle this via the Requires and After attribute. Launchd under macOS is missing a reliable way to order agents. Theoretically, one can achieve similar things via, e.g., WatchPaths (used before this commit). But this is known to be very unreliable (see also my comment in issue #6542). Thus, we just start syncthing-init and rely on the wrapped curl bash function (see curlShellFunction in nix file) to wait for the Syncthing server to be up and running. --- modules/services/syncthing.nix | 55 +++++++++++++++------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/modules/services/syncthing.nix b/modules/services/syncthing.nix index a924caaf..1de24029 100644 --- a/modules/services/syncthing.nix +++ b/modules/services/syncthing.nix @@ -825,40 +825,33 @@ in }; }; - launchd.agents = - let - # agent `syncthing` uses `${syncthing_dir}/${watch_file}` to notify agent `syncthing-init` - watch_file = ".launchd_update_config"; - in - { - syncthing = { - enable = true; - config = { - ProgramArguments = [ - "${pkgs.writers.writeBash "syncthing-wrapper" '' - ${copyKeys} # simulate systemd's `syncthing-init.Service.ExecStartPre` - touch "${syncthing_dir}/${watch_file}" # notify syncthing-init agent - exec ${lib.escapeShellArgs syncthingArgs} - ''}" - ]; - KeepAlive = { - Crashed = true; - SuccessfulExit = false; - }; - ProcessType = "Background"; - }; - }; - - syncthing-init = { - enable = cleanedConfig != { }; - config = { - ProgramArguments = [ "${updateConfig}" ]; - WatchPaths = [ - "${config.home.homeDirectory}/Library/Application Support/Syncthing/${watch_file}" - ]; + launchd.agents = { + syncthing = { + enable = true; + config = { + ProgramArguments = [ + "${pkgs.writers.writeBash "syncthing-wrapper" '' + ${copyKeys} # simulate systemd's `syncthing-init.Service.ExecStartPre` + exec ${lib.escapeShellArgs syncthingArgs} + ''}" + ]; + KeepAlive = { + Crashed = true; + SuccessfulExit = false; }; + ProcessType = "Background"; }; }; + + syncthing-init = { + enable = cleanedConfig != { }; + config = { + ProgramArguments = [ "${updateConfig}" ]; + ProcessType = "Background"; + RunAtLoad = true; + }; + }; + }; }) (lib.mkIf cfg.tray.enable {