From ab70a02363e28738f8c6e2793e4d6b7105a0494d Mon Sep 17 00:00:00 2001 From: ryane Date: Thu, 20 Jul 2023 21:10:17 -0400 Subject: [PATCH] git-sync: add darwin support - On darwin, creates a launch agent to run git-sync on an interval and when the `path` changes. - The `uri` option is not used on Darwin. The auto-creation of the local git directory from the `uri` is a feature of the git-sync-on-inotify [1] wrapper (which won't work on Darwin afaik) and not `git-sync` itself. [1] https://github.com/simonthum/git-sync/blob/master/contrib/git-sync-on-inotify --- modules/services/git-sync.nix | 30 +++++++++++++------ tests/default.nix | 1 + .../services/git-sync-darwin/basic.nix | 18 +++++++++++ .../services/git-sync-darwin/default.nix | 1 + .../git-sync-darwin/expected-agent.plist | 22 ++++++++++++++ 5 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 tests/modules/services/git-sync-darwin/basic.nix create mode 100644 tests/modules/services/git-sync-darwin/default.nix create mode 100644 tests/modules/services/git-sync-darwin/expected-agent.plist diff --git a/modules/services/git-sync.nix b/modules/services/git-sync.nix index 770cec0f..a274da8f 100644 --- a/modules/services/git-sync.nix +++ b/modules/services/git-sync.nix @@ -24,9 +24,21 @@ let }; }; + mkAgent = name: repo: { + enable = true; + config = { + StartInterval = repo.interval; + ProcessType = "Background"; + WorkingDirectory = "${repo.path}"; + WatchPaths = [ "${repo.path}" ]; + ProgramArguments = [ "${cfg.package}/bin/git-sync" ]; + }; + }; + + mkService = if pkgs.stdenv.isLinux then mkUnit else mkAgent; services = mapAttrs' (name: repo: { name = "git-sync-${name}"; - value = mkUnit name repo; + value = mkService name repo; }) cfg.repositories; repositoryType = types.submodule ({ name, ... }: { @@ -51,6 +63,8 @@ let event that the directory does not already exist. See for the supported URIs. + + This option is not supported on Darwin. ''; }; @@ -66,7 +80,8 @@ let }); in { - meta.maintainers = [ maintainers.imalison maintainers.cab404 ]; + meta.maintainers = + [ maintainers.imalison maintainers.cab404 maintainers.ryane ]; options = { services.git-sync = { @@ -90,12 +105,9 @@ in { }; }; - config = mkIf cfg.enable { - assertions = [ - (lib.hm.assertions.assertPlatform "services.git-sync" pkgs - lib.platforms.linux) - ]; + config = mkIf cfg.enable (mkMerge [ + (mkIf pkgs.stdenv.isLinux { systemd.user.services = services; }) + (mkIf pkgs.stdenv.isDarwin { launchd.agents = services; }) + ]); - systemd.user.services = services; - }; } diff --git a/tests/default.nix b/tests/default.nix index 97f4b4ef..e52d8d88 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -147,6 +147,7 @@ import nmt { ./modules/xresources ] ++ lib.optionals isDarwin [ ./modules/launchd + ./modules/services/git-sync-darwin ./modules/services/imapnotify-darwin ./modules/targets-darwin ] ++ lib.optionals isLinux [ diff --git a/tests/modules/services/git-sync-darwin/basic.nix b/tests/modules/services/git-sync-darwin/basic.nix new file mode 100644 index 00000000..a50c1e04 --- /dev/null +++ b/tests/modules/services/git-sync-darwin/basic.nix @@ -0,0 +1,18 @@ +{ config, ... }: + +{ + services.git-sync = { + enable = true; + package = config.lib.test.mkStubPackage { outPath = "@git-sync@"; }; + repositories.test = { + path = "/a/path"; + uri = "git+ssh://user@example.com:/~user/path/to/repo.git"; + }; + }; + + nmt.script = '' + serviceFile=LaunchAgents/org.nix-community.home.git-sync-test.plist + assertFileExists "$serviceFile" + assertFileContent "$serviceFile" ${./expected-agent.plist} + ''; +} diff --git a/tests/modules/services/git-sync-darwin/default.nix b/tests/modules/services/git-sync-darwin/default.nix new file mode 100644 index 00000000..0e9db79d --- /dev/null +++ b/tests/modules/services/git-sync-darwin/default.nix @@ -0,0 +1 @@ +{ git-sync = ./basic.nix; } diff --git a/tests/modules/services/git-sync-darwin/expected-agent.plist b/tests/modules/services/git-sync-darwin/expected-agent.plist new file mode 100644 index 00000000..2699a996 --- /dev/null +++ b/tests/modules/services/git-sync-darwin/expected-agent.plist @@ -0,0 +1,22 @@ + + + + + Label + org.nix-community.home.git-sync-test + ProcessType + Background + ProgramArguments + + @git-sync@/bin/git-sync + + StartInterval + 500 + WatchPaths + + /a/path + + WorkingDirectory + /a/path + + \ No newline at end of file