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
This commit is contained in:
ryane 2023-07-20 21:10:17 -04:00 committed by Robert Helgesson
parent fb03fa5516
commit ab70a02363
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
5 changed files with 63 additions and 9 deletions

View file

@ -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: { services = mapAttrs' (name: repo: {
name = "git-sync-${name}"; name = "git-sync-${name}";
value = mkUnit name repo; value = mkService name repo;
}) cfg.repositories; }) cfg.repositories;
repositoryType = types.submodule ({ name, ... }: { repositoryType = types.submodule ({ name, ... }: {
@ -51,6 +63,8 @@ let
event that the directory does not already exist. See event that the directory does not already exist. See
<https://git-scm.com/docs/git-clone#_git_urls> <https://git-scm.com/docs/git-clone#_git_urls>
for the supported URIs. for the supported URIs.
This option is not supported on Darwin.
''; '';
}; };
@ -66,7 +80,8 @@ let
}); });
in { in {
meta.maintainers = [ maintainers.imalison maintainers.cab404 ]; meta.maintainers =
[ maintainers.imalison maintainers.cab404 maintainers.ryane ];
options = { options = {
services.git-sync = { services.git-sync = {
@ -90,12 +105,9 @@ in {
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable (mkMerge [
assertions = [ (mkIf pkgs.stdenv.isLinux { systemd.user.services = services; })
(lib.hm.assertions.assertPlatform "services.git-sync" pkgs (mkIf pkgs.stdenv.isDarwin { launchd.agents = services; })
lib.platforms.linux) ]);
];
systemd.user.services = services;
};
} }

View file

@ -147,6 +147,7 @@ import nmt {
./modules/xresources ./modules/xresources
] ++ lib.optionals isDarwin [ ] ++ lib.optionals isDarwin [
./modules/launchd ./modules/launchd
./modules/services/git-sync-darwin
./modules/services/imapnotify-darwin ./modules/services/imapnotify-darwin
./modules/targets-darwin ./modules/targets-darwin
] ++ lib.optionals isLinux [ ] ++ lib.optionals isLinux [

View file

@ -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}
'';
}

View file

@ -0,0 +1 @@
{ git-sync = ./basic.nix; }

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.nix-community.home.git-sync-test</string>
<key>ProcessType</key>
<string>Background</string>
<key>ProgramArguments</key>
<array>
<string>@git-sync@/bin/git-sync</string>
</array>
<key>StartInterval</key>
<integer>500</integer>
<key>WatchPaths</key>
<array>
<string>/a/path</string>
</array>
<key>WorkingDirectory</key>
<string>/a/path</string>
</dict>
</plist>