summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/monitoring/gitwatch.nix
blob: 3cb493378ffe7249031479367d7927c9bbc3813e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
  lib,
  pkgs,
  config,
  ...
}:
let
  inherit (lib)
    maintainers
    mapAttrs'
    mkEnableOption
    mkOption
    nameValuePair
    optionalString
    types
    ;
  mkSystemdService =
    name: cfg:
    nameValuePair "gitwatch-${name}" (
      let
        getvar = flag: var: optionalString (cfg."${var}" != null) "${flag} ${cfg."${var}"}";
        branch = getvar "-b" "branch";
        remote = getvar "-r" "remote";
        message = getvar "-m" "message";
      in
      rec {
        inherit (cfg) enable;
        after = [ "network-online.target" ];
        wants = after;
        wantedBy = [ "multi-user.target" ];
        description = "gitwatch for ${name}";
        path = with pkgs; [
          gitwatch
          git
          openssh
        ];
        script = ''
          if [ -n "${cfg.remote}" ] && ! [ -d "${cfg.path}" ]; then
            git clone ${branch} "${cfg.remote}" "${cfg.path}"
          fi
          gitwatch ${remote} ${message} ${branch} ${cfg.path}
        '';
        serviceConfig.User = cfg.user;
      }
    );
in
{
  options.services.gitwatch = mkOption {
    description = ''
      A set of git repositories to watch for. See
      [gitwatch](https://github.com/gitwatch/gitwatch) for more.
    '';
    default = { };
    example = {
      my-repo = {
        enable = true;
        user = "user";
        path = "/home/user/watched-project";
        remote = "git@github.com:me/my-project.git";
        message = "Auto-commit by gitwatch on %d";
      };
      disabled-repo = {
        enable = false;
        user = "user";
        path = "/home/user/disabled-project";
        remote = "git@github.com:me/my-old-project.git";
        branch = "autobranch";
      };
    };
    type =
      with types;
      attrsOf (submodule {
        options = {
          enable = mkEnableOption "watching for repo";
          path = mkOption {
            description = "The path to repo in local machine";
            type = str;
          };
          user = mkOption {
            description = "The name of services's user";
            type = str;
            default = "root";
          };
          remote = mkOption {
            description = "Optional url of remote repository";
            type = nullOr str;
            default = null;
          };
          message = lib.mkOption {
            description = "Optional text to use in as commit message; all occurrences of `%d` will be replaced by formatted date/time";
            type = nullOr str;
            default = null;
          };
          branch = mkOption {
            description = "Optional branch in remote repository";
            type = nullOr str;
            default = null;
          };
        };
      });
  };
  config.systemd.services = mapAttrs' mkSystemdService config.services.gitwatch;
  meta.maintainers = with maintainers; [
    shved
    zareix
  ];
}