git.iko.soy/typeable/nixpkgs
on master help sign in
nixos: add gerrit module

Co-authored-by: edef <edef@edef.eu> Co-authored-by: Florian Klink <flokli@flokli.de>

commitd37a0dca1322126097431d27336384e436687d5e authorzimbatm <zimbatm@zimbatm.com> dateThu 5 Mar 2020, 22:11 (7 years ago) parent10c96e5
5 files changed+294
nixos/modules/module-list.nix+1 nixos/modules/services/web-apps/gerrit.nix+218 nixos/tests/all-tests.nix+1 nixos/tests/gerrit.nix+56 pkgs/applications/version-management/gerrit/default.nix+18
nixos/modules/module-list.nix+1
@@ -823,6 +823,7 @@
823823 ./services/web-apps/documize.nix
824824 ./services/web-apps/dokuwiki.nix
825825 ./services/web-apps/frab.nix
826+ ./services/web-apps/gerrit.nix
826827 ./services/web-apps/gotify-server.nix
827828 ./services/web-apps/grocy.nix
828829 ./services/web-apps/icingaweb2/icingaweb2.nix
nixos/modules/services/web-apps/gerrit.nixnew file+218
@@ -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+}
nixos/tests/all-tests.nix+1
@@ -97,6 +97,7 @@ in
9797 fontconfig-default-fonts = handleTest ./fontconfig-default-fonts.nix {};
9898 freeswitch = handleTest ./freeswitch.nix {};
9999 fsck = handleTest ./fsck.nix {};
100+ gerrit = handleTest ./gerrit.nix {};
100101 gotify-server = handleTest ./gotify-server.nix {};
101102 grocy = handleTest ./grocy.nix {};
102103 gitdaemon = handleTest ./gitdaemon.nix {};
nixos/tests/gerrit.nixnew file+56
@@ -0,0 +1,56 @@
1+import ./make-test-python.nix ({ pkgs, ... }:
2+
3+let
4+ lfs = pkgs.fetchurl {
5+ url = "https://gerrit-ci.gerritforge.com/job/plugin-lfs-bazel-master/90/artifact/bazel-bin/plugins/lfs/lfs.jar";
6+ sha256 = "023b0kd8djm3cn1lf1xl67yv3j12yl8bxccn42lkfmwxjwjfqw6h";
7+ };
8+
9+in {
10+ name = "gerrit";
11+
12+ meta = with pkgs.stdenv.lib.maintainers; {
13+ maintainers = [ flokli zimbatm ];
14+ };
15+
16+ nodes = {
17+ server =
18+ { config, pkgs, ... }: {
19+ networking.firewall.allowedTCPPorts = [ 80 2222 ];
20+
21+ virtualisation.memorySize = 1024;
22+
23+ services.gerrit = {
24+ enable = true;
25+ serverId = "aa76c84b-50b0-4711-a0a0-1ee30e45bbd0";
26+ listenAddress = "[::]:80";
27+ jvmPackage = pkgs.jdk12_headless;
28+ jvmHeapLimit = "1g";
29+
30+ plugins = [ lfs ];
31+ builtinPlugins = [ "hooks" "webhooks" ];
32+ settings = {
33+ gerrit.canonicalWebUrl = "http://server";
34+ lfs.plugin = "lfs";
35+ plugins.allowRemoteAdmin = true;
36+ sshd.listenAddress = "[::]:2222";
37+ sshd.advertisedAddress = "[::]:2222";
38+ };
39+ };
40+ };
41+
42+ client =
43+ { ... }: {
44+ };
45+ };
46+
47+ testScript = ''
48+ start_all()
49+ server.wait_for_unit("gerrit.service")
50+ server.wait_for_open_port(80)
51+ client.succeed("curl http://server")
52+
53+ server.wait_for_open_port(2222)
54+ client.succeed("nc -z server 2222")
55+ '';
56+})
pkgs/applications/version-management/gerrit/default.nix+18
@@ -14,6 +14,24 @@ stdenv.mkDerivation rec {
1414 ln -s ${src} "$out"/webapps/gerrit-${version}.war
1515 '';
1616
17+ passthru = {
18+ # A list of plugins that are part of the gerrit.war file.
19+ # Use `java -jar gerrit.war ls | grep plugins/` to generate that list.
20+ plugins = [
21+ "codemirror-editor"
22+ "commit-message-length-validator"
23+ "delete-project"
24+ "download-commands"
25+ "gitiles"
26+ "hooks"
27+ "plugin-manager"
28+ "replication"
29+ "reviewnotes"
30+ "singleusergroup"
31+ "webhooks"
32+ ];
33+ };
34+
1735 meta = with stdenv.lib; {
1836 homepage = "https://www.gerritcodereview.com/index.md";
1937 license = licenses.asl20;