2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.buildkite-agents;
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
hooksDir = hooks:
|
|
|
|
let
|
|
|
|
mkHookEntry = name: text: ''
|
|
|
|
ln --symbolic ${pkgs.writeShellApplication { inherit name text; }}/bin/${name} $out/${name}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
pkgs.runCommandLocal "buildkite-agent-hooks" { } ''
|
|
|
|
mkdir $out
|
|
|
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList mkHookEntry hooks)}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
buildkiteOptions = { name ? "", config, ... }: {
|
|
|
|
options = {
|
2023-08-04 22:07:22 +00:00
|
|
|
enable = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = true;
|
2023-08-04 22:07:22 +00:00
|
|
|
type = lib.types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether to enable this buildkite agent";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
package = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = pkgs.buildkite-agent;
|
2023-08-04 22:07:22 +00:00
|
|
|
defaultText = lib.literalExpression "pkgs.buildkite-agent";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Which buildkite-agent derivation to use";
|
2023-08-04 22:07:22 +00:00
|
|
|
type = lib.types.package;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
dataDir = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "/var/lib/buildkite-agent-${name}";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The workdir for the agent";
|
2023-08-04 22:07:22 +00:00
|
|
|
type = lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
extraGroups = lib.mkOption {
|
|
|
|
default = [ "keys" ];
|
|
|
|
description = lib.mdDoc "Groups the user for this buildkite agent should belong to";
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
runtimePackages = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [ pkgs.bash pkgs.gnutar pkgs.gzip pkgs.git pkgs.nix ];
|
2023-08-04 22:07:22 +00:00
|
|
|
defaultText = lib.literalExpression "[ pkgs.bash pkgs.gnutar pkgs.gzip pkgs.git pkgs.nix ]";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Add programs to the buildkite-agent environment";
|
2023-08-04 22:07:22 +00:00
|
|
|
type = lib.types.listOf lib.types.package;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
tokenPath = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The token from your Buildkite "Agents" page.
|
|
|
|
|
|
|
|
A run-time path to the token file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "%hostname-${name}-%n";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The name of the agent as seen in the buildkite dashboard.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
tags = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.either lib.types.str (lib.types.listOf lib.types.str));
|
|
|
|
default = { };
|
|
|
|
example = { queue = "default"; docker = "true"; ruby2 = "true"; };
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Tags for the agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
extraConfig = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
example = "debug=true";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Extra lines to be added verbatim to the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
privateSshKeyPath = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
|
|
|
## 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;
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
OpenSSH private key
|
|
|
|
|
|
|
|
A run-time path to the key file, which is supposed to be provisioned
|
|
|
|
outside of Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
hooks = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf lib.types.lines;
|
|
|
|
default = { };
|
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
environment = '''
|
|
|
|
export SECRET_VAR=`head -1 /run/keys/secret`
|
|
|
|
''';
|
|
|
|
}'';
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
"Agent" hooks to install.
|
|
|
|
See <https://buildkite.com/docs/agent/v3/hooks> for possible options.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
hooksPath = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
default = hooksDir config.hooks;
|
|
|
|
defaultText = lib.literalMD "generated from {option}`services.buildkite-agents.<name>.hooks`";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Path to the directory storing the hooks.
|
2022-08-12 12:06:08 +00:00
|
|
|
Consider using {option}`services.buildkite-agents.<name>.hooks.<name>`
|
2020-04-24 23:36:52 +00:00
|
|
|
instead.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
shell = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "${pkgs.bash}/bin/bash -e -c";
|
2023-08-04 22:07:22 +00:00
|
|
|
defaultText = lib.literalExpression ''"''${pkgs.bash}/bin/bash -e -c"'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Command that buildkite-agent 3 will execute when it spawns a shell.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
enabledAgents = lib.filterAttrs (n: v: v.enable) cfg;
|
|
|
|
mapAgents = function: lib.mkMerge (lib.mapAttrsToList function enabledAgents);
|
|
|
|
in
|
|
|
|
{
|
2023-08-04 22:07:22 +00:00
|
|
|
options.services.buildkite-agents = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule buildkiteOptions);
|
|
|
|
default = { };
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
config.users.users = mapAgents (name: cfg: {
|
|
|
|
"buildkite-agent-${name}" = {
|
|
|
|
name = "buildkite-agent-${name}";
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
description = "Buildkite agent user";
|
2024-01-02 11:29:13 +00:00
|
|
|
extraGroups = cfg.extraGroups;
|
2020-04-24 23:36:52 +00:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "buildkite-agent-${name}";
|
|
|
|
};
|
|
|
|
});
|
|
|
|
config.users.groups = mapAgents (name: cfg: {
|
2023-08-04 22:07:22 +00:00
|
|
|
"buildkite-agent-${name}" = { };
|
2020-04-24 23:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
config.systemd.services = mapAgents (name: cfg: {
|
2023-08-04 22:07:22 +00:00
|
|
|
"buildkite-agent-${name}" = {
|
|
|
|
description = "Buildkite Agent";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
path = cfg.runtimePackages ++ [ cfg.package pkgs.coreutils ];
|
|
|
|
environment = config.networking.proxy.envVars // {
|
|
|
|
HOME = cfg.dataDir;
|
|
|
|
NIX_REMOTE = "daemon";
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
## NB: maximum care is taken so that secrets (ssh keys and the CI token)
|
|
|
|
## don't end up in the Nix store.
|
|
|
|
preStart =
|
|
|
|
let
|
2020-04-24 23:36:52 +00:00
|
|
|
sshDir = "${cfg.dataDir}/.ssh";
|
2021-05-03 20:48:10 +00: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);
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
2023-08-04 22:07:22 +00:00
|
|
|
lib.optionalString (cfg.privateSshKeyPath != null) ''
|
|
|
|
mkdir -m 0700 -p "${sshDir}"
|
|
|
|
install -m600 "${toString cfg.privateSshKeyPath}" "${sshDir}/id_rsa"
|
|
|
|
'' + ''
|
|
|
|
cat > "${cfg.dataDir}/buildkite-agent.cfg" <<EOF
|
|
|
|
token="$(cat ${toString cfg.tokenPath})"
|
|
|
|
name="${cfg.name}"
|
|
|
|
shell="${cfg.shell}"
|
|
|
|
tags="${tagsStr}"
|
|
|
|
build-path="${cfg.dataDir}/builds"
|
|
|
|
hooks-path="${cfg.hooksPath}"
|
|
|
|
${cfg.extraConfig}
|
|
|
|
EOF
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/buildkite-agent start --config ${cfg.dataDir}/buildkite-agent.cfg";
|
|
|
|
User = "buildkite-agent-${name}";
|
|
|
|
RestartSec = 5;
|
|
|
|
Restart = "on-failure";
|
|
|
|
TimeoutSec = 10;
|
|
|
|
# set a long timeout to give buildkite-agent a chance to finish current builds
|
|
|
|
TimeoutStopSec = "2 min";
|
|
|
|
KillMode = "mixed";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2023-08-04 22:07:22 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
});
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
config.assertions = mapAgents (name: cfg: [{
|
|
|
|
assertion = cfg.hooksPath != hooksDir cfg.hooks -> cfg.hooks == { };
|
|
|
|
message = ''
|
|
|
|
Options `services.buildkite-agents.${name}.hooksPath' and
|
|
|
|
`services.buildkite-agents.${name}.hooks.<name>' are mutually exclusive.
|
|
|
|
'';
|
|
|
|
}]);
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|