1 { config, lib, pkgs, ... }:
3 cfg = config.services.jenkins;
4 jenkinsUrl = "http://${cfg.listenAddress}:${toString cfg.port}${cfg.prefix}";
8 enable = lib.mkOption {
12 Whether to enable the jenkins continuous integration server.
20 User the jenkins server should execute under.
24 group = lib.mkOption {
28 If the default user "jenkins" is configured then this is the primary
33 extraGroups = lib.mkOption {
34 type = lib.types.listOf lib.types.str;
36 example = [ "wheel" "dialout" ];
38 List of extra groups that the "jenkins" user should be a part of.
43 default = "/var/lib/jenkins";
44 type = lib.types.path;
46 The path to use as JENKINS_HOME. If the default user "jenkins" is configured then
47 this is the home of the "jenkins" user.
51 listenAddress = lib.mkOption {
53 example = "localhost";
56 Specifies the bind address on which the jenkins HTTP interface listens.
57 The default is the wildcard address.
63 type = lib.types.port;
65 Specifies port number on which the jenkins HTTP interface listens.
70 prefix = lib.mkOption {
75 Specifies a urlPrefix to use with jenkins.
76 If the example /jenkins is given, the jenkins server will be
77 accessible using localhost:8080/jenkins.
81 package = lib.mkPackageOption pkgs "jenkins" { };
83 packages = lib.mkOption {
84 default = [ pkgs.stdenv pkgs.git pkgs.jdk17 config.programs.ssh.package pkgs.nix ];
85 defaultText = lib.literalExpression "[ pkgs.stdenv pkgs.git pkgs.jdk17 config.programs.ssh.package pkgs.nix ]";
86 type = lib.types.listOf lib.types.package;
88 Packages to add to PATH for the jenkins process.
92 environment = lib.mkOption {
94 type = with lib.types; attrsOf str;
96 Additional environment variables to be passed to the jenkins process.
97 As a base environment, jenkins receives NIX_PATH from
98 {option}`environment.sessionVariables`, NIX_REMOTE is set to
99 "daemon" and JENKINS_HOME is set to the value of
100 {option}`services.jenkins.home`.
101 This option has precedence and can be used to override those
106 plugins = lib.mkOption {
108 type = lib.types.nullOr (lib.types.attrsOf lib.types.package);
110 A set of plugins to activate. Note that this will completely
111 remove and replace any previously installed plugins. If you
112 have manually-installed plugins that you want to keep while
113 using this module, set this option to
114 `null`. You can generate this set with a
115 tool such as `jenkinsPlugins2nix`.
117 example = lib.literalExpression ''
118 import path/to/jenkinsPlugins2nix-generated-plugins.nix { inherit (pkgs) fetchurl stdenv; }
122 extraOptions = lib.mkOption {
123 type = lib.types.listOf lib.types.str;
125 example = [ "--debug=9" ];
127 Additional command line arguments to pass to Jenkins.
131 extraJavaOptions = lib.mkOption {
132 type = lib.types.listOf lib.types.str;
134 example = [ "-Xmx80m" ];
136 Additional command line arguments to pass to the Java run time (as opposed to Jenkins).
140 withCLI = lib.mkOption {
141 type = lib.types.bool;
144 Whether to make the CLI available.
146 More info about the CLI available at
148 https://www.jenkins.io/doc/book/managing/cli](https://www.jenkins.io/doc/book/managing/cli) .
154 config = lib.mkIf cfg.enable {
156 # server references the dejavu fonts
159 ] ++ lib.optional cfg.withCLI cfg.package;
162 // lib.optionalAttrs cfg.withCLI {
163 # Make it more convenient to use the `jenkins-cli`.
164 JENKINS_URL = jenkinsUrl;
168 users.groups = lib.optionalAttrs (cfg.group == "jenkins") {
169 jenkins.gid = config.ids.gids.jenkins;
172 users.users = lib.optionalAttrs (cfg.user == "jenkins") {
174 description = "jenkins user";
178 extraGroups = cfg.extraGroups;
179 useDefaultShell = true;
180 uid = config.ids.uids.jenkins;
184 systemd.services.jenkins = {
185 description = "Jenkins Continuous Integration Server";
186 after = [ "network.target" ];
187 wantedBy = [ "multi-user.target" ];
191 selectedSessionVars =
192 lib.filterAttrs (n: v: builtins.elem n [ "NIX_PATH" ])
193 config.environment.sessionVariables;
195 selectedSessionVars //
196 { JENKINS_HOME = cfg.home;
197 NIX_REMOTE = "daemon";
203 # Force .war (re)extraction, or else we might run stale Jenkins.
207 lib.optionalString (cfg.plugins != null) (
208 let pluginCmds = lib.mapAttrsToList
209 (n: v: "cp ${v} ${cfg.home}/plugins/${n}.jpi")
212 rm -r ${cfg.home}/plugins || true
213 mkdir -p ${cfg.home}/plugins
214 ${lib.concatStringsSep "\n" pluginCmds}
217 rm -rf ${cfg.home}/war
221 # For reference: https://wiki.jenkins.io/display/JENKINS/JenkinsLinuxStartupScript
223 ${pkgs.jdk17}/bin/java ${lib.concatStringsSep " " cfg.extraJavaOptions} -jar ${cfg.package}/webapps/jenkins.war --httpListenAddress=${cfg.listenAddress} \
224 --httpPort=${toString cfg.port} \
225 --prefix=${cfg.prefix} \
226 -Djava.awt.headless=true \
227 ${lib.concatStringsSep " " cfg.extraOptions}
231 until [[ $(${pkgs.curl.bin}/bin/curl -L -s --head -w '\n%{http_code}' ${jenkinsUrl} | tail -n1) =~ ^(200|403)$ ]]; do
238 StateDirectory = lib.mkIf (lib.hasPrefix "/var/lib/jenkins" cfg.home) "jenkins";
239 # For (possible) socket use
240 RuntimeDirectory = "jenkins";