aboutsummaryrefslogtreecommitdiff
path: root/modules/nixos/services/git/default.nix
blob: a69ddbf6714ae09d76d527fdcc3c56ea3ef0e994 (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
{ options, config, lib, namespace, pkgs, ... }:

with lib; with lib.${namespace}; let
  cfg = config.${namespace}.services.git;
  impermanence = config.${namespace}.system.impermanence;
in {
  options.${namespace}.services.git = with types; {
    enable = mkEnableOption "git server";

    host = mkOption {
      type = str;
    };

    path = mkOption {
      type = path;
      default = "/srv/git";
    };

    adminPubkey = mkOption {
      type = str;
    };
  };

  config = mkIf cfg.enable {
    cxl.services.web.cgit = {
      enable = true;
      path = cfg.path + "/repositories";
      virtualHost = cfg.host;
    };

    environment.persistence.${impermanence.location} = {
      directories = [
        cfg.path
      ];
    };

    services.gitolite = {
      enable = true;

      user = "git";
      group = "git";
      description = "git";

      adminPubkey = cfg.adminPubkey;
      dataDir = cfg.path;

      extraGitoliteRc = ''
        $RC{GIT_CONFIG_KEYS} = 'cgit.* remote.origin.*';
      '';
    };

    systemd = {
      timers."cxl.git-mirror" = {
        wantedBy = [ "timers.target" ];
        timerConfig = {
          OnCalendar = "daily";
          Persistent = true;
          Unit = "cxl.git-mirror.service";
        };
      };

      services."cxl.git-mirror" = {
        #TODO: make this not shit
        script = ''
          API="https://api.github.com"
          API_USER="CartConnoisseur"
          API_HEADERS="-H 'Accept: application/vnd.github+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'User-Agent: CartConnoisseur'"

          for dir in *; do
            if [[ -d "$dir" ]]; then
              if ! origin="$(${pkgs.git}/bin/git -C "$dir" remote get-url origin 2> /dev/null)"; then continue; fi
              if ! [[ "$origin" == *"github.com"* ]]; then continue; fi
              if ! [[ "$origin" == *"$API_USER"* ]]; then continue; fi
              repo="''${origin##*/}"

              if ! res="$(${pkgs.curl}/bin/curl -s -L $API_HEADERS "$API/repos/$API_USER/''${repo%.git}")"; then continue; fi
              desc="$(${pkgs.jq}/bin/jq -r '.description' <<<"$res")"
              printf '%s\n' "$desc" > "$dir/description"
              printf 'synced %s desc: %s\n' "$dir" "$desc"

              ${pkgs.git}/bin/git -C "$dir" fetch --prune origin
              printf 'synced %s commits\n' "$dir"
            fi
          done
        '';

        serviceConfig = {
          Type = "oneshot";
          User = "git";
          WorkingDirectory = cfg.path + "/repositories";
        };
      };
    };
  };
}