2021-07-26 02:28:05 +02:00
|
|
|
{ pkgs, lib, config, ... }:
|
|
|
|
with lib;
|
|
|
|
with types;
|
|
|
|
let
|
2021-07-26 02:47:50 +02:00
|
|
|
runners = config.services.drone-exec-runner;
|
|
|
|
runnerOpts = {
|
|
|
|
options = {
|
2021-07-26 02:28:05 +02:00
|
|
|
enable = mkEnableOption "Enable an Drone CI/CD Exec Runner";
|
2021-07-26 03:07:00 +02:00
|
|
|
restartIfChanged = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = true;
|
|
|
|
description = ''Restart the runner if configuration changes.
|
|
|
|
Consider the scenario where the runner runs on the same machine where it gets deployed and
|
|
|
|
this runner is redeploying itself.
|
|
|
|
If restart if changed is true, the runner gets killed during the process, this is very bad.
|
|
|
|
To enable these scenarios, restart manually the runners once deployment is done.
|
|
|
|
'';
|
|
|
|
};
|
2021-07-26 02:49:47 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = package;
|
|
|
|
default = pkgs.drone-runner-exec;
|
|
|
|
defaultText = "pkgs.drone-runner-exec";
|
|
|
|
};
|
|
|
|
env = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
description = "Environment strings (e.g. DRONE_RUNNER_CAPACITY, CLIENT_DRONE_RPC_HOST, etc.)";
|
|
|
|
example = [
|
|
|
|
"DRONE_RUNNER_CAPACITY=10"
|
|
|
|
"CLIENT_DRONE_RPC_HOST=127.0.0.1:3030"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
envFile = mkOption {
|
|
|
|
type = str;
|
|
|
|
description = "Path to the environment file (may contains secrets, notably the shared RPC secret)";
|
|
|
|
};
|
|
|
|
user = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "drone-runner-exec";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "drone-runner-exec";
|
|
|
|
};
|
|
|
|
allowedPackages = mkOption {
|
|
|
|
type = listOf package;
|
|
|
|
default = with pkgs; [
|
|
|
|
git
|
|
|
|
gnutar
|
|
|
|
bash
|
|
|
|
nix
|
|
|
|
gzip
|
|
|
|
];
|
2021-07-26 02:28:05 +02:00
|
|
|
};
|
|
|
|
};
|
2021-07-26 02:49:47 +02:00
|
|
|
};
|
2021-07-26 02:47:50 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.drone-exec-runner = mkOption {
|
|
|
|
type = attrsOf (submodule runnerOpts);
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
|
2021-07-26 02:50:51 +02:00
|
|
|
config = mkIf (any (cfg: cfg.enable) (attrValues runners)) {
|
2021-07-26 02:47:50 +02:00
|
|
|
systemd.services = mapAttrs' (runnerName: cfg: nameValuePair ("drone-exec-runner-${runnerName}") ({
|
2021-07-26 02:28:05 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2021-07-26 03:07:59 +02:00
|
|
|
inherit (cfg) restartIfChanged;
|
2021-07-26 03:07:00 +02:00
|
|
|
|
2021-07-26 02:28:05 +02:00
|
|
|
confinement.enable = true;
|
|
|
|
confinement.packages = cfg.allowedPackages;
|
|
|
|
path = cfg.allowedPackages;
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Environment = [
|
|
|
|
"NIX_REMOTE=daemon"
|
|
|
|
"PAGER=cat"
|
2021-07-26 02:58:21 +02:00
|
|
|
"DRONE_RUNNER_NAME=${runnerName}"
|
2021-07-26 03:33:27 +02:00
|
|
|
"NIX_PATH=nixpkgs=/var/nixpkgs"
|
2021-07-26 02:28:05 +02:00
|
|
|
] ++ cfg.env;
|
|
|
|
|
|
|
|
BindPaths = [
|
|
|
|
"/nix/var/nix/daemon-socket/socket"
|
|
|
|
"/run/nscd/socket"
|
|
|
|
];
|
|
|
|
|
|
|
|
BindReadOnlyPaths = [
|
|
|
|
"/etc/passwd:/etc/passwd"
|
|
|
|
"/etc/group:/etc/group"
|
|
|
|
"/nix/var/nix/profiles/system/etc/nix:/etc/nix"
|
2021-07-26 03:33:27 +02:00
|
|
|
"/nix/var/nix/profiles/per-user/root/channels/nixos:/var/nixpkgs"
|
2021-07-26 02:28:05 +02:00
|
|
|
"${config.environment.etc."ssl/certs/ca-certificates.crt".source}:/etc/ssl/certs/ca-certificates.crt"
|
|
|
|
"${config.environment.etc."ssh/ssh_known_hosts".source}:/etc/ssh/ssh_known_hosts"
|
|
|
|
"/etc/machine-id"
|
|
|
|
"/nix/"
|
|
|
|
];
|
|
|
|
|
|
|
|
EnvironmentFile = [
|
|
|
|
cfg.envFile
|
|
|
|
];
|
|
|
|
|
|
|
|
ExecStart = "${cfg.package}/bin/drone-runner-exec";
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
};
|
2021-07-26 02:49:47 +02:00
|
|
|
})) runners;
|
2021-07-26 02:28:05 +02:00
|
|
|
|
2021-07-26 02:47:50 +02:00
|
|
|
users.users = mapAttrs' (_: cfg: nameValuePair cfg.user ({
|
2021-07-26 02:28:05 +02:00
|
|
|
isSystemUser = true;
|
2021-07-26 02:47:50 +02:00
|
|
|
inherit (cfg) group;
|
|
|
|
})) runners;
|
|
|
|
|
|
|
|
users.groups = mapAttrs' (_: cfg: nameValuePair cfg.group {}) runners;
|
2021-07-26 02:28:05 +02:00
|
|
|
};
|
|
|
|
}
|