2023-05-17 21:56:46 +00:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
self,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
2022-10-22 12:57:40 +00:00
|
|
|
bootstrap = pkgs.writeScript "bootstrap.sh" ''
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
apt update
|
|
|
|
apt install --yes curl git sudo xz-utils
|
|
|
|
|
|
|
|
adduser --system --uid 999 build
|
|
|
|
chown build /nix
|
|
|
|
|
|
|
|
sudo -u build curl -L https://nixos.org/nix/install > install
|
|
|
|
sudo -u build sh install
|
|
|
|
|
|
|
|
echo "export PATH=/nix/var/nix/profiles/per-user/build/profile/bin:''$PATH" >> /etc/profile
|
|
|
|
|
|
|
|
mkdir /etc/nix
|
|
|
|
echo 'experimental-features = nix-command flakes' >> /etc/nix/nix.conf
|
|
|
|
|
|
|
|
export nix_user_config_file="/home/build/.local/share/nix/trusted-settings.json"
|
|
|
|
mkdir -p $(dirname \\$nix_user_config_file)
|
2023-03-28 12:03:41 +00:00
|
|
|
echo '{"extra-experimental-features":{"nix-command flakes":true}}' > \\$nix_user_config_file
|
2022-10-22 12:57:40 +00:00
|
|
|
chown -R build /home/build/
|
|
|
|
|
|
|
|
curl -L https://github.com/drone-runners/drone-runner-exec/releases/latest/download/drone_runner_exec_linux_amd64.tar.gz | tar xz
|
|
|
|
sudo install -t /usr/local/bin drone-runner-exec
|
|
|
|
|
|
|
|
if [ ! -f /run/vars ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cp -a /run/vars /run/runtime-vars
|
|
|
|
env | grep "DRONE" >> /run/runtime-vars
|
|
|
|
|
|
|
|
su - -s /bin/bash build sh -c "/usr/local/bin/drone-runner-exec daemon /run/runtime-vars"
|
|
|
|
'';
|
|
|
|
psCfg = config.pub-solar;
|
|
|
|
cfg = config.pub-solar.docker-ci-runner;
|
2023-05-17 21:56:46 +00:00
|
|
|
in {
|
2022-10-22 12:57:40 +00:00
|
|
|
options.pub-solar.docker-ci-runner = {
|
2022-10-26 20:04:48 +00:00
|
|
|
enable = lib.mkEnableOption "Enables a docker container running a drone exec runner as unprivileged user.";
|
2022-10-22 12:57:40 +00:00
|
|
|
|
|
|
|
enableKvm = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Enable kvm support.
|
|
|
|
'';
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
nixCacheLocation = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Location of nix cache that is shared between builds
|
|
|
|
'';
|
2022-10-30 20:37:24 +00:00
|
|
|
default = "/var/lib/docker-ci-runner";
|
2022-10-22 12:57:40 +00:00
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
|
|
|
|
runnerEnvironment = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Additional environment vars added to the vars file on container runtime
|
|
|
|
'';
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
runnerVarsFile = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Location of vars file passed to drone runner
|
|
|
|
'';
|
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
virtualisation = {
|
|
|
|
docker = {
|
|
|
|
enable = true; # sadly podman is not supported rightnow
|
|
|
|
};
|
|
|
|
|
|
|
|
oci-containers = {
|
|
|
|
backend = "docker";
|
|
|
|
containers."drone-exec-runner" = {
|
|
|
|
image = "debian";
|
|
|
|
autoStart = true;
|
|
|
|
entrypoint = "bash";
|
2023-05-17 21:56:46 +00:00
|
|
|
cmd = ["/bootstrap.sh"];
|
2022-10-26 20:04:48 +00:00
|
|
|
|
2022-10-22 12:57:40 +00:00
|
|
|
volumes = [
|
|
|
|
"${cfg.runnerVarsFile}:/run/vars"
|
|
|
|
"${cfg.nixCacheLocation}:/nix"
|
|
|
|
"${bootstrap}:/bootstrap.sh"
|
|
|
|
];
|
|
|
|
|
|
|
|
environment = cfg.runnerEnvironment;
|
2022-10-26 20:04:48 +00:00
|
|
|
|
2023-05-17 21:56:46 +00:00
|
|
|
extraOptions = lib.mkIf cfg.enableKvm ["--device=/dev/kvm"];
|
2022-10-22 12:57:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-04-15 19:13:45 +00:00
|
|
|
# Fix container not stopping correctly and holding the system 120s upon
|
|
|
|
# shutdown / reboot
|
|
|
|
systemd.services.docker-drone-exec-runner.preStop = ''
|
|
|
|
docker stop drone-exec-runner
|
|
|
|
'';
|
2022-10-22 12:57:40 +00:00
|
|
|
};
|
|
|
|
}
|