152 lines
5.9 KiB
Nix
152 lines
5.9 KiB
Nix
# On-demand Windows/macOS Gitea Actions guests. This is a flake-parts aspect;
|
|
# host.nix opts into it, and import-tree ignores the underscore-prefixed assets.
|
|
{ config, lib, ... }:
|
|
let
|
|
registrationSecret = config.meta.secretsDir + "/gitea-runner-token.age";
|
|
apiSecret = config.meta.secretsDir + "/gitea-vm-runner-api-token.age";
|
|
hasRegistrationSecret = builtins.pathExists registrationSecret;
|
|
hasApiSecret = builtins.pathExists apiSecret;
|
|
in
|
|
{
|
|
flake.modules.nixos.gitea-vm-runners = { config, lib, pkgs, ... }:
|
|
let
|
|
assets = ./_gitea-vm-runner-assets;
|
|
|
|
windowsOem = pkgs.runCommand "gitea-vm-windows-oem" { } ''
|
|
mkdir -p "$out"
|
|
cp ${assets}/windows/install.bat "$out/install.bat"
|
|
cp ${assets}/windows/install.ps1 "$out/install.ps1"
|
|
cp ${../../ci/cxx26-feature-probe.cpp} "$out/cxx26-feature-probe.cpp"
|
|
'';
|
|
|
|
macosShare = pkgs.runCommand "gitea-vm-macos-share" { } ''
|
|
mkdir -p "$out"
|
|
cp ${assets}/macos/bootstrap.sh "$out/bootstrap.sh"
|
|
cp ${../../ci/cxx26-feature-probe.cpp} "$out/cxx26-feature-probe.cpp"
|
|
'';
|
|
|
|
registrationCopy = lib.optionalString hasRegistrationSecret (
|
|
"${pkgs.coreutils}/bin/install -m 0400 ${config.age.secrets.gitea-runner-token.path} \"$oem/runner-token\""
|
|
);
|
|
|
|
windowsStart = pkgs.writeShellScript "gitea-vm-windows-start" ''
|
|
set -euo pipefail
|
|
state=/var/lib/gitea-vm-runners/windows
|
|
oem=/run/gitea-vm-runners/windows-oem
|
|
${pkgs.coreutils}/bin/mkdir -p "$state" /run/gitea-vm-runners
|
|
${pkgs.coreutils}/bin/rm -rf "$oem"
|
|
${pkgs.coreutils}/bin/mkdir -m 0700 -p "$oem"
|
|
${pkgs.coreutils}/bin/cp -R ${windowsOem}/. "$oem/"
|
|
${registrationCopy}
|
|
${pkgs.docker}/bin/docker rm -f gitea-vm-windows >/dev/null 2>&1 || true
|
|
exec ${pkgs.docker}/bin/docker run --rm --init \
|
|
--name gitea-vm-windows \
|
|
--device=/dev/kvm --device=/dev/net/tun --cap-add=NET_ADMIN \
|
|
-e VERSION=11 -e RAM_SIZE=16G -e CPU_CORES=8 -e DISK_SIZE=160G \
|
|
-p 127.0.0.1:18006:8006 \
|
|
-p 127.0.0.1:13389:3389/tcp \
|
|
-v /var/lib/gitea-vm-runners/windows:/storage \
|
|
-v "$oem:/oem:ro" \
|
|
dockurr/windows:latest
|
|
'';
|
|
|
|
windowsStop = pkgs.writeShellScript "gitea-vm-windows-stop" ''
|
|
set +e
|
|
${pkgs.docker}/bin/docker stop --time 120 gitea-vm-windows >/dev/null 2>&1
|
|
${pkgs.coreutils}/bin/rm -rf /run/gitea-vm-runners/windows-oem
|
|
exit 0
|
|
'';
|
|
|
|
macosStart = pkgs.writeShellScript "gitea-vm-macos-start" ''
|
|
set -euo pipefail
|
|
${pkgs.coreutils}/bin/mkdir -p /var/lib/gitea-vm-runners/macos
|
|
${pkgs.docker}/bin/docker rm -f gitea-vm-macos >/dev/null 2>&1 || true
|
|
exec ${pkgs.docker}/bin/docker run --rm --init \
|
|
--name gitea-vm-macos \
|
|
--device=/dev/kvm --device=/dev/net/tun --device=/dev/dri --cap-add=NET_ADMIN \
|
|
-e VERSION=15 -e RAM_SIZE=16G -e CPU_CORES=8 -e DISK_SIZE=128G -e GPU=Y \
|
|
-p 127.0.0.1:18007:8006 \
|
|
-p 127.0.0.1:15900:5900/tcp \
|
|
-p 127.0.0.1:15900:5900/udp \
|
|
-v /var/lib/gitea-vm-runners/macos:/storage \
|
|
-v ${macosShare}:/shared:ro \
|
|
dockurr/macos:latest
|
|
'';
|
|
|
|
macosStop = pkgs.writeShellScript "gitea-vm-macos-stop" ''
|
|
set +e
|
|
${pkgs.docker}/bin/docker stop --time 120 gitea-vm-macos >/dev/null 2>&1
|
|
exit 0
|
|
'';
|
|
in
|
|
{
|
|
# AMD KVM support. The host's existing docker aspect provides docker.service.
|
|
boot.kernelModules = lib.mkAfter [ "kvm-amd" "tun" ];
|
|
|
|
age.secrets = lib.optionalAttrs hasApiSecret {
|
|
gitea-vm-runner-api-token = {
|
|
file = apiSecret;
|
|
mode = "0400";
|
|
};
|
|
};
|
|
|
|
systemd.tmpfiles.rules = [
|
|
"d /var/lib/gitea-vm-runners 0750 root root - -"
|
|
"d /var/lib/gitea-vm-runners/windows 0750 root root - -"
|
|
"d /var/lib/gitea-vm-runners/macos 0750 root root - -"
|
|
"d /var/lib/gitea-vm-runners/keep-awake 0750 root root - -"
|
|
"d /run/gitea-vm-runners 0700 root root - -"
|
|
];
|
|
|
|
# Intentionally no wantedBy: the dispatcher alone starts/stops these units.
|
|
systemd.services.gitea-vm-windows = {
|
|
description = "On-demand Windows Gitea Actions VM";
|
|
after = [ "docker.service" ] ++ lib.optional hasRegistrationSecret "agenix.service";
|
|
requires = [ "docker.service" ];
|
|
serviceConfig = {
|
|
Type = "simple";
|
|
ExecStart = windowsStart;
|
|
ExecStop = windowsStop;
|
|
Restart = "no";
|
|
TimeoutStartSec = "infinity";
|
|
TimeoutStopSec = "150s";
|
|
KillMode = "mixed";
|
|
};
|
|
};
|
|
|
|
systemd.services.gitea-vm-macos = {
|
|
description = "On-demand macOS Gitea Actions VM";
|
|
after = [ "docker.service" ];
|
|
requires = [ "docker.service" ];
|
|
serviceConfig = {
|
|
Type = "simple";
|
|
ExecStart = macosStart;
|
|
ExecStop = macosStop;
|
|
Restart = "no";
|
|
TimeoutStartSec = "infinity";
|
|
TimeoutStopSec = "150s";
|
|
KillMode = "mixed";
|
|
};
|
|
};
|
|
|
|
# The API token is read-only and is distinct from the runner registration token.
|
|
# On API/network failures the controller deliberately keeps guests awake.
|
|
systemd.services.gitea-vm-runner-dispatcher = lib.mkIf hasApiSecret {
|
|
description = "Wake Gitea Windows/macOS runners for queued workflow jobs";
|
|
wantedBy = [ "multi-user.target" ];
|
|
wants = [ "network-online.target" ];
|
|
after = [ "network-online.target" "docker.service" "agenix.service" ];
|
|
requires = [ "docker.service" ];
|
|
serviceConfig = {
|
|
Type = "simple";
|
|
ExecStart = "${pkgs.python3}/bin/python ${assets}/dispatcher.py ${config.age.secrets.gitea-vm-runner-api-token.path}";
|
|
Restart = "always";
|
|
RestartSec = "10s";
|
|
User = "root";
|
|
UMask = "0077";
|
|
};
|
|
};
|
|
};
|
|
|
|
}
|