{ config, pkgs, ... }: let # Local bare repo Forgejo already updated before the merge event runs. # Reading it avoids a credential and a round trip through nginx. owner = "mz"; repo = "hetzner-cloud-1"; repositoryRoot = config.services.forgejo.repositoryRoot; bareRepo = "${repositoryRoot}/${owner}/${repo}.git"; triggerDir = "/run/deploy-flake"; triggerFile = "${triggerDir}/request"; # Runs as root, detached from the Actions job. A flake bump restarts the # runner and Docker; if this were the job, that restart would kill the # SSH session and deploy-rs magic rollback would undo the switch. deployFlakeUpdate = pkgs.writeShellApplication { name = "deploy-flake-update"; runtimeInputs = with pkgs; [ coreutils git gnugrep nix nixos-rebuild ]; text = '' is_flake_bump() { local rev="$1" local author subject body author=$(git -C "$work" log -1 --format='%an' "$rev") subject=$(git -C "$work" log -1 --format='%s' "$rev") body=$(git -C "$work" log -1 --format='%b' "$rev") if [[ $author == 'forgejo-actions[bot]' ]]; then return 0 fi if [[ $subject == *'update flake.lock'* || $subject == *'chore/update-flake-lock'* ]]; then return 0 fi if [[ $body == *'Deploy-On-Merge: flake-lock'* ]]; then return 0 fi return 1 } second_parent_is_flake_bump() { local rev="$1" local parent2 parent2=$(git -C "$work" rev-parse --verify --quiet "$rev^2") || return 1 is_flake_bump "$parent2" } request=${triggerFile} pending="''${STATE_DIRECTORY}/pending" work="''${STATE_DIRECTORY}/src" deployed="''${STATE_DIRECTORY}/deployed-rev" repo_src=${bareRepo} export GIT_CONFIG_COUNT=1 export GIT_CONFIG_KEY_0=safe.directory export GIT_CONFIG_VALUE_0='*' if [[ -e $request ]]; then if [[ -L $request ]]; then echo "refusing symlink at $request" >&2 rm -f "$request" exit 1 fi mv "$request" "$pending" elif [[ ! -f $pending ]]; then echo "no deploy request" >&2 exit 0 fi sha=$(tr -d '[:space:]' < "$pending") if [[ ! $sha =~ ^[0-9a-f]{40}$ && ! $sha =~ ^[0-9a-f]{64}$ ]]; then echo "refusing malformed sha" >&2 rm -f "$pending" exit 1 fi if [[ ! -d $repo_src ]]; then echo "bare repo not found: $repo_src" >&2 exit 1 fi mkdir -p "$work" if [[ ! -d $work/.git ]]; then git clone "$repo_src" "$work" fi git -C "$work" fetch --prune "$repo_src" +refs/heads/main:refs/remotes/origin/main main=$(git -C "$work" rev-parse refs/remotes/origin/main) if [[ $main != "$sha" ]]; then echo "requested $sha is not origin/main ($main); not deploying" >&2 rm -f "$pending" exit 1 fi files=$(git -C "$work" diff --name-only "$sha^" "$sha") if [[ $files != flake.lock ]]; then echo "refusing deploy; origin/main does not change only flake.lock:" >&2 printf '%s\n' "$files" >&2 rm -f "$pending" exit 1 fi if ! is_flake_bump "$sha" && ! second_parent_is_flake_bump "$sha"; then echo "refusing deploy; $sha is not a flake.lock update" >&2 rm -f "$pending" exit 1 fi if [[ -f $deployed ]]; then previous=$(tr -d '[:space:]' < "$deployed") if [[ $previous == "$sha" ]]; then echo "already deployed $sha" rm -f "$pending" exit 0 fi fi git -C "$work" checkout --detach --force "$sha" nixos-rebuild switch --flake "$work#hetzner-cloud-1" printf '%s\n' "$sha" > "$deployed" rm -f "$pending" echo "deployed $sha" ''; }; in { # Any workflow on this runner may mount this directory. It holds no secret. # The service above ignores the file unless origin/main is that commit, the # diff is only flake.lock, and the commit is one of the bot's bumps. systemd.tmpfiles.rules = [ "d ${triggerDir} 0755 root root - -" ]; systemd.paths.deploy-flake-update = { description = "Queue a deploy when a flake.lock update is merged"; wantedBy = [ "multi-user.target" ]; wants = [ "systemd-tmpfiles-setup.service" ]; after = [ "systemd-tmpfiles-setup.service" ]; pathConfig = { PathExists = triggerFile; PathModified = triggerFile; }; }; systemd.services.deploy-flake-update = { description = "Deploy a merged flake.lock update"; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; # The switch this unit starts rewrites this unit's store path. Leave the # running deploy alone; the new definition applies on the next trigger. restartIfChanged = false; stopIfChanged = false; serviceConfig = { Type = "oneshot"; StateDirectory = "deploy-flake"; TimeoutStartSec = "2h"; ExecStart = "${deployFlakeUpdate}/bin/deploy-flake-update"; }; }; }