hydra/hydra-module.nix

343 lines
9.7 KiB
Nix
Raw Normal View History

{ config, pkgs, lib ? pkgs.lib, ... }:
2011-05-05 06:27:38 +00:00
with lib;
2011-05-05 06:27:38 +00:00
let
2011-05-05 06:27:38 +00:00
cfg = config.services.hydra;
baseDir = "/var/lib/hydra";
hydraConf = pkgs.writeScript "hydra.conf" cfg.extraConfig;
2013-01-22 13:41:02 +00:00
hydraEnv =
{ HYDRA_DBI = cfg.dbi;
HYDRA_CONFIG = "${baseDir}/hydra.conf";
HYDRA_DATA = "${baseDir}";
};
env =
{ NIX_REMOTE = "daemon";
SSL_CERT_FILE = "/etc/ssl/certs/ca-certificates.crt";
2015-07-08 14:57:33 +00:00
OPENSSL_X509_CERT_FILE = "/etc/ssl/certs/ca-certificates.crt"; # FIXME: remove on NixOS >= 15.07
PGPASSFILE = "${baseDir}/pgpass";
} // hydraEnv // cfg.extraEnv;
serverEnv = env //
2013-11-06 16:27:36 +00:00
{ HYDRA_TRACKER = cfg.tracker;
COLUMNS = "80";
2015-07-02 00:55:15 +00:00
PGPASSFILE = "${baseDir}/pgpass-www"; # grrr
} // (optionalAttrs cfg.debugServer { DBIC_TRACE = 1; });
localDB = "dbi:Pg:dbname=hydra;user=hydra;";
haveLocalDB = cfg.dbi == localDB;
2011-05-05 06:27:38 +00:00
in
{
###### interface
options = {
2013-11-06 16:27:36 +00:00
2011-05-05 06:27:38 +00:00
services.hydra = rec {
2013-01-22 13:41:02 +00:00
2011-05-05 06:27:38 +00:00
enable = mkOption {
type = types.bool;
2011-05-05 06:27:38 +00:00
default = false;
description = ''
Whether to run Hydra services.
'';
};
dbi = mkOption {
2015-06-29 09:28:34 +00:00
type = types.str;
default = localDB;
example = "dbi:Pg:dbname=hydra;host=postgres.example.org;user=foo;";
2011-05-05 06:27:38 +00:00
description = ''
2011-05-05 10:07:59 +00:00
The DBI string for Hydra database connection.
2011-05-05 06:27:38 +00:00
'';
};
2013-01-22 13:41:02 +00:00
package = mkOption {
type = types.path;
#default = pkgs.hydra;
description = "The Hydra package.";
2011-05-05 06:27:38 +00:00
};
2013-01-22 13:41:02 +00:00
2011-05-05 06:27:38 +00:00
hydraURL = mkOption {
type = types.str;
2011-05-05 06:27:38 +00:00
description = ''
2013-01-22 13:41:02 +00:00
The base URL for the Hydra webserver instance. Used for links in emails.
2011-05-05 06:27:38 +00:00
'';
};
2011-05-05 10:07:56 +00:00
listenHost = mkOption {
type = types.str;
default = "*";
example = "localhost";
description = ''
The hostname or address to listen on or <literal>*</literal> to listen
on all interfaces.
'';
};
2011-05-05 10:07:56 +00:00
port = mkOption {
type = types.int;
2011-05-05 10:07:56 +00:00
default = 3000;
description = ''
TCP port the web server should listen to.
'';
};
2011-05-05 06:27:38 +00:00
minimumDiskFree = mkOption {
type = types.int;
default = 0;
2011-05-05 06:27:38 +00:00
description = ''
Threshold of minimum disk space (GiB) to determine if queue runner should run or not.
2011-05-05 06:27:38 +00:00
'';
};
minimumDiskFreeEvaluator = mkOption {
type = types.int;
default = 0;
2011-05-05 06:27:38 +00:00
description = ''
Threshold of minimum disk space (GiB) to determine if evaluator should run or not.
2011-05-05 06:27:38 +00:00
'';
};
notificationSender = mkOption {
type = types.str;
2011-05-05 06:27:38 +00:00
description = ''
2013-01-22 13:41:02 +00:00
Sender email address used for email notifications.
2011-05-05 06:27:38 +00:00
'';
2013-01-22 13:41:02 +00:00
};
2011-05-05 06:27:38 +00:00
tracker = mkOption {
type = types.str;
2011-05-05 06:27:38 +00:00
default = "";
description = ''
Piece of HTML that is included on all pages.
'';
2011-05-05 10:07:59 +00:00
};
logo = mkOption {
2013-11-06 16:27:36 +00:00
type = types.nullOr types.path;
2011-05-05 10:07:59 +00:00
default = null;
description = ''
Path to a file containing the logo of your Hydra instance.
2011-05-05 10:07:59 +00:00
'';
};
debugServer = mkOption {
type = types.bool;
default = false;
description = "Whether to run the server in debug mode.";
};
extraConfig = mkOption {
type = types.lines;
description = "Extra lines for the Hydra configuration.";
};
extraEnv = mkOption {
type = types.attrsOf types.str;
default = {};
description = "Extra environment variables for Hydra.";
};
2011-05-05 06:27:38 +00:00
};
};
2013-01-22 13:41:02 +00:00
2011-05-05 06:27:38 +00:00
###### implementation
config = mkIf cfg.enable {
2013-11-06 16:27:36 +00:00
users.extraGroups.hydra = { };
users.extraUsers.hydra =
{ description = "Hydra";
group = "hydra";
createHome = true;
home = baseDir;
useDefaultShell = true;
};
users.extraUsers.hydra-queue-runner =
{ description = "Hydra queue runner";
group = "hydra";
useDefaultShell = true;
2015-07-02 00:55:15 +00:00
home = "${baseDir}/queue-runner"; # really only to keep SSH happy
};
users.extraUsers.hydra-www =
{ description = "Hydra web server";
group = "hydra";
useDefaultShell = true;
};
nix.trustedUsers = [ "hydra-queue-runner" ];
services.hydra.package = mkDefault ((import ./release.nix {}).build.x86_64-linux);
services.hydra.extraConfig =
''
using_frontend_proxy 1
base_uri ${cfg.hydraURL}
notification_sender ${cfg.notificationSender}
max_servers 25
2013-11-06 16:27:36 +00:00
${optionalString (cfg.logo != null) ''
hydra_logo ${cfg.logo}
''}
'';
environment.systemPackages = [ cfg.package ];
2011-05-05 06:27:38 +00:00
environment.variables = hydraEnv;
2011-05-05 06:27:38 +00:00
nix.extraOptions = ''
gc-keep-outputs = true
gc-keep-derivations = true
# The default (`true') slows Nix down a lot since the build farm
# has so many GC roots.
gc-check-reachability = false
'';
2015-06-26 13:24:12 +00:00
systemd.services.hydra-init =
{ wantedBy = [ "multi-user.target" ];
2015-07-02 00:54:16 +00:00
requires = optional haveLocalDB "postgresql.service";
after = optional haveLocalDB "postgresql.service";
environment = env;
preStart = ''
mkdir -p ${baseDir}
chown hydra.hydra ${baseDir}
chmod 0750 ${baseDir}
ln -sf ${hydraConf} ${baseDir}/hydra.conf
2015-07-02 00:54:59 +00:00
mkdir -m 0700 -p ${baseDir}/www
chown hydra-www.hydra ${baseDir}/www
2015-07-02 00:54:59 +00:00
mkdir -m 0700 -p ${baseDir}/queue-runner
mkdir -m 0750 -p ${baseDir}/build-logs
chown hydra-queue-runner.hydra ${baseDir}/queue-runner ${baseDir}/build-logs
${optionalString haveLocalDB ''
if ! [ -e ${baseDir}/.db-created ]; then
${config.services.postgresql.package}/bin/createuser hydra
${config.services.postgresql.package}/bin/createdb -O hydra hydra
touch ${baseDir}/.db-created
fi
''}
2011-05-05 06:27:38 +00:00
'';
serviceConfig.ExecStart = "${cfg.package}/bin/hydra-init";
serviceConfig.PermissionsStartOnly = true;
serviceConfig.User = "hydra";
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
};
2015-06-26 13:24:12 +00:00
systemd.services.hydra-server =
{ wantedBy = [ "multi-user.target" ];
requires = [ "hydra-init.service" ];
after = [ "hydra-init.service" ];
environment = serverEnv;
serviceConfig =
{ ExecStart =
"@${cfg.package}/bin/hydra-server hydra-server -f -h '${cfg.listenHost}' "
+ "-p ${toString cfg.port} --max_spare_servers 5 --max_servers 25 "
+ "--max_requests 100 ${optionalString cfg.debugServer "-d"}";
User = "hydra-www";
PermissionsStartOnly = true;
Restart = "always";
};
2011-05-05 06:27:38 +00:00
};
2015-06-26 13:24:12 +00:00
systemd.services.hydra-queue-runner =
{ wantedBy = [ "multi-user.target" ];
requires = [ "hydra-init.service" ];
after = [ "hydra-init.service" "network.target" ];
path = [ pkgs.nettools ];
2015-07-02 00:55:15 +00:00
environment = env // {
PGPASSFILE = "${baseDir}/pgpass-queue-runner"; # grrr
};
serviceConfig =
{ ExecStartPre = "${cfg.package}/bin/hydra-queue-runner --unlock";
ExecStart = "@${cfg.package}/bin/hydra-queue-runner hydra-queue-runner";
2015-06-26 09:29:30 +00:00
ExecStopPost = "${cfg.package}/bin/hydra-queue-runner --unlock";
User = "hydra-queue-runner";
Restart = "always";
};
2011-05-05 06:27:38 +00:00
};
2015-06-26 13:24:12 +00:00
systemd.services.hydra-evaluator =
{ wantedBy = [ "multi-user.target" ];
requires = [ "hydra-init.service" ];
after = [ "hydra-init.service" "network.target" ];
path = [ pkgs.nettools ];
environment = env;
serviceConfig =
{ ExecStart = "@${cfg.package}/bin/hydra-evaluator hydra-evaluator";
User = "hydra";
Restart = "always";
};
2011-05-05 06:27:38 +00:00
};
2015-06-26 13:24:12 +00:00
systemd.services.hydra-update-gc-roots =
{ requires = [ "hydra-init.service" ];
after = [ "hydra-init.service" ];
environment = env;
serviceConfig =
{ ExecStart = "@${cfg.package}/bin/hydra-update-gc-roots hydra-update-gc-roots";
User = "hydra";
};
startAt = "2,14:15";
2011-05-05 06:27:38 +00:00
};
2015-06-26 13:24:12 +00:00
systemd.services.hydra-send-stats =
{ wantedBy = [ "multi-user.target" ];
after = [ "hydra-init.service" ];
environment = env;
serviceConfig =
{ ExecStart = "@${cfg.package}/bin/hydra-send-stats hydra-send-stats";
User = "hydra";
};
};
# If there is less than a certain amount of free disk space, stop
# the queue/evaluator to prevent builds from failing or aborting.
systemd.services.hydra-check-space =
{ script =
''
if [ $(($(stat -f -c '%a' /nix/store) * $(stat -f -c '%S' /nix/store))) -lt $((${toString cfg.minimumDiskFree} * 1024**3)) ]; then
echo "stopping Hydra queue runner due to lack of free space..."
systemctl stop hydra-queue-runner
fi
if [ $(($(stat -f -c '%a' /nix/store) * $(stat -f -c '%S' /nix/store))) -lt $((${toString cfg.minimumDiskFreeEvaluator} * 1024**3)) ]; then
echo "stopping Hydra evaluator due to lack of free space..."
systemctl stop hydra-evaluator
fi
'';
startAt = "*:0/5";
};
services.postgresql.enable = mkIf haveLocalDB true;
services.postgresql.identMap = optionalString haveLocalDB
''
hydra-users hydra hydra
hydra-users hydra-queue-runner hydra
hydra-users hydra-www hydra
hydra-users root hydra
'';
services.postgresql.authentication = optionalString haveLocalDB
''
local hydra all ident map=hydra-users
'';
};
2011-05-05 06:27:38 +00:00
}