2016-03-10 15:00:09 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2020-01-23 16:19:53 +00:00
|
|
|
cfg = config.services.buildkite-agents;
|
2018-02-18 16:35:27 +00:00
|
|
|
|
|
|
|
mkHookOption = { name, description, example ? null }: {
|
|
|
|
inherit name;
|
|
|
|
value = mkOption {
|
|
|
|
default = null;
|
2022-09-01 18:44:36 +01:00
|
|
|
description = lib.mdDoc description;
|
2018-02-18 16:35:27 +00:00
|
|
|
type = types.nullOr types.lines;
|
|
|
|
} // (if example == null then {} else { inherit example; });
|
|
|
|
};
|
|
|
|
mkHookOptions = hooks: listToAttrs (map mkHookOption hooks);
|
|
|
|
|
2020-01-23 16:19:53 +00:00
|
|
|
hooksDir = cfg: let
|
2018-02-18 16:35:27 +00:00
|
|
|
mkHookEntry = name: value: ''
|
2018-04-02 11:30:24 +01:00
|
|
|
cat > $out/${name} <<'EOF'
|
2018-03-01 19:38:53 +00:00
|
|
|
#! ${pkgs.runtimeShell}
|
2018-02-18 16:35:27 +00:00
|
|
|
set -e
|
|
|
|
${value}
|
|
|
|
EOF
|
|
|
|
chmod 755 $out/${name}
|
|
|
|
'';
|
2018-11-08 10:59:03 +00:00
|
|
|
in pkgs.runCommand "buildkite-agent-hooks" { preferLocalBuild = true; } ''
|
2018-02-18 16:35:27 +00:00
|
|
|
mkdir $out
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList mkHookEntry (filterAttrs (n: v: v != null) cfg.hooks))}
|
|
|
|
'';
|
|
|
|
|
2020-01-23 16:19:53 +00:00
|
|
|
buildkiteOptions = { name ? "", config, ... }: {
|
|
|
|
options = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Whether to enable this buildkite agent";
|
2020-01-23 16:19:53 +00:00
|
|
|
};
|
2016-03-10 15:00:09 +00:00
|
|
|
|
2017-10-20 18:16:05 +01:00
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.buildkite-agent;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.buildkite-agent";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Which buildkite-agent derivation to use";
|
2017-10-20 18:16:05 +01:00
|
|
|
type = types.package;
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
2020-01-23 16:19:53 +00:00
|
|
|
default = "/var/lib/buildkite-agent-${name}";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "The workdir for the agent";
|
2017-10-20 18:16:05 +01:00
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
runtimePackages = mkOption {
|
2020-01-19 20:49:19 +00:00
|
|
|
default = [ pkgs.bash pkgs.gnutar pkgs.gzip pkgs.git pkgs.nix ];
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "[ pkgs.bash pkgs.gnutar pkgs.gzip pkgs.git pkgs.nix ]";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Add programs to the buildkite-agent environment";
|
2017-10-20 18:16:05 +01:00
|
|
|
type = types.listOf types.package;
|
|
|
|
};
|
|
|
|
|
2017-11-23 16:09:35 +00:00
|
|
|
tokenPath = mkOption {
|
|
|
|
type = types.path;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-03-10 15:00:09 +00:00
|
|
|
The token from your Buildkite "Agents" page.
|
2017-11-23 16:09:35 +00:00
|
|
|
|
|
|
|
A run-time path to the token file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
2016-03-10 15:00:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
2020-01-23 16:19:53 +00:00
|
|
|
default = "%hostname-${name}-%n";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-23 16:19:53 +00:00
|
|
|
The name of the agent as seen in the buildkite dashboard.
|
2016-03-10 15:00:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-17 16:12:35 +00:00
|
|
|
tags = mkOption {
|
2021-04-22 13:23:52 +01:00
|
|
|
type = types.attrsOf (types.either types.str (types.listOf types.str));
|
2020-01-17 16:12:35 +00:00
|
|
|
default = {};
|
|
|
|
example = { queue = "default"; docker = "true"; ruby2 ="true"; };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-17 16:12:35 +00:00
|
|
|
Tags for the agent.
|
2016-03-10 15:00:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-02-18 16:19:26 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "debug=true";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-02-18 16:19:26 +00:00
|
|
|
Extra lines to be added verbatim to the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-17 15:03:00 +00:00
|
|
|
privateSshKeyPath = mkOption {
|
2020-01-20 09:28:47 +00:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2020-01-17 15:03:00 +00:00
|
|
|
## maximum care is taken so that secrets (ssh keys and the CI token)
|
|
|
|
## don't end up in the Nix store.
|
|
|
|
apply = final: if final == null then null else toString final;
|
2017-11-23 16:09:35 +00:00
|
|
|
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-17 15:03:00 +00:00
|
|
|
OpenSSH private key
|
2017-11-23 16:09:35 +00:00
|
|
|
|
2020-01-17 15:03:00 +00:00
|
|
|
A run-time path to the key file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
|
|
|
'';
|
|
|
|
};
|
2018-02-18 16:35:27 +00: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;
|
2020-01-23 16:19:53 +00:00
|
|
|
default = hooksDir config;
|
2022-08-20 21:27:20 +01:00
|
|
|
defaultText = literalMD "generated from {option}`services.buildkite-agents.<name>.hooks`";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-02-18 16:35:27 +00:00
|
|
|
Path to the directory storing the hooks.
|
2022-07-28 22:19:15 +01:00
|
|
|
Consider using {option}`services.buildkite-agents.<name>.hooks.<name>`
|
2018-02-18 16:35:27 +00:00
|
|
|
instead.
|
|
|
|
'';
|
|
|
|
};
|
2018-09-06 13:02:55 +01:00
|
|
|
|
|
|
|
shell = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${pkgs.bash}/bin/bash -e -c";
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression ''"''${pkgs.bash}/bin/bash -e -c"'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-09-06 13:02:55 +01:00
|
|
|
Command that buildkite-agent 3 will execute when it spawns a shell.
|
|
|
|
'';
|
|
|
|
};
|
2016-03-10 15:00:09 +00:00
|
|
|
};
|
|
|
|
};
|
2020-01-23 16:19:53 +00:00
|
|
|
enabledAgents = lib.filterAttrs (n: v: v.enable) cfg;
|
|
|
|
mapAgents = function: lib.mkMerge (lib.mapAttrsToList function enabledAgents);
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.buildkite-agents = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule buildkiteOptions);
|
|
|
|
default = {};
|
2022-08-14 08:03:30 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-23 16:19:53 +00:00
|
|
|
Attribute set of buildkite agents.
|
|
|
|
The attribute key is combined with the hostname and a unique integer to
|
|
|
|
create the final agent name. This can be overridden by setting the `name`
|
|
|
|
attribute.
|
|
|
|
'';
|
|
|
|
};
|
2016-03-10 15:00:09 +00:00
|
|
|
|
2020-01-23 16:19:53 +00:00
|
|
|
config.users.users = mapAgents (name: cfg: {
|
|
|
|
"buildkite-agent-${name}" = {
|
|
|
|
name = "buildkite-agent-${name}";
|
2020-01-19 20:19:35 +00:00
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
description = "Buildkite agent user";
|
|
|
|
extraGroups = [ "keys" ];
|
|
|
|
isSystemUser = true;
|
2020-02-28 14:31:18 +00:00
|
|
|
group = "buildkite-agent-${name}";
|
2020-01-19 20:19:35 +00:00
|
|
|
};
|
2020-01-23 16:19:53 +00:00
|
|
|
});
|
2020-02-28 14:31:18 +00:00
|
|
|
config.users.groups = mapAgents (name: cfg: {
|
|
|
|
"buildkite-agent-${name}" = {};
|
|
|
|
});
|
2016-03-10 15:00:09 +00:00
|
|
|
|
2020-01-23 16:19:53 +00:00
|
|
|
config.systemd.services = mapAgents (name: cfg: {
|
|
|
|
"buildkite-agent-${name}" =
|
2016-03-10 15:00:09 +00:00
|
|
|
{ description = "Buildkite Agent";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2020-01-23 16:19:53 +00:00
|
|
|
path = cfg.runtimePackages ++ [ cfg.package pkgs.coreutils ];
|
2017-10-20 18:16:05 +01:00
|
|
|
environment = config.networking.proxy.envVars // {
|
|
|
|
HOME = cfg.dataDir;
|
|
|
|
NIX_REMOTE = "daemon";
|
|
|
|
};
|
2016-03-10 15:00:09 +00:00
|
|
|
|
2017-11-23 16:09:35 +00:00
|
|
|
## NB: maximum care is taken so that secrets (ssh keys and the CI token)
|
|
|
|
## don't end up in the Nix store.
|
2018-02-18 08:30:42 +00:00
|
|
|
preStart = let
|
|
|
|
sshDir = "${cfg.dataDir}/.ssh";
|
2021-04-22 13:23:52 +01:00
|
|
|
tagStr = name: value:
|
|
|
|
if lib.isList value
|
|
|
|
then lib.concatStringsSep "," (builtins.map (v: "${name}=${v}") value)
|
|
|
|
else "${name}=${value}";
|
|
|
|
tagsStr = lib.concatStringsSep "," (lib.mapAttrsToList tagStr cfg.tags);
|
2018-02-18 08:30:42 +00:00
|
|
|
in
|
2020-01-20 09:28:47 +00:00
|
|
|
optionalString (cfg.privateSshKeyPath != null) ''
|
2018-02-18 08:30:42 +00:00
|
|
|
mkdir -m 0700 -p "${sshDir}"
|
2021-05-01 17:36:37 +01:00
|
|
|
install -m600 "${toString cfg.privateSshKeyPath}" "${sshDir}/id_rsa"
|
2020-01-20 09:28:47 +00:00
|
|
|
'' + ''
|
2017-11-23 16:09:35 +00:00
|
|
|
cat > "${cfg.dataDir}/buildkite-agent.cfg" <<EOF
|
|
|
|
token="$(cat ${toString cfg.tokenPath})"
|
|
|
|
name="${cfg.name}"
|
2018-09-06 13:02:55 +01:00
|
|
|
shell="${cfg.shell}"
|
2021-04-22 13:23:52 +01:00
|
|
|
tags="${tagsStr}"
|
2017-11-23 16:09:35 +00:00
|
|
|
build-path="${cfg.dataDir}/builds"
|
2018-01-05 22:43:16 +00:00
|
|
|
hooks-path="${cfg.hooksPath}"
|
2018-02-18 16:19:26 +00:00
|
|
|
${cfg.extraConfig}
|
2017-11-23 16:09:35 +00:00
|
|
|
EOF
|
|
|
|
'';
|
2016-03-10 15:00:09 +00:00
|
|
|
|
|
|
|
serviceConfig =
|
2020-01-23 16:19:53 +00:00
|
|
|
{ ExecStart = "${cfg.package}/bin/buildkite-agent start --config ${cfg.dataDir}/buildkite-agent.cfg";
|
|
|
|
User = "buildkite-agent-${name}";
|
2016-03-10 15:00:09 +00:00
|
|
|
RestartSec = 5;
|
|
|
|
Restart = "on-failure";
|
|
|
|
TimeoutSec = 10;
|
2020-01-17 15:47:34 +00:00
|
|
|
# set a long timeout to give buildkite-agent a chance to finish current builds
|
|
|
|
TimeoutStopSec = "2 min";
|
|
|
|
KillMode = "mixed";
|
2016-03-10 15:00:09 +00:00
|
|
|
};
|
|
|
|
};
|
2020-01-23 16:19:53 +00:00
|
|
|
});
|
2018-02-18 16:35:27 +00:00
|
|
|
|
2020-01-23 16:19:53 +00:00
|
|
|
config.assertions = mapAgents (name: cfg: [
|
2020-02-19 11:22:35 +00:00
|
|
|
{ assertion = cfg.hooksPath == (hooksDir cfg) || all (v: v == null) (attrValues cfg.hooks);
|
2018-02-18 16:35:27 +00:00
|
|
|
message = ''
|
2020-01-23 16:19:53 +00:00
|
|
|
Options `services.buildkite-agents.${name}.hooksPath' and
|
|
|
|
`services.buildkite-agents.${name}.hooks.<name>' are mutually exclusive.
|
2018-02-18 16:35:27 +00:00
|
|
|
'';
|
|
|
|
}
|
2020-01-23 16:19:53 +00:00
|
|
|
]);
|
|
|
|
|
2017-11-23 16:09:35 +00:00
|
|
|
imports = [
|
2020-01-24 18:31:33 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "buildkite-agent"] "services.buildkite-agent has been upgraded from version 2 to version 3 and moved to an attribute set at services.buildkite-agents. Please consult the 20.03 release notes for more information.")
|
2017-11-23 16:09:35 +00:00
|
|
|
];
|
2016-03-10 15:00:09 +00:00
|
|
|
}
|