all repos — nixfiles @ dfa8b3db53caa5a17d3d9ca1dbc7235aca945b3d

System and user configuration, managed by nix and home-manager

system/settings/services/git-server.nix (view raw)

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{ config
, lib
, pkgs
, ...
}:
let
  inherit (lib) pipe flatten concatMapAttrs mapAttrsToList;
  inherit (import ../../../lib/caddy.nix { inherit lib; }) security-headers;

  gitoliteCfg = config.services.gitolite;
  repos = "${gitoliteCfg.dataDir}/repositories";

  mirrors = {
    sourcehut = {
      hostname = "git.sr.ht";
      username = "~alanpearce";
    };
    codeberg = {
      hostname = "codeberg.org";
      username = "alanpearce";
    };
    github = {
      hostname = "github.com";
      username = "alanpearce";
    };
  };

  repoMirrors = {
    nixfiles = [ "sourcehut" "codeberg" ];
    searchix = [ "sourcehut" "codeberg" ];
    website = [ "sourcehut" ];
    homestead = [ "sourcehut" ];
    legit = [ "github" ];
    elgit = [ "codeberg" ];
    gomponents = [ "sourcehut" "github" ];
    nix-packages = [ "sourcehut" "github" ];
    zola-bearblog = [ "sourcehut" "codeberg" ];
  };

  createMirrorService =
    name: { hostname, username }:
    {
      "mirror-to-${name}@" = {
        path = with pkgs; [ gitMinimal openssh ];
        serviceConfig = {
          Type = "oneshot";
          User = "gitolite";
          WorkingDirectory = "${repos}/%i.git";
          ExecStart = "${pkgs.gitMinimal}/bin/git push --mirror git@${hostname}:${username}/%i";
        };
        unitConfig = {
          # only mirror public repositories
          ConditionPathExists = "${repos}/%i.git/git-daemon-export-ok";
        };
      };
    };

  createMirrorPath = name: { hostname, username }:
    {
      "mirror-to-${name}@" = {
        pathConfig = {
          PathChanged = "${repos}/%i.git/refs/heads";
          StartLimitIntervalSec = "1h";
          StartLimitBurst = 5;
        };
      };
    };


  mkMirrorWants = repo: map (target: "mirror-to-${target}@${repo}.path");
in
{
  imports = [
    <elgit>
  ];

  services.gitolite = {
    enable = true;
    adminPubkey = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHYUyDdw92TNXguAxcmcmZmn/7ECGdRp6ckjxU+5zCw3BCnsS5+xEvHBVnnFdJRoH2XpfMeJjE+fi67zFVhlbn4= root@secretive.marvin";
    extraGitoliteRc = ''
      $RC{UMASK} = 0027;
      $RC{LOG_EXTRA} = 0;
      $RC{HOSTNAME} = "${config.networking.hostName}";
      $RC{LOCAL_CODE} = "$rc{GL_ADMIN_BASE}/local";
      push( @{$RC{ENABLE}}, 'D' );
      push( @{$RC{ENABLE}}, 'Shell alan' );
      push( @{$RC{ENABLE}}, 'cgit' );
      push( @{$RC{ENABLE}}, 'repo-specific-hooks' );
    '';
  };
  services.elgit = {
    enable = true;
    group = gitoliteCfg.group;
    settings = {
      server.name = "git.alanpearce.eu";
      repo = {
        root = gitoliteCfg.dataDir;
      };
    };
  };
  services.gitDaemon = {
    enable = true;
    user = "gitolite";
    group = gitoliteCfg.group;
    basePath = repos;
  };

  services.caddy.virtualHosts = {
    "git.alanpearce.eu" =
      let
        settings = config.services.elgit.settings;
        server = settings.server;
      in
      {
        extraConfig = ''
          encode zstd gzip
          ${security-headers {
            overrides.content-security-policy = {
              default-src = [ "none" ];
              base-uri = [ "none" ];
              style-src = [ "self" ];
              script-src = [ "none" ];
              form-action = [ "self" ];
              connect-src = [ "self" ];
              img-src = [ "https:" ];
              object-src = [ "none" ];
            };
          }}
          reverse_proxy ${server.host}:${toString server.port}
        '';
      };

    "legit.alanpearce.eu" = {
      serverAliases = [ "elgit.alanpearce.eu" ];
      extraConfig = ''
        encode zstd gzip
        redir https://git.alanpearce.eu{uri} temporary
      '';
    };
  };

  systemd.services = concatMapAttrs createMirrorService mirrors;
  systemd.paths = concatMapAttrs createMirrorPath mirrors;
  systemd.targets.git-mirroring = {
    wantedBy = [ "multi-user.target" ];
    wants = pipe
      repoMirrors [
      (mapAttrsToList mkMirrorWants)
      flatten
    ];
  };
}