diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 0c8496a9..f7c6a338 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -964,6 +964,14 @@ in A new module is available: 'services.copyq'. ''; } + + { + time = "2023-03-25T11:03:24+00:00"; + condition = hostPlatform.isDarwin; + message = '' + A new module is available: 'services.syncthing'. + ''; + } ]; }; } diff --git a/modules/services/syncthing.nix b/modules/services/syncthing.nix index bbe9f866..c847985f 100644 --- a/modules/services/syncthing.nix +++ b/modules/services/syncthing.nix @@ -6,6 +6,15 @@ let cfg = config.services.syncthing; + defaultSyncthingArgs = [ + "${pkgs.syncthing}/bin/syncthing" + "-no-browser" + "-no-restart" + "-logflags=0" + ]; + + syncthingArgs = defaultSyncthingArgs ++ cfg.extraOptions; + in { meta.maintainers = [ maintainers.rycee ]; @@ -57,11 +66,6 @@ in { config = mkMerge [ (mkIf cfg.enable { - assertions = [ - (lib.hm.assertions.assertPlatform "services.syncthing" pkgs - lib.platforms.linux) - ]; - home.packages = [ (getOutput "man" pkgs.syncthing) ]; systemd.user.services = { @@ -74,10 +78,7 @@ in { }; Service = { - ExecStart = - "${pkgs.syncthing}/bin/syncthing -no-browser -no-restart -logflags=0" - + optionalString (cfg.extraOptions != [ ]) - (" " + escapeShellArgs cfg.extraOptions); + ExecStart = escapeShellArgs syncthingArgs; Restart = "on-failure"; SuccessExitStatus = [ 3 4 ]; RestartForceExitStatus = [ 3 4 ]; @@ -95,6 +96,18 @@ in { Install = { WantedBy = [ "default.target" ]; }; }; }; + + launchd.agents.syncthing = { + enable = true; + config = { + ProgramArguments = syncthingArgs; + KeepAlive = { + Crashed = true; + SuccessfulExit = false; + }; + ProcessType = "Background"; + }; + }; }) (mkIf (isAttrs cfg.tray && cfg.tray.enable) { diff --git a/tests/default.nix b/tests/default.nix index 136dda54..f120d692 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -118,6 +118,7 @@ import nmt { ./modules/programs/sm64ex ./modules/programs/ssh ./modules/programs/starship + ./modules/services/syncthing/common ./modules/programs/taskwarrior ./modules/programs/texlive ./modules/programs/tmate @@ -202,7 +203,7 @@ import nmt { ./modules/services/screen-locker ./modules/services/swayidle ./modules/services/sxhkd - ./modules/services/syncthing + ./modules/services/syncthing/linux ./modules/services/trayer ./modules/services/twmn ./modules/services/udiskie diff --git a/tests/modules/services/syncthing/common/default.nix b/tests/modules/services/syncthing/common/default.nix new file mode 100644 index 00000000..2cdef6ff --- /dev/null +++ b/tests/modules/services/syncthing/common/default.nix @@ -0,0 +1 @@ +{ syncthing-extra-options = ./extra-options.nix; } diff --git a/tests/modules/services/syncthing/common/expected-agent.plist b/tests/modules/services/syncthing/common/expected-agent.plist new file mode 100644 index 00000000..3b9a28ff --- /dev/null +++ b/tests/modules/services/syncthing/common/expected-agent.plist @@ -0,0 +1,26 @@ + + + + + KeepAlive + + Crashed + + SuccessfulExit + + + Label + org.nix-community.home.syncthing + ProcessType + Background + ProgramArguments + + @syncthing@/bin/syncthing + -no-browser + -no-restart + -logflags=0 + -foo + -bar "baz" + + + \ No newline at end of file diff --git a/tests/modules/services/syncthing/common/extra-options.nix b/tests/modules/services/syncthing/common/extra-options.nix new file mode 100644 index 00000000..016850cc --- /dev/null +++ b/tests/modules/services/syncthing/common/extra-options.nix @@ -0,0 +1,26 @@ +{ lib, pkgs, ... }: + +lib.mkMerge [ + { + services.syncthing = { + enable = true; + extraOptions = [ "-foo" ''-bar "baz"'' ]; + }; + + test.stubs.syncthing = { }; + } + (lib.mkIf pkgs.stdenv.isLinux { + nmt.script = '' + assertFileExists home-files/.config/systemd/user/syncthing.service + assertFileContains home-files/.config/systemd/user/syncthing.service \ + "ExecStart='@syncthing@/bin/syncthing' '-no-browser' '-no-restart' '-logflags=0' '-foo' '-bar \"baz\"'" + ''; + }) + (lib.mkIf pkgs.stdenv.isDarwin { + nmt.script = '' + serviceFile=LaunchAgents/org.nix-community.home.syncthing.plist + assertFileExists "$serviceFile" + assertFileContent "$serviceFile" ${./expected-agent.plist} + ''; + }) +] diff --git a/tests/modules/services/syncthing/extra-options.nix b/tests/modules/services/syncthing/extra-options.nix deleted file mode 100644 index c78b00f9..00000000 --- a/tests/modules/services/syncthing/extra-options.nix +++ /dev/null @@ -1,16 +0,0 @@ -{ config, ... }: - -{ - services.syncthing = { - enable = true; - extraOptions = [ "-foo" ''-bar "baz"'' ]; - }; - - test.stubs.syncthing = { }; - - nmt.script = '' - assertFileExists home-files/.config/systemd/user/syncthing.service - assertFileContains home-files/.config/systemd/user/syncthing.service \ - "ExecStart=@syncthing@/bin/syncthing -no-browser -no-restart -logflags=0 '-foo' '-bar \"baz\"'" - ''; -} diff --git a/tests/modules/services/syncthing/default.nix b/tests/modules/services/syncthing/linux/default.nix similarity index 70% rename from tests/modules/services/syncthing/default.nix rename to tests/modules/services/syncthing/linux/default.nix index 86f72b57..14136fe6 100644 --- a/tests/modules/services/syncthing/default.nix +++ b/tests/modules/services/syncthing/linux/default.nix @@ -1,5 +1,4 @@ { - syncthing-extra-options = ./extra-options.nix; syncthing-tray = ./tray.nix; syncthing-tray-as-bool-triggers-warning = ./tray-as-bool-triggers-warning.nix; } diff --git a/tests/modules/services/syncthing/tray-as-bool-triggers-warning.nix b/tests/modules/services/syncthing/linux/tray-as-bool-triggers-warning.nix similarity index 100% rename from tests/modules/services/syncthing/tray-as-bool-triggers-warning.nix rename to tests/modules/services/syncthing/linux/tray-as-bool-triggers-warning.nix diff --git a/tests/modules/services/syncthing/tray.nix b/tests/modules/services/syncthing/linux/tray.nix similarity index 100% rename from tests/modules/services/syncthing/tray.nix rename to tests/modules/services/syncthing/linux/tray.nix