@@ -0,0 +1,218 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+let
5+ cfg = config.services.gerrit;
6+
7+ # NixOS option type for git-like configs
8+ gitIniType = with types;
9+ let
10+ primitiveType = either str (either bool int);
11+ multipleType = either primitiveType (listOf primitiveType);
12+ sectionType = lazyAttrsOf multipleType;
13+ supersectionType = lazyAttrsOf (either multipleType sectionType);
14+ in lazyAttrsOf supersectionType;
15+
16+ gerritConfig = pkgs.writeText "gerrit.conf" (
17+ lib.generators.toGitINI cfg.settings
18+ );
19+
20+ # Wrap the gerrit java with all the java options so it can be called
21+ # like a normal CLI app
22+ gerrit-cli = pkgs.writeShellScriptBin "gerrit" ''
23+ set -euo pipefail
24+ jvmOpts=(
25+ ${lib.escapeShellArgs cfg.jvmOpts}
26+ -Xmx${cfg.jvmHeapLimit}
27+ )
28+ exec ${cfg.jvmPackage}/bin/java \
29+ "''${jvmOpts[@]}" \
30+ -jar ${cfg.package}/webapps/${cfg.package.name}.war \
31+ "$@"
32+ '';
33+
34+ gerrit-plugins = pkgs.runCommand
35+ "gerrit-plugins"
36+ {
37+ buildInputs = [ gerrit-cli ];
38+ }
39+ ''
40+ shopt -s nullglob
41+ mkdir $out
42+
43+ for name in ${toString cfg.builtinPlugins}; do
44+ echo "Installing builtin plugin $name.jar"
45+ gerrit cat plugins/$name.jar > $out/$name.jar
46+ done
47+
48+ for file in ${toString cfg.plugins}; do
49+ name=$(echo "$file" | cut -d - -f 2-)
50+ echo "Installing plugin $name"
51+ ln -sf "$file" $out/$name
52+ done
53+ '';
54+in
55+{
56+ options = {
57+ services.gerrit = {
58+ enable = mkEnableOption "Gerrit service";
59+
60+ package = mkOption {
61+ type = types.package;
62+ default = pkgs.gerrit;
63+ description = "Gerrit package to use";
64+ };
65+
66+ jvmPackage = mkOption {
67+ type = types.package;
68+ default = pkgs.jre_headless;
69+ defaultText = "pkgs.jre_headless";
70+ description = "Java Runtime Environment package to use";
71+ };
72+
73+ jvmOpts = mkOption {
74+ type = types.listOf types.str;
75+ default = [
76+ "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
77+ "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
78+ ];
79+ description = "A list of JVM options to start gerrit with.";
80+ };
81+
82+ jvmHeapLimit = mkOption {
83+ type = types.str;
84+ default = "1024m";
85+ description = ''
86+ How much memory to allocate to the JVM heap
87+ '';
88+ };
89+
90+ listenAddress = mkOption {
91+ type = types.str;
92+ default = "[::]:8080";
93+ description = ''
94+ <literal>hostname:port</literal> to listen for HTTP traffic.
95+
96+ This is bound using the systemd socket activation.
97+ '';
98+ };
99+
100+ settings = mkOption {
101+ type = gitIniType;
102+ default = {};
103+ description = ''
104+ Gerrit configuration. This will be generated to the
105+ <literal>etc/gerrit.config</literal> file.
106+ '';
107+ };
108+
109+ plugins = mkOption {
110+ type = types.listOf types.package;
111+ default = [];
112+ description = ''
113+ List of plugins to add to Gerrit. Each derivation is a jar file
114+ itself where the name of the derivation is the name of plugin.
115+ '';
116+ };
117+
118+ builtinPlugins = mkOption {
119+ type = types.listOf (types.enum cfg.package.passthru.plugins);
120+ default = [];
121+ description = ''
122+ List of builtins plugins to install. Those are shipped in the
123+ <literal>gerrit.war</literal> file.
124+ '';
125+ };
126+
127+ serverId = mkOption {
128+ type = types.str;
129+ description = ''
130+ Set a UUID that uniquely identifies the server.
131+
132+ This can be generated with
133+ <literal>nix-shell -p utillinux --run uuidgen</literal>.
134+ '';
135+ };
136+ };
137+ };
138+
139+ config = mkIf cfg.enable {
140+
141+ services.gerrit.settings = {
142+ cache.directory = "/var/cache/gerrit";
143+ container.heapLimit = cfg.jvmHeapLimit;
144+ gerrit.basePath = lib.mkDefault "git";
145+ gerrit.serverId = cfg.serverId;
146+ httpd.inheritChannel = "true";
147+ httpd.listenUrl = lib.mkDefault "http://${cfg.listenAddress}";
148+ index.type = lib.mkDefault "lucene";
149+ };
150+
151+ # Add the gerrit CLI to the system to run `gerrit init` and friends.
152+ environment.systemPackages = [ gerrit-cli ];
153+
154+ systemd.sockets.gerrit = {
155+ unitConfig.Description = "Gerrit HTTP socket";
156+ wantedBy = [ "sockets.target" ];
157+ listenStreams = [ cfg.listenAddress ];
158+ };
159+
160+ systemd.services.gerrit = {
161+ description = "Gerrit";
162+
163+ wantedBy = [ "multi-user.target" ];
164+ requires = [ "gerrit.socket" ];
165+ after = [ "gerrit.socket" "network.target" ];
166+
167+ path = [
168+ gerrit-cli
169+ pkgs.bash
170+ pkgs.coreutils
171+ pkgs.git
172+ pkgs.openssh
173+ ];
174+
175+ environment = {
176+ GERRIT_HOME = "%S/gerrit";
177+ GERRIT_TMP = "%T";
178+ HOME = "%S/gerrit";
179+ XDG_CONFIG_HOME = "%S/gerrit/.config";
180+ };
181+
182+ preStart = ''
183+ set -euo pipefail
184+
185+ # bootstrap if nothing exists
186+ if [[ ! -d git ]]; then
187+ gerrit init --batch --no-auto-start
188+ fi
189+
190+ # install gerrit.war for the plugin manager
191+ rm -rf bin
192+ mkdir bin
193+ ln -sfv ${cfg.package}/webapps/${cfg.package.name}.war bin/gerrit.war
194+
195+ # copy the config, keep it mutable because Gerrit
196+ ln -sfv ${gerritConfig} etc/gerrit.config
197+
198+ # install the plugins
199+ rm -rf plugins
200+ ln -sv ${gerrit-plugins} plugins
201+ ''
202+ ;
203+
204+ serviceConfig = {
205+ CacheDirectory = "gerrit";
206+ DynamicUser = true;
207+ ExecStart = "${gerrit-cli}/bin/gerrit daemon --console-log";
208+ LimitNOFILE = 4096;
209+ StandardInput = "socket";
210+ StandardOutput = "journal";
211+ StateDirectory = "gerrit";
212+ WorkingDirectory = "%S/gerrit";
213+ };
214+ };
215+ };
216+
217+ meta.maintainers = with lib.maintainers; [ edef zimbatm ];
218+}