Files
NixOsConfig/modules/dev/gitea-vm-runners.nix
T
faumaray 620ae1577d vm gitea runner
Signed-off-by: faumaray <faumaray@gmail.com>
2026-10-29 21:28:40 +03:00

185 lines
7.2 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;
# The Docker Hub v3.12/latest image predates the Reims vGPU QEMU fix.
# Build a pinned upstream source revision lazily on first macOS VM start.
# Bump this revision to select a new source tree and image tag together.
macosRev = "16a5b470cdd601bae8b05b02d748d7edfb36c12e";
macosSource = builtins.fetchGit {
url = "https://github.com/dockur/macos.git";
rev = macosRev;
};
macosImage = "local/dockur-macos:${builtins.substring 0 7 macosRev}";
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
share=/run/gitea-vm-runners/windows-share
${pkgs.coreutils}/bin/mkdir -p "$state" /run/gitea-vm-runners
${pkgs.coreutils}/bin/rm -rf "$oem" "$share"
${pkgs.coreutils}/bin/mkdir -m 0700 -p "$oem"
${pkgs.coreutils}/bin/mkdir -m 0755 -p "$share"
${pkgs.coreutils}/bin/cp -R ${windowsOem}/. "$oem/"
${pkgs.coreutils}/bin/cp ${windowsOem}/install.ps1 ${windowsOem}/cxx26-feature-probe.cpp "$share/"
${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 \
-e SAMBA_READONLY=Y \
-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" \
-v "$share:/shared" \
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 /run/gitea-vm-runners/windows-share
exit 0
'';
macosStart = pkgs.writeShellScript "gitea-vm-macos-start" ''
set -euo pipefail
# Build once into Docker's persistent image store. Guest RAM is not
# allocated during idle; this image build happens only on the first start.
if ! ${pkgs.docker}/bin/docker image inspect ${macosImage} >/dev/null 2>&1; then
echo "Building pinned dockur/macos source ${macosRev} (one-time first start)..." >&2
${pkgs.docker}/bin/docker build --pull --tag ${macosImage} ${macosSource}
fi
${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=8G -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 \
${macosImage}
'';
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";
};
};
};
}