fork/home-manager

git clone https://git.julia.blue/fork/home-manager

ryanegit-sync: add darwin supportab70a023

master
2.9 KiB113 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.git-sync;
8
9  mkUnit = name: repo: {
10    Unit.Description = "Git Sync ${name}";
11
12    Install.WantedBy = [ "default.target" ];
13
14    Service = {
15      Environment = [
16        "PATH=${lib.makeBinPath (with pkgs; [ openssh git ])}"
17        "GIT_SYNC_DIRECTORY=${repo.path}"
18        "GIT_SYNC_COMMAND=${cfg.package}/bin/git-sync"
19        "GIT_SYNC_REPOSITORY=${repo.uri}"
20        "GIT_SYNC_INTERVAL=${toString repo.interval}"
21      ];
22      ExecStart = "${cfg.package}/bin/git-sync-on-inotify";
23      Restart = "on-abort";
24    };
25  };
26
27  mkAgent = name: repo: {
28    enable = true;
29    config = {
30      StartInterval = repo.interval;
31      ProcessType = "Background";
32      WorkingDirectory = "${repo.path}";
33      WatchPaths = [ "${repo.path}" ];
34      ProgramArguments = [ "${cfg.package}/bin/git-sync" ];
35    };
36  };
37
38  mkService = if pkgs.stdenv.isLinux then mkUnit else mkAgent;
39  services = mapAttrs' (name: repo: {
40    name = "git-sync-${name}";
41    value = mkService name repo;
42  }) cfg.repositories;
43
44  repositoryType = types.submodule ({ name, ... }: {
45    options = {
46      name = mkOption {
47        internal = true;
48        default = name;
49        type = types.str;
50        description = "The name that should be given to this unit.";
51      };
52
53      path = mkOption {
54        type = types.path;
55        description = "The path at which to sync the repository";
56      };
57
58      uri = mkOption {
59        type = types.str;
60        example = "git+ssh://user@example.com:/~[user]/path/to/repo.git";
61        description = ''
62          The URI of the remote to be synchronized. This is only used in the
63          event that the directory does not already exist. See
64          <https://git-scm.com/docs/git-clone#_git_urls>
65          for the supported URIs.
66
67          This option is not supported on Darwin.
68        '';
69      };
70
71      interval = mkOption {
72        type = types.int;
73        default = 500;
74        description = ''
75          The interval, specified in seconds, at which the synchronization will
76          be triggered even without filesystem changes.
77        '';
78      };
79    };
80  });
81
82in {
83  meta.maintainers =
84    [ maintainers.imalison maintainers.cab404 maintainers.ryane ];
85
86  options = {
87    services.git-sync = {
88      enable = mkEnableOption "git-sync services";
89
90      package = mkOption {
91        type = types.package;
92        default = pkgs.git-sync;
93        defaultText = literalExpression "pkgs.git-sync";
94        description = ''
95          Package containing the {command}`git-sync` program.
96        '';
97      };
98
99      repositories = mkOption {
100        type = with types; attrsOf repositoryType;
101        description = ''
102          The repositories that should be synchronized.
103        '';
104      };
105    };
106  };
107
108  config = mkIf cfg.enable (mkMerge [
109    (mkIf pkgs.stdenv.isLinux { systemd.user.services = services; })
110    (mkIf pkgs.stdenv.isDarwin { launchd.agents = services; })
111  ]);
112
113}