1 { config, lib, pkgs, ... }:
5 cfg = config.services.gerrit;
7 # NixOS option type for git-like configs
8 gitIniType = with types;
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;
16 gerritConfig = pkgs.writeText "gerrit.conf" (
17 lib.generators.toGitINI cfg.settings
20 replicationConfig = pkgs.writeText "replication.conf" (
21 lib.generators.toGitINI cfg.replicationSettings
24 # Wrap the gerrit java with all the java options so it can be called
25 # like a normal CLI app
26 gerrit-cli = pkgs.writeShellScriptBin "gerrit" ''
29 ${lib.escapeShellArgs cfg.jvmOpts}
30 -Xmx${cfg.jvmHeapLimit}
32 exec ${cfg.jvmPackage}/bin/java \
34 -jar ${cfg.package}/webapps/${cfg.package.name}.war \
38 gerrit-plugins = pkgs.runCommand
41 buildInputs = [ gerrit-cli ];
47 for name in ${toString cfg.builtinPlugins}; do
48 echo "Installing builtin plugin $name.jar"
49 gerrit cat plugins/$name.jar > $out/$name.jar
52 for file in ${toString cfg.plugins}; do
53 name=$(echo "$file" | cut -d - -f 2-)
54 echo "Installing plugin $name"
55 ln -sf "$file" $out/$name
62 enable = mkEnableOption "Gerrit service";
64 package = mkPackageOption pkgs "gerrit" { };
66 jvmPackage = mkPackageOption pkgs "jre_headless" { };
69 type = types.listOf types.str;
71 "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
72 "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
74 description = "A list of JVM options to start gerrit with.";
77 jvmHeapLimit = mkOption {
81 How much memory to allocate to the JVM heap
85 listenAddress = mkOption {
87 default = "[::]:8080";
89 `hostname:port` to listen for HTTP traffic.
91 This is bound using the systemd socket activation.
99 Gerrit configuration. This will be generated to the
100 `etc/gerrit.config` file.
104 replicationSettings = mkOption {
108 Replication configuration. This will be generated to the
109 `etc/replication.config` file.
114 type = types.listOf types.package;
117 List of plugins to add to Gerrit. Each derivation is a jar file
118 itself where the name of the derivation is the name of plugin.
122 builtinPlugins = mkOption {
123 type = types.listOf (types.enum cfg.package.passthru.plugins);
126 List of builtins plugins to install. Those are shipped in the
131 serverId = mkOption {
134 Set a UUID that uniquely identifies the server.
136 This can be generated with
137 `nix-shell -p util-linux --run uuidgen`.
143 config = mkIf cfg.enable {
147 assertion = cfg.replicationSettings != {} -> elem "replication" cfg.builtinPlugins;
148 message = "Gerrit replicationSettings require enabling the replication plugin";
152 services.gerrit.settings = {
153 cache.directory = "/var/cache/gerrit";
154 container.heapLimit = cfg.jvmHeapLimit;
155 gerrit.basePath = lib.mkDefault "git";
156 gerrit.serverId = cfg.serverId;
157 httpd.inheritChannel = "true";
158 httpd.listenUrl = lib.mkDefault "http://${cfg.listenAddress}";
159 index.type = lib.mkDefault "lucene";
162 # Add the gerrit CLI to the system to run `gerrit init` and friends.
163 environment.systemPackages = [ gerrit-cli ];
165 systemd.sockets.gerrit = {
166 unitConfig.Description = "Gerrit HTTP socket";
167 wantedBy = [ "sockets.target" ];
168 listenStreams = [ cfg.listenAddress ];
171 systemd.services.gerrit = {
172 description = "Gerrit";
174 wantedBy = [ "multi-user.target" ];
175 requires = [ "gerrit.socket" ];
176 after = [ "gerrit.socket" "network.target" ];
187 GERRIT_HOME = "%S/gerrit";
190 XDG_CONFIG_HOME = "%S/gerrit/.config";
196 # bootstrap if nothing exists
197 if [[ ! -d git ]]; then
198 gerrit init --batch --no-auto-start
201 # install gerrit.war for the plugin manager
204 ln -sfv ${cfg.package}/webapps/${cfg.package.name}.war bin/gerrit.war
206 # copy the config, keep it mutable because Gerrit
207 ln -sfv ${gerritConfig} etc/gerrit.config
208 ln -sfv ${replicationConfig} etc/replication.config
210 # install the plugins
212 ln -sv ${gerrit-plugins} plugins
217 CacheDirectory = "gerrit";
219 ExecStart = "${gerrit-cli}/bin/gerrit daemon --console-log";
221 StandardInput = "socket";
222 StandardOutput = "journal";
223 StateDirectory = "gerrit";
224 WorkingDirectory = "%S/gerrit";
229 meta.maintainers = with lib.maintainers; [ edef zimbatm ];
230 # uses attributes of the linked package
231 meta.buildDocsInSandbox = false;