2014-04-14 16:26:48 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2014-02-10 12:07:12 -08:00
|
|
|
let
|
|
|
|
cfg = config.services.jenkins;
|
2021-05-05 14:21:24 -07:00
|
|
|
jenkinsUrl = "http://${cfg.listenAddress}:${toString cfg.port}${cfg.prefix}";
|
2014-02-10 12:07:12 -08:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.jenkins = {
|
2025-05-30 22:27:27 +02:00
|
|
|
enable = lib.mkEnableOption "Jenkins, a continuous integration server";
|
2014-02-10 12:07:12 -08:00
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
user = lib.mkOption {
|
2014-02-10 12:07:12 -08:00
|
|
|
default = "jenkins";
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.str;
|
2014-02-10 12:07:12 -08:00
|
|
|
description = ''
|
2014-03-06 10:06:53 -08:00
|
|
|
User the jenkins server should execute under.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
group = lib.mkOption {
|
2014-03-06 10:06:53 -08:00
|
|
|
default = "jenkins";
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.str;
|
2014-03-06 10:06:53 -08:00
|
|
|
description = ''
|
2014-04-09 21:04:49 +02:00
|
|
|
If the default user "jenkins" is configured then this is the primary
|
|
|
|
group of that user.
|
2014-02-10 12:07:12 -08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
extraGroups = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2014-09-12 15:22:25 +02:00
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"wheel"
|
|
|
|
"dialout"
|
|
|
|
];
|
|
|
|
description = ''
|
|
|
|
List of extra groups that the "jenkins" user should be a part of.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
home = lib.mkOption {
|
2014-03-06 10:06:53 -08:00
|
|
|
default = "/var/lib/jenkins";
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.path;
|
2014-02-10 12:07:12 -08:00
|
|
|
description = ''
|
2014-03-06 10:06:53 -08:00
|
|
|
The path to use as JENKINS_HOME. If the default user "jenkins" is configured then
|
|
|
|
this is the home of the "jenkins" user.
|
2014-02-10 12:07:12 -08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
listenAddress = lib.mkOption {
|
2015-12-23 09:19:18 +01:00
|
|
|
default = "0.0.0.0";
|
|
|
|
example = "localhost";
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.str;
|
2015-12-23 09:19:18 +01:00
|
|
|
description = ''
|
2015-12-23 23:23:07 +01:00
|
|
|
Specifies the bind address on which the jenkins HTTP interface listens.
|
|
|
|
The default is the wildcard address.
|
2015-12-23 09:19:18 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
port = lib.mkOption {
|
2014-02-10 12:07:12 -08:00
|
|
|
default = 8080;
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.port;
|
2014-02-10 12:07:12 -08:00
|
|
|
description = ''
|
2015-12-23 09:19:18 +01:00
|
|
|
Specifies port number on which the jenkins HTTP interface listens.
|
|
|
|
The default is 8080.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
prefix = lib.mkOption {
|
2015-12-23 09:19:18 +01:00
|
|
|
default = "";
|
|
|
|
example = "/jenkins";
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.str;
|
2015-12-23 09:19:18 +01:00
|
|
|
description = ''
|
|
|
|
Specifies a urlPrefix to use with jenkins.
|
|
|
|
If the example /jenkins is given, the jenkins server will be
|
|
|
|
accessible using localhost:8080/jenkins.
|
2014-02-10 12:07:12 -08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
package = lib.mkPackageOption pkgs "jenkins" { };
|
2017-10-19 12:17:47 -06:00
|
|
|
|
2025-05-30 23:06:36 +02:00
|
|
|
javaPackage = lib.mkPackageOption pkgs "jdk21" { };
|
2025-05-30 22:37:11 +02:00
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
packages = lib.mkOption {
|
2022-10-11 21:16:37 +02:00
|
|
|
default = [
|
|
|
|
pkgs.stdenv
|
|
|
|
pkgs.git
|
2025-05-30 23:06:36 +02:00
|
|
|
pkgs.jdk21
|
2022-10-11 21:16:37 +02:00
|
|
|
config.programs.ssh.package
|
|
|
|
pkgs.nix
|
|
|
|
];
|
2024-08-27 20:42:54 +02:00
|
|
|
defaultText = lib.literalExpression "[ pkgs.stdenv pkgs.git pkgs.jdk17 config.programs.ssh.package pkgs.nix ]";
|
|
|
|
type = lib.types.listOf lib.types.package;
|
2014-02-10 12:07:12 -08:00
|
|
|
description = ''
|
|
|
|
Packages to add to PATH for the jenkins process.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
environment = lib.mkOption {
|
2015-10-04 16:11:28 +02:00
|
|
|
default = { };
|
2024-08-27 20:42:54 +02:00
|
|
|
type = with lib.types; attrsOf str;
|
2014-02-10 12:07:12 -08:00
|
|
|
description = ''
|
|
|
|
Additional environment variables to be passed to the jenkins process.
|
2016-02-01 10:18:31 +01:00
|
|
|
As a base environment, jenkins receives NIX_PATH from
|
|
|
|
{option}`environment.sessionVariables`, NIX_REMOTE is set to
|
|
|
|
"daemon" and JENKINS_HOME is set to the value of
|
|
|
|
{option}`services.jenkins.home`.
|
|
|
|
This option has precedence and can be used to override those
|
|
|
|
mentioned variables.
|
2014-02-10 12:07:12 -08:00
|
|
|
'';
|
|
|
|
};
|
2014-09-09 17:34:49 +02:00
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
plugins = lib.mkOption {
|
2017-05-20 22:44:34 +01:00
|
|
|
default = null;
|
2024-08-27 20:42:54 +02:00
|
|
|
type = lib.types.nullOr (lib.types.attrsOf lib.types.package);
|
2017-05-20 22:44:34 +01:00
|
|
|
description = ''
|
|
|
|
A set of plugins to activate. Note that this will completely
|
|
|
|
remove and replace any previously installed plugins. If you
|
|
|
|
have manually-installed plugins that you want to keep while
|
|
|
|
using this module, set this option to
|
|
|
|
`null`. You can generate this set with a
|
|
|
|
tool such as `jenkinsPlugins2nix`.
|
|
|
|
'';
|
2024-08-27 20:42:54 +02:00
|
|
|
example = lib.literalExpression ''
|
2017-05-20 22:44:34 +01:00
|
|
|
import path/to/jenkinsPlugins2nix-generated-plugins.nix { inherit (pkgs) fetchurl stdenv; }
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
extraOptions = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2014-09-09 17:34:49 +02:00
|
|
|
default = [ ];
|
2015-12-23 09:19:18 +01:00
|
|
|
example = [ "--debug=9" ];
|
2014-09-09 17:34:49 +02:00
|
|
|
description = ''
|
|
|
|
Additional command line arguments to pass to Jenkins.
|
|
|
|
'';
|
|
|
|
};
|
2017-06-16 07:09:32 +00:00
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
extraJavaOptions = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2017-06-16 07:09:32 +00:00
|
|
|
default = [ ];
|
|
|
|
example = [ "-Xmx80m" ];
|
|
|
|
description = ''
|
|
|
|
Additional command line arguments to pass to the Java run time (as opposed to Jenkins).
|
|
|
|
'';
|
|
|
|
};
|
2021-05-05 14:21:24 -07:00
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
withCLI = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-05 14:21:24 -07:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to make the CLI available.
|
|
|
|
|
|
|
|
More info about the CLI available at
|
2022-07-28 23:19:15 +02:00
|
|
|
[
|
2021-05-05 14:21:24 -07:00
|
|
|
https://www.jenkins.io/doc/book/managing/cli](https://www.jenkins.io/doc/book/managing/cli) .
|
|
|
|
'';
|
|
|
|
};
|
2014-02-10 12:07:12 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
config = lib.mkIf cfg.enable {
|
2021-05-05 14:21:24 -07:00
|
|
|
environment = {
|
|
|
|
# server references the dejavu fonts
|
|
|
|
systemPackages = [
|
|
|
|
pkgs.dejavu_fonts
|
2024-08-27 20:42:54 +02:00
|
|
|
] ++ lib.optional cfg.withCLI cfg.package;
|
2021-05-05 14:21:24 -07:00
|
|
|
|
|
|
|
variables =
|
|
|
|
{ }
|
2024-08-27 20:42:54 +02:00
|
|
|
// lib.optionalAttrs cfg.withCLI {
|
2021-05-05 14:21:24 -07:00
|
|
|
# Make it more convenient to use the `jenkins-cli`.
|
|
|
|
JENKINS_URL = jenkinsUrl;
|
|
|
|
};
|
|
|
|
};
|
2018-03-17 22:16:26 -07:00
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
users.groups = lib.optionalAttrs (cfg.group == "jenkins") {
|
2019-09-14 19:51:29 +02:00
|
|
|
jenkins.gid = config.ids.gids.jenkins;
|
2014-03-06 10:06:53 -08:00
|
|
|
};
|
|
|
|
|
2024-08-27 20:42:54 +02:00
|
|
|
users.users = lib.optionalAttrs (cfg.user == "jenkins") {
|
2019-09-14 19:51:29 +02:00
|
|
|
jenkins = {
|
|
|
|
description = "jenkins user";
|
|
|
|
createHome = true;
|
|
|
|
home = cfg.home;
|
|
|
|
group = cfg.group;
|
|
|
|
extraGroups = cfg.extraGroups;
|
|
|
|
useDefaultShell = true;
|
|
|
|
uid = config.ids.uids.jenkins;
|
|
|
|
};
|
2014-03-06 10:06:53 -08:00
|
|
|
};
|
2014-02-10 12:07:12 -08:00
|
|
|
|
|
|
|
systemd.services.jenkins = {
|
2014-02-25 15:44:07 -08:00
|
|
|
description = "Jenkins Continuous Integration Server";
|
2014-02-10 12:07:12 -08:00
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2015-10-04 16:11:28 +02:00
|
|
|
environment =
|
2015-10-06 21:41:13 +02:00
|
|
|
let
|
|
|
|
selectedSessionVars = lib.filterAttrs (
|
2016-02-01 10:18:31 +01:00
|
|
|
n: v: builtins.elem n [ "NIX_PATH" ]
|
2015-10-06 21:41:13 +02:00
|
|
|
) config.environment.sessionVariables;
|
|
|
|
in
|
|
|
|
selectedSessionVars
|
|
|
|
// {
|
|
|
|
JENKINS_HOME = cfg.home;
|
|
|
|
NIX_REMOTE = "daemon";
|
|
|
|
}
|
|
|
|
// cfg.environment;
|
2014-02-10 12:07:12 -08:00
|
|
|
|
|
|
|
path = cfg.packages;
|
|
|
|
|
2015-11-02 10:25:11 +01:00
|
|
|
# Force .war (re)extraction, or else we might run stale Jenkins.
|
2017-05-20 22:44:34 +01:00
|
|
|
|
|
|
|
preStart =
|
|
|
|
let
|
2024-08-27 20:42:54 +02:00
|
|
|
replacePlugins = lib.optionalString (cfg.plugins != null) (
|
2024-09-04 12:21:09 -04:00
|
|
|
let
|
2019-08-06 11:08:18 -04:00
|
|
|
pluginCmds = lib.mapAttrsToList (n: v: "cp ${v} ${cfg.home}/plugins/${n}.jpi") cfg.plugins;
|
2017-05-20 22:44:34 +01:00
|
|
|
in
|
|
|
|
''
|
2017-05-22 11:44:22 +01:00
|
|
|
rm -r ${cfg.home}/plugins || true
|
2017-05-20 22:44:34 +01:00
|
|
|
mkdir -p ${cfg.home}/plugins
|
2024-09-04 12:21:09 -04:00
|
|
|
${lib.concatStringsSep "\n" pluginCmds}
|
2023-06-24 20:19:19 +02:00
|
|
|
''
|
|
|
|
);
|
2017-05-20 22:44:34 +01:00
|
|
|
in
|
|
|
|
''
|
|
|
|
rm -rf ${cfg.home}/war
|
|
|
|
${replacePlugins}
|
|
|
|
'';
|
2015-11-02 10:25:11 +01:00
|
|
|
|
2018-03-17 22:16:59 -07:00
|
|
|
# For reference: https://wiki.jenkins.io/display/JENKINS/JenkinsLinuxStartupScript
|
2014-02-10 12:07:12 -08:00
|
|
|
script = ''
|
2025-05-30 22:37:11 +02:00
|
|
|
${cfg.javaPackage}/bin/java ${lib.concatStringsSep " " cfg.extraJavaOptions} -jar ${cfg.package}/webapps/jenkins.war --httpListenAddress=${cfg.listenAddress} \
|
2015-12-23 09:19:18 +01:00
|
|
|
--httpPort=${toString cfg.port} \
|
|
|
|
--prefix=${cfg.prefix} \
|
2018-03-17 22:16:59 -07:00
|
|
|
-Djava.awt.headless=true \
|
2024-08-27 20:42:54 +02:00
|
|
|
${lib.concatStringsSep " " cfg.extraOptions}
|
2014-02-10 12:07:12 -08:00
|
|
|
'';
|
|
|
|
|
|
|
|
postStart = ''
|
2021-05-05 14:21:24 -07:00
|
|
|
until [[ $(${pkgs.curl.bin}/bin/curl -L -s --head -w '\n%{http_code}' ${jenkinsUrl} | tail -n1) =~ ^(200|403)$ ]]; do
|
2016-05-12 03:22:39 +02:00
|
|
|
sleep 1
|
2014-02-10 12:07:12 -08:00
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = cfg.user;
|
2024-08-27 20:42:54 +02:00
|
|
|
StateDirectory = lib.mkIf (lib.hasPrefix "/var/lib/jenkins" cfg.home) "jenkins";
|
2024-06-23 18:00:35 +03:00
|
|
|
# For (possible) socket use
|
|
|
|
RuntimeDirectory = "jenkins";
|
2014-02-10 12:07:12 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|