Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 1 | { pkgs, config, lib, base, system, ... }: |
| 2 | let cfg = config.services.gerrit; |
| 3 | in lib.recursiveUpdate { |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 4 | 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 Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 16 | users.groups.gerrit = { }; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 17 | |
| 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 Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 26 | "-Djava.class.path=${pkgs.postgresql_jdbc}/share/java" |
| 27 | ]; |
| 28 | */ |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 29 | |
| 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 Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 63 | abandonMessage = |
| 64 | "This change was abandoned due to 3 weeks of inactivity. If you still want it, please restore it"; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 65 | startTime = "00:00"; |
| 66 | interval = "1 day"; |
| 67 | }; |
| 68 | attentionSet = { |
| 69 | readdAfter = "1 week"; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 70 | 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 Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 72 | 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 | }; |
Skyler Grey | fca1804 | 2023-10-24 19:17:27 +0000 | [diff] [blame] | 84 | repository."*".defaultSubmitType = "REBASE_ALWAYS"; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 85 | gerrit = { |
| 86 | basePath = "/var/lib/gerrit/repos"; |
| 87 | defaultBranch = "refs/heads/main"; |
| 88 | canonicalWebUrl = "https://git.clicks.codes/"; |
| 89 | canonicalGitUrl = "ssh://ssh.clicks.codes/"; |
| 90 | gitHttpUrl = "https://git.clicks.codes/"; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 91 | reportBugUrl = |
| 92 | "https://discord.gg/bPaNnxe"; # TODO: kinda obnoxious, better to setup bugzilla/similar |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 93 | enablePeerIPInReflogRecord = true; |
| 94 | instanceId = "a1d1"; |
| 95 | instanceName = "a1d1.clicks"; |
| 96 | }; |
| 97 | mimetype = lib.pipe [ "image/*" "video/*" "application/pdf" ] [ |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 98 | (map (name: { |
| 99 | inherit name; |
| 100 | value.safe = true; |
| 101 | })) |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 102 | builtins.listToAttrs |
| 103 | ]; |
| 104 | receive.enableSignedPush = true; |
| 105 | sendemail.enable = false; # TODO: add credentials to git@clicks.codes |
| 106 | sshd.advertisedAddress = "ssh.clicks.codes:29418"; |
| 107 | user = { |
| 108 | name = "Clicks Gerrit"; |
| 109 | email = "git@clicks.codes"; |
| 110 | anonymousCoward = "Anonymous"; |
| 111 | }; |
| 112 | httpd.listenUrl = "proxy-https://${cfg.listenAddress}"; |
| 113 | }; |
| 114 | |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 115 | plugins = [ |
| 116 | (derivation { |
| 117 | name = |
| 118 | "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? |
| 119 | src = (pkgs.buildBazelPackage { |
| 120 | __noChroot = true; # FIXME: terrible, horrible, no good, very bad |
| 121 | # name = "gerrit-oauth-provider.jar"; |
| 122 | pname = "gerrit-oauth-provider.jar"; |
| 123 | version = "unstable-2023-10-08"; |
| 124 | src = pkgs.fetchgit { |
| 125 | url = "https://gerrit.googlesource.com/plugins/oauth"; |
| 126 | rev = "1b3cc407cb2571d08601ab852e6e01f82d27160f"; |
| 127 | hash = "sha256-yC/8qnkDbfIujl+Cvamr+EQSwto1DcIUWXh5cwDEZHo="; |
| 128 | deepClone = |
| 129 | true; # FIXME: this bazel build uses some git stuff, maybe we should try replacing with fakegit? |
| 130 | }; |
| 131 | bazelTargets = [ "oauth" ]; |
| 132 | bazel = pkgs.bazel_4; |
| 133 | buildAttrs = { }; |
| 134 | fetchAttrs.sha256 = |
| 135 | "sha256-i5wOTn2NqqgJf4TCIqaCucpXu+5Vm5C84UPrGYFMSzc="; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 136 | |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 137 | postUnpack = '' |
| 138 | echo "4.2.2" > */.bazelversion # nixpkgs only has certain bazel versions, so let's upgrade the patch of this one |
| 139 | ''; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 140 | |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 141 | buildInputs = with pkgs; [ git curl jdk11 ]; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 142 | |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 143 | postInstall = '' |
| 144 | cp bazel-bin/oauth.jar $out |
| 145 | ''; |
| 146 | }); |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 147 | builder = "/bin/sh"; |
| 148 | args = [ "-c" "${pkgs.coreutils}/bin/cp $src $out" ]; |
| 149 | inherit system; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 150 | }) |
| 151 | ]; |
| 152 | builtinPlugins = [ |
| 153 | "codemirror-editor" |
| 154 | "commit-message-length-validator" |
| 155 | "delete-project" |
| 156 | "download-commands" |
| 157 | "gitiles" |
| 158 | "hooks" |
| 159 | "reviewnotes" |
| 160 | "singleusergroup" |
| 161 | "webhooks" |
| 162 | ]; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 163 | serverId = "45f277d0-fce7-43b7-9eb3-2e3234e0110f"; |
| 164 | |
| 165 | listenAddress = "127.0.0.255:1000"; |
| 166 | }; |
| 167 | |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 168 | nix.settings.sandbox = |
| 169 | "relaxed"; # FIXME: terrible, horrible, no good, very bad, here to support buildBazelPackage's use of cURL |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 170 | |
| 171 | sops.secrets = { |
| 172 | gerrit_email_private_key = { |
| 173 | mode = "0400"; |
| 174 | owner = config.users.users.root.name; |
| 175 | group = config.users.users.nobody.group; |
| 176 | sopsFile = ../secrets/gerrit.json; |
| 177 | format = "json"; |
| 178 | }; |
| 179 | gerrit_oauth_client_secret = { |
| 180 | mode = "0400"; |
| 181 | owner = config.users.users.root.name; |
| 182 | group = config.users.users.nobody.group; |
| 183 | sopsFile = ../secrets/gerrit.json; |
| 184 | format = "json"; |
| 185 | }; |
| 186 | }; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 187 | } (let isDerived = base != null; |
| 188 | in if isDerived then |
| 189 | let |
| 190 | gerrit_cfgfile = |
| 191 | pkgs.writeText "gerrit.conf" (lib.generators.toGitINI cfg.settings); |
| 192 | in { |
| 193 | scalpel.trafos."gerrit.conf" = { |
| 194 | source = toString gerrit_cfgfile; |
| 195 | matchers."gerrit_email_private_key".secret = |
| 196 | config.sops.secrets.gerrit_email_private_key.path; |
| 197 | matchers."gerrit_oauth_client_secret".secret = |
| 198 | config.sops.secrets.gerrit_oauth_client_secret.path; |
| 199 | owner = config.users.users.nobody.name; |
| 200 | group = "gerrit"; |
| 201 | mode = "0040"; |
| 202 | }; |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 203 | |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 204 | systemd.services.gerrit.preStart = |
| 205 | base.config.systemd.services.gerrit.preStart + '' |
Skyler Grey | 08758a6 | 2023-10-09 07:35:09 +0000 | [diff] [blame] | 206 | rm etc/gerrit.config |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 207 | ln -sfv ${ |
| 208 | config.scalpel.trafos."gerrit.conf".destination |
| 209 | } etc/gerrit.config |
| 210 | ''; |
| 211 | } |
| 212 | else |
| 213 | { }) |