blob: 6c0749ca068dc26c11f9e69725c92717a8744e6a [file] [log] [blame]
Skyler Greyfe1740c2023-10-21 01:24:18 +00001{ pkgs, config, lib, base, system, ... }:
2let cfg = config.services.gerrit;
3in lib.recursiveUpdate {
Skyler Grey08758a62023-10-09 07:35:09 +00004 sops.secrets.clicks_gerrit_db_password = {
5 mode = lib.mkForce "0440";
6 group = lib.mkForce "gerrit";
7 };
8
9 users.users.gerrit = {
10 isSystemUser = true;
11 createHome = true;
12 home = "/var/lib/gerrit";
13 group = config.users.groups.gerrit.name;
14 shell = pkgs.bashInteractive;
15 };
Skyler Greyfe1740c2023-10-21 01:24:18 +000016 users.groups.gerrit = { };
Skyler Grey08758a62023-10-09 07:35:09 +000017
18 systemd.services.gerrit.serviceConfig.User = "gerrit";
19 systemd.services.gerrit.serviceConfig.Group = "gerrit";
20 systemd.services.gerrit.serviceConfig.DynamicUser = lib.mkForce false;
21
22 services.gerrit = {
23 enable = true;
24
25 /* jvmOpts = [
Skyler Greyfe1740c2023-10-21 01:24:18 +000026 "-Djava.class.path=${pkgs.postgresql_jdbc}/share/java"
27 ];
28 */
Skyler Grey08758a62023-10-09 07:35:09 +000029
30 settings = {
31 # accountPatchReviewDb.url = "postgresql://localhost:${toString config.services.postgresql.port}/gerrit?user=gerrit&password=!!gerrit_database_password!!";
32 accounts = {
33 visibility = "SAME_GROUP";
34 defaultDisplayName = "USERNAME";
35 };
36 addReviewer = {
37 maxWithoutConfirmation = 3;
38 maxAllowed = 10;
39 };
40 auth = {
41 type = "OAUTH";
42 registerEmailPrivateKey = "!!gerrit_email_private_key!!";
43 userNameCaseInsensitive = true;
44 gitBasicAuthPolicy = "HTTP";
45 };
46 plugin."gerrit-oauth-provider-keycloak-oauth" = {
47 root-url = "https://login.clicks.codes";
48 realm = "clicks";
49 client-id = "git";
50 client-secret = "!!gerrit_oauth_client_secret!!";
51 use-preferred-username = true;
52 };
53 change = {
54 topicLimit = 0;
55 mergeabilityComputationBehavior = "API_REF_UPDATED_AND_CHANGE_REINDEX";
56 sendNewPatchsetEmails = false;
57 showAssigneeInChangesTable = true;
58 submitWholeTopic = true;
59 diff3ConflictView = true;
60 };
61 changeCleanup = {
62 abandonAfter = "3 weeks";
Skyler Greyfe1740c2023-10-21 01:24:18 +000063 abandonMessage =
64 "This change was abandoned due to 3 weeks of inactivity. If you still want it, please restore it";
Skyler Grey08758a62023-10-09 07:35:09 +000065 startTime = "00:00";
66 interval = "1 day";
67 };
68 attentionSet = {
69 readdAfter = "1 week";
Skyler Greyfe1740c2023-10-21 01:24:18 +000070 readdMessage =
71 "I've given the owner a *ping* as nothing has happened for a week. If in two weeks time the change is still inactive, I'll abandon it for you. If you still want it, please do something before then";
Skyler Grey08758a62023-10-09 07:35:09 +000072 startTime = "00:00";
73 interval = "1 day";
74 };
75 commentlink.gerrit = {
76 match = "(I[0-9a-f]{8,40})";
77 link = "/q/$1";
78 };
79 gc = {
80 aggressive = true;
81 startTime = "Sun 00:00";
82 interval = "1 week";
83 };
84 gerrit = {
85 basePath = "/var/lib/gerrit/repos";
86 defaultBranch = "refs/heads/main";
87 canonicalWebUrl = "https://git.clicks.codes/";
88 canonicalGitUrl = "ssh://ssh.clicks.codes/";
89 gitHttpUrl = "https://git.clicks.codes/";
Skyler Greyfe1740c2023-10-21 01:24:18 +000090 reportBugUrl =
91 "https://discord.gg/bPaNnxe"; # TODO: kinda obnoxious, better to setup bugzilla/similar
Skyler Grey08758a62023-10-09 07:35:09 +000092 enablePeerIPInReflogRecord = true;
93 instanceId = "a1d1";
94 instanceName = "a1d1.clicks";
95 };
96 mimetype = lib.pipe [ "image/*" "video/*" "application/pdf" ] [
Skyler Greyfe1740c2023-10-21 01:24:18 +000097 (map (name: {
98 inherit name;
99 value.safe = true;
100 }))
Skyler Grey08758a62023-10-09 07:35:09 +0000101 builtins.listToAttrs
102 ];
103 receive.enableSignedPush = true;
104 sendemail.enable = false; # TODO: add credentials to git@clicks.codes
105 sshd.advertisedAddress = "ssh.clicks.codes:29418";
106 user = {
107 name = "Clicks Gerrit";
108 email = "git@clicks.codes";
109 anonymousCoward = "Anonymous";
110 };
111 httpd.listenUrl = "proxy-https://${cfg.listenAddress}";
112 };
113
Skyler Greyfe1740c2023-10-21 01:24:18 +0000114 plugins = [
115 (derivation {
116 name =
117 "oauth.jar"; # HACK: wrapping a derivation in a derivation to rename it seems like a bad hack... but bazel would not build if I didn't (I think because it didn't like the .jar extension...) check why though?
118 src = (pkgs.buildBazelPackage {
119 __noChroot = true; # FIXME: terrible, horrible, no good, very bad
120 # name = "gerrit-oauth-provider.jar";
121 pname = "gerrit-oauth-provider.jar";
122 version = "unstable-2023-10-08";
123 src = pkgs.fetchgit {
124 url = "https://gerrit.googlesource.com/plugins/oauth";
125 rev = "1b3cc407cb2571d08601ab852e6e01f82d27160f";
126 hash = "sha256-yC/8qnkDbfIujl+Cvamr+EQSwto1DcIUWXh5cwDEZHo=";
127 deepClone =
128 true; # FIXME: this bazel build uses some git stuff, maybe we should try replacing with fakegit?
129 };
130 bazelTargets = [ "oauth" ];
131 bazel = pkgs.bazel_4;
132 buildAttrs = { };
133 fetchAttrs.sha256 =
134 "sha256-i5wOTn2NqqgJf4TCIqaCucpXu+5Vm5C84UPrGYFMSzc=";
Skyler Grey08758a62023-10-09 07:35:09 +0000135
Skyler Greyfe1740c2023-10-21 01:24:18 +0000136 postUnpack = ''
137 echo "4.2.2" > */.bazelversion # nixpkgs only has certain bazel versions, so let's upgrade the patch of this one
138 '';
Skyler Grey08758a62023-10-09 07:35:09 +0000139
Skyler Greyfe1740c2023-10-21 01:24:18 +0000140 buildInputs = with pkgs; [ git curl jdk11 ];
Skyler Grey08758a62023-10-09 07:35:09 +0000141
Skyler Greyfe1740c2023-10-21 01:24:18 +0000142 postInstall = ''
143 cp bazel-bin/oauth.jar $out
144 '';
145 });
Skyler Grey08758a62023-10-09 07:35:09 +0000146 builder = "/bin/sh";
147 args = [ "-c" "${pkgs.coreutils}/bin/cp $src $out" ];
148 inherit system;
Skyler Greyfe1740c2023-10-21 01:24:18 +0000149 })
150 ];
151 builtinPlugins = [
152 "codemirror-editor"
153 "commit-message-length-validator"
154 "delete-project"
155 "download-commands"
156 "gitiles"
157 "hooks"
158 "reviewnotes"
159 "singleusergroup"
160 "webhooks"
161 ];
Skyler Grey08758a62023-10-09 07:35:09 +0000162 serverId = "45f277d0-fce7-43b7-9eb3-2e3234e0110f";
163
164 listenAddress = "127.0.0.255:1000";
165 };
166
Skyler Greyfe1740c2023-10-21 01:24:18 +0000167 nix.settings.sandbox =
168 "relaxed"; # FIXME: terrible, horrible, no good, very bad, here to support buildBazelPackage's use of cURL
Skyler Grey08758a62023-10-09 07:35:09 +0000169
170 sops.secrets = {
171 gerrit_email_private_key = {
172 mode = "0400";
173 owner = config.users.users.root.name;
174 group = config.users.users.nobody.group;
175 sopsFile = ../secrets/gerrit.json;
176 format = "json";
177 };
178 gerrit_oauth_client_secret = {
179 mode = "0400";
180 owner = config.users.users.root.name;
181 group = config.users.users.nobody.group;
182 sopsFile = ../secrets/gerrit.json;
183 format = "json";
184 };
185 };
Skyler Greyfe1740c2023-10-21 01:24:18 +0000186} (let isDerived = base != null;
187in if isDerived then
188 let
189 gerrit_cfgfile =
190 pkgs.writeText "gerrit.conf" (lib.generators.toGitINI cfg.settings);
191 in {
192 scalpel.trafos."gerrit.conf" = {
193 source = toString gerrit_cfgfile;
194 matchers."gerrit_email_private_key".secret =
195 config.sops.secrets.gerrit_email_private_key.path;
196 matchers."gerrit_oauth_client_secret".secret =
197 config.sops.secrets.gerrit_oauth_client_secret.path;
198 owner = config.users.users.nobody.name;
199 group = "gerrit";
200 mode = "0040";
201 };
Skyler Grey08758a62023-10-09 07:35:09 +0000202
Skyler Greyfe1740c2023-10-21 01:24:18 +0000203 systemd.services.gerrit.preStart =
204 base.config.systemd.services.gerrit.preStart + ''
Skyler Grey08758a62023-10-09 07:35:09 +0000205 rm etc/gerrit.config
Skyler Greyfe1740c2023-10-21 01:24:18 +0000206 ln -sfv ${
207 config.scalpel.trafos."gerrit.conf".destination
208 } etc/gerrit.config
209 '';
210 }
211else
212 { })