about summary refs log tree commit diff
path: root/modules/services/gerrit/default.nix
blob: 485bd58d98bd6c3eaa5f273e8c4512379baa4a04 (plain) (blame)
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
{ config, pkgs, lib, ... }:
let
  cfg = config.my.services.gerrit;
  oauth = pkgs.fetchurl {
    url =
      "https://github.com/davido/gerrit-oauth-provider/releases/download/v3.5.1/gerrit-oauth-provider.jar";
    sha256 = "312dc494c454ac15f89a289f95ea4c11344add26804aaa6a3b79d49fd92adc69";
  };
in {
  options.my.services.gerrit = with lib; {
    enable = mkEnableOption "gerrit git server";
    vhostName = mkOption {
      type = types.str;
      example = "cl.fcuny.net";
      description = "Name for the virtual host";
    };
  };

  config = lib.mkIf cfg.enable {
    users.users.git = {
      description = "git";
      home = "/var/lib/gerrit";
      useDefaultShell = true;
      group = "git";
      isSystemUser = true;
    };
    users.groups.git = { };

    services.gerrit = {
      enable = true;
      listenAddress = "[::]:4778";
      serverId = "36bc0ffe-8f33-4045-bf8b-de5f88815fc0";
      builtinPlugins = [ "download-commands" "hooks" ];
      jvmHeapLimit = "4g";

      plugins = [ oauth ];

      # The default JDK is incompatible with gerrit.
      jvmPackage = pkgs.openjdk11_headless;

      settings = {
        core.packedGitLimit = "100m";
        log.jsonLogging = true;
        log.textLogging = false;
        sshd.advertisedAddress = "git.fcuny.net:29418";
        cache.web_sessions.maxAge = "3 months";
        plugins.allowRemoteAdmin = false;
        change.enableAttentionSet = true;
        change.enableAssignee = false;

        gerrit = {
          canonicalWebUrl = "https://${cfg.vhostName}";
          docUrl = "/Documentation";
        };

        httpd.listenUrl = "proxy-https://localhost:4778";

        download.command = [ "checkout" "cherry_pick" "format_patch" "pull" ];

        # Configure for cgit.
        gitweb = {
          type = "custom";
          url = "https://git.fcuny.net";
          project = "/\${project}";
          revision = "/commit/?id=\${commit}";
          branch = "/log/?h=\${branch}";
          tag = "/tag/?h=\${tag}";
          roottree = "/tree/?h=\${commit}";
          file = "/tree/\${file}?h=\${commit}";
          filehistory = "/log/\${file}?h=\${branch}";
          linkname = "cgit";
        };

        auth.type = "OAUTH";

        # users can change their emails
        oauth.allowRegisterNewEmail = true;

        plugin.gerrit-oauth-provider-google-oauth = {
          client-id =
            "966881439540-5k20bis59lqs2bsi3rukfbveu8r0ta8q.apps.googleusercontent.com";
        };

        # use gerrit HTTP password
        auth.gitBasicAuthPolicy = "HTTP";

        # Receiving email is not currently supported.
        sendemail = {
          enable = true;
          html = false;
          connectTimeout = "10sec";
          from = "gerrit <gerrit@fcuny.net>";
          includeDiff = true;
          smtpEncryption = "tls";
          smtpServer = "smtp.fastmail.com";
          smtpServerPort = 587;
        };
      };
    };

    systemd.services.gerrit = {
      serviceConfig = {
        # Using DynamicUser fails to generate correctly the ssh keys
        # needed for the ssh server that is managed by gerrit.
        # Instead, let's re-use the git user.
        DynamicUser = lib.mkForce false;
        User = "git";
        Group = "git";
      };
    };

    my.services.backup = { paths = [ "/var/lib/gerrit" ]; };

    services.nginx.virtualHosts."${cfg.vhostName}" = {
      forceSSL = true;
      enableACME = true;
      locations."/" = { proxyPass = "http://127.0.0.1:4778"; };
    };
  };
}