2016-03-10 16:00:09 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.buildkite-agent;
|
2018-02-18 17:35:27 +01:00
|
|
|
|
|
|
|
mkHookOption = { name, description, example ? null }: {
|
|
|
|
inherit name;
|
|
|
|
value = mkOption {
|
|
|
|
default = null;
|
|
|
|
inherit description;
|
|
|
|
type = types.nullOr types.lines;
|
|
|
|
} // (if example == null then {} else { inherit example; });
|
|
|
|
};
|
|
|
|
mkHookOptions = hooks: listToAttrs (map mkHookOption hooks);
|
|
|
|
|
|
|
|
hooksDir = let
|
|
|
|
mkHookEntry = name: value: ''
|
|
|
|
cat > $out/${name} <<EOF
|
|
|
|
#! ${pkgs.stdenv.shell}
|
|
|
|
set -e
|
|
|
|
${value}
|
|
|
|
EOF
|
|
|
|
chmod 755 $out/${name}
|
|
|
|
'';
|
|
|
|
in pkgs.runCommand "buildkite-agent-hooks" {} ''
|
|
|
|
mkdir $out
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList mkHookEntry (filterAttrs (n: v: v != null) cfg.hooks))}
|
|
|
|
'';
|
|
|
|
|
2016-03-10 16:00:09 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.buildkite-agent = {
|
|
|
|
enable = mkEnableOption "buildkite-agent";
|
|
|
|
|
2017-10-20 19:16:05 +02:00
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.buildkite-agent;
|
|
|
|
defaultText = "pkgs.buildkite-agent";
|
|
|
|
description = "Which buildkite-agent derivation to use";
|
|
|
|
type = types.package;
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
default = "/var/lib/buildkite-agent";
|
|
|
|
description = "The workdir for the agent";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
runtimePackages = mkOption {
|
2017-10-26 18:42:14 +02:00
|
|
|
default = [ pkgs.bash pkgs.nix ];
|
|
|
|
defaultText = "[ pkgs.bash pkgs.nix ]";
|
2017-10-20 19:16:05 +02:00
|
|
|
description = "Add programs to the buildkite-agent environment";
|
|
|
|
type = types.listOf types.package;
|
|
|
|
};
|
|
|
|
|
2017-11-23 17:09:35 +01:00
|
|
|
tokenPath = mkOption {
|
|
|
|
type = types.path;
|
2016-03-10 16:00:09 +01:00
|
|
|
description = ''
|
|
|
|
The token from your Buildkite "Agents" page.
|
2017-11-23 17:09:35 +01:00
|
|
|
|
|
|
|
A run-time path to the token file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
2016-03-10 16:00:09 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
The name of the agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
meta-data = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2018-02-18 17:42:13 +01:00
|
|
|
example = "queue=default,docker=true,ruby2=true";
|
2016-03-10 16:00:09 +01:00
|
|
|
description = ''
|
2018-02-18 17:42:13 +01:00
|
|
|
Meta data for the agent. This is a comma-separated list of
|
|
|
|
<code>key=value</code> pairs.
|
2016-03-10 16:00:09 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:26 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "debug=true";
|
|
|
|
description = ''
|
|
|
|
Extra lines to be added verbatim to the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-03-10 16:00:09 +01:00
|
|
|
openssh =
|
2017-11-23 17:09:35 +01:00
|
|
|
{ privateKeyPath = mkOption {
|
|
|
|
type = types.path;
|
2016-03-10 16:00:09 +01:00
|
|
|
description = ''
|
|
|
|
Private agent key.
|
2017-11-23 17:09:35 +01:00
|
|
|
|
|
|
|
A run-time path to the key file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
2016-03-10 16:00:09 +01:00
|
|
|
'';
|
|
|
|
};
|
2017-11-23 17:09:35 +01:00
|
|
|
publicKeyPath = mkOption {
|
|
|
|
type = types.path;
|
2016-03-10 16:00:09 +01:00
|
|
|
description = ''
|
|
|
|
Public agent key.
|
2017-11-23 17:09:35 +01:00
|
|
|
|
|
|
|
A run-time path to the key file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
2016-03-10 16:00:09 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2018-02-18 17:35:27 +01:00
|
|
|
|
|
|
|
hooks = mkHookOptions [
|
|
|
|
{ name = "checkout";
|
|
|
|
description = ''
|
|
|
|
The `checkout` hook script will replace the default checkout routine of the
|
|
|
|
bootstrap.sh script. You can use this hook to do your own SCM checkout
|
|
|
|
behaviour
|
|
|
|
''; }
|
|
|
|
{ name = "command";
|
|
|
|
description = ''
|
|
|
|
The `command` hook script will replace the default implementation of running
|
|
|
|
the build command.
|
|
|
|
''; }
|
|
|
|
{ name = "environment";
|
|
|
|
description = ''
|
|
|
|
The `environment` hook will run before all other commands, and can be used
|
|
|
|
to set up secrets, data, etc. Anything exported in hooks will be available
|
|
|
|
to the build script.
|
|
|
|
|
|
|
|
Note: the contents of this file will be copied to the world-readable
|
|
|
|
Nix store.
|
|
|
|
'';
|
|
|
|
example = ''
|
|
|
|
export SECRET_VAR=`head -1 /run/keys/secret`
|
|
|
|
''; }
|
|
|
|
{ name = "post-artifact";
|
|
|
|
description = ''
|
|
|
|
The `post-artifact` hook will run just after artifacts are uploaded
|
|
|
|
''; }
|
|
|
|
{ name = "post-checkout";
|
|
|
|
description = ''
|
|
|
|
The `post-checkout` hook will run after the bootstrap script has checked out
|
|
|
|
your projects source code.
|
|
|
|
''; }
|
|
|
|
{ name = "post-command";
|
|
|
|
description = ''
|
|
|
|
The `post-command` hook will run after the bootstrap script has run your
|
|
|
|
build commands
|
|
|
|
''; }
|
|
|
|
{ name = "pre-artifact";
|
|
|
|
description = ''
|
|
|
|
The `pre-artifact` hook will run just before artifacts are uploaded
|
|
|
|
''; }
|
|
|
|
{ name = "pre-checkout";
|
|
|
|
description = ''
|
|
|
|
The `pre-checkout` hook will run just before your projects source code is
|
|
|
|
checked out from your SCM provider
|
|
|
|
''; }
|
|
|
|
{ name = "pre-command";
|
|
|
|
description = ''
|
|
|
|
The `pre-command` hook will run just before your build command runs
|
|
|
|
''; }
|
|
|
|
{ name = "pre-exit";
|
|
|
|
description = ''
|
|
|
|
The `pre-exit` hook will run just before your build job finishes
|
|
|
|
''; }
|
|
|
|
];
|
|
|
|
|
|
|
|
hooksPath = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = hooksDir;
|
|
|
|
defaultText = "generated from services.buildkite-agent.hooks";
|
|
|
|
description = ''
|
|
|
|
Path to the directory storing the hooks.
|
|
|
|
Consider using <option>services.buildkite-agent.hooks.<name></option>
|
|
|
|
instead.
|
|
|
|
'';
|
|
|
|
};
|
2016-03-10 16:00:09 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.services.buildkite-agent.enable {
|
|
|
|
users.extraUsers.buildkite-agent =
|
|
|
|
{ name = "buildkite-agent";
|
2017-10-20 19:16:05 +02:00
|
|
|
home = cfg.dataDir;
|
2016-03-10 16:00:09 +01:00
|
|
|
createHome = true;
|
|
|
|
description = "Buildkite agent user";
|
2017-11-23 17:09:35 +01:00
|
|
|
extraGroups = [ "keys" ];
|
2016-03-10 16:00:09 +01:00
|
|
|
};
|
|
|
|
|
2017-10-20 19:16:05 +02:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2016-03-10 16:00:09 +01:00
|
|
|
|
|
|
|
systemd.services.buildkite-agent =
|
2017-11-23 17:09:35 +01:00
|
|
|
let copy = x: target: perms:
|
|
|
|
"cp -f ${x} ${target}; ${pkgs.coreutils}/bin/chmod ${toString perms} ${target}; ";
|
|
|
|
in
|
2016-03-10 16:00:09 +01:00
|
|
|
{ description = "Buildkite Agent";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2017-10-20 19:16:05 +02:00
|
|
|
path = cfg.runtimePackages;
|
|
|
|
environment = config.networking.proxy.envVars // {
|
|
|
|
HOME = cfg.dataDir;
|
|
|
|
NIX_REMOTE = "daemon";
|
|
|
|
};
|
2016-03-10 16:00:09 +01:00
|
|
|
|
2017-11-23 17:09:35 +01:00
|
|
|
## NB: maximum care is taken so that secrets (ssh keys and the CI token)
|
|
|
|
## don't end up in the Nix store.
|
|
|
|
preStart = ''
|
|
|
|
${pkgs.coreutils}/bin/mkdir -m 0700 -p ${cfg.dataDir}/.ssh
|
|
|
|
${copy (toString cfg.openssh.privateKeyPath) "${cfg.dataDir}/.ssh/id_rsa" 600}
|
|
|
|
${copy (toString cfg.openssh.publicKeyPath) "${cfg.dataDir}/.ssh/id_rsa.pub" 600}
|
|
|
|
|
|
|
|
cat > "${cfg.dataDir}/buildkite-agent.cfg" <<EOF
|
|
|
|
token="$(cat ${toString cfg.tokenPath})"
|
|
|
|
name="${cfg.name}"
|
|
|
|
meta-data="${cfg.meta-data}"
|
|
|
|
build-path="${cfg.dataDir}/builds"
|
2018-01-05 23:43:16 +01:00
|
|
|
hooks-path="${cfg.hooksPath}"
|
2018-02-18 17:19:26 +01:00
|
|
|
${cfg.extraConfig}
|
2017-11-23 17:09:35 +01:00
|
|
|
EOF
|
|
|
|
'';
|
2016-03-10 16:00:09 +01:00
|
|
|
|
|
|
|
serviceConfig =
|
2017-11-23 17:09:35 +01:00
|
|
|
{ ExecStart = "${pkgs.buildkite-agent}/bin/buildkite-agent start --config /var/lib/buildkite-agent/buildkite-agent.cfg";
|
2016-03-10 16:00:09 +01:00
|
|
|
User = "buildkite-agent";
|
|
|
|
RestartSec = 5;
|
|
|
|
Restart = "on-failure";
|
|
|
|
TimeoutSec = 10;
|
|
|
|
};
|
|
|
|
};
|
2018-02-18 17:35:27 +01:00
|
|
|
|
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.hooksPath == hooksDir || all isNull (attrValues cfg.hooks);
|
|
|
|
message = ''
|
|
|
|
Options `services.buildkite-agent.hooksPath' and
|
|
|
|
`services.buildkite-agent.hooks.<name>' are mutually exclusive.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
2016-03-10 16:00:09 +01:00
|
|
|
};
|
2017-11-23 17:09:35 +01:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "buildkite-agent" "token" ] [ "services" "buildkite-agent" "tokenPath" ])
|
|
|
|
(mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "privateKey" ] [ "services" "buildkite-agent" "openssh" "privateKeyPath" ])
|
|
|
|
(mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "publicKey" ] [ "services" "buildkite-agent" "openssh" "publicKeyPath" ])
|
|
|
|
];
|
2016-03-10 16:00:09 +01:00
|
|
|
}
|