Record the conversion of asxpio-services (34 minutes of downtime) and the lessons now in the config or the runbook: the admin's uplink runs through Xray on services, build on the host rather than from the workstation, Podman DNS on every network, IPv6 for published ports. Phase 2 gains two steps: switch Ansible's deploy off before ops converts, and drop the interim certificate-push key afterwards. Assisted-by: Claude:opus-5-5
17 KiB
Moving production from Ansible and Terraform to NixOS and OpenTofu
The plan and the runbook. Delete this file when the last step is done.
End state: both servers run NixOS built from nixos/, deployed by
.forgejo/workflows/nixos.yaml; services are native NixOS services
where nixpkgs has a module and Podman containers otherwise; no Docker
daemon, no Ansible; infrastructure in terraform/ is applied with
OpenTofu. The servers themselves stay: they are billed below today's
list price, so each one is converted in place and never recreated.
Nothing in production changes without the owner's go-ahead for that step. Every step below that touches a server says so.
Where things stand (2026-11-17)
asxpio-services runs NixOS since 2026-11-17 (Phase 1 below, done) and
is deployed by .forgejo/workflows/nixos.yaml from main. asxpio-ops
still runs Debian, deployed by Ansible from main; Ansible no longer
manages services (inventory group nixos). Snapshot pre-nixos-2026-11-17
of services is kept until /old-root is deleted.
Phase 1 took 34 minutes of downtime (02:29–03:03 UTC, plus a few seconds for XMPP at 03:31), most of it the first build. What it taught, and is now in the config or the runbook:
- Xray on services carries the admin's uplink (all traffic, WireGuard
included, because of DPI). It was moved to a temporary server
(
claude-test-xray, same config from sops) and the admin's client switched before anything stopped. Phase 2 does not stop it. - Build on the server, not from the workstation.
nixos-rebuild --build-hostsent every derivation through the admin's slow uplink; copying the repo (git archive HEAD | ssh … tar -x) and runningnixos-rebuild switchon the host took four minutes. - Podman DNS was open on podman0 only: CI jobs (own networks) could
not resolve names. Fixed in
modules/podman.nix. - IPv6 for published ports: Podman's default network forwards IPv4 only, Docker on Debian did both; XMPP was unreachable over IPv6 for half an hour. XMPP has its own IPv6 network now; anything else that publishes ports and has an AAAA record needs the same.
- The rehearsal ran nothing that downloads inside a CI job, and never tested IPv6 from outside. Both are now part of the checks below.
Everything else ran first on the rehearsal pair (two cx23 in the personal project), most of it on a restored copy of production data:
| Area | Rehearsed | Notes |
|---|---|---|
| First boot (cloud-init + nixos-infect) | yes | 3–4 min to a bare NixOS |
| In-place conversion with Docker data | yes | data kept under /old-root |
| Base, SSH, firewall, WireGuard, sops | yes | same host keys, same peers |
| Postgres 18.7, Garage 2.5, Forgejo 17 | yes, prod data | roles, buckets, keys from sops |
| Traefik, label routing via Podman | yes | certificates: staging CA only |
| asxpio, datacenterbingo on Podman | yes, prod data | mail and IndexNow disabled |
| Minecraft, SillyTavern, Xray, ACME forward | yes, prod data | Discord plugin kept off |
| XMPP (ejabberd, apushd, tackygram) | dummy data only | never started on prod data on purpose |
| CI runner + jobs | yes | against the rehearsal Forgejo |
| Backup, restore, bucket replica | yes | replica: config only, no run |
| Monitoring, logs, alerts, security baseline | yes | 28 targets up, 0 unknown items |
| CI deploy job | evaluate only | deploys nothing until a host is NixOS |
What each service became, and why:
| Was (Ansible role, container) | Now |
|---|---|
base, ssh_hardening, swap |
modules/base.nix, hetzner.nix |
wireguard |
modules/wireguard.nix, keys in sops |
traefik |
native services.traefik; native services' routes in Nix, apps keep labels |
forgejo |
native services.forgejo as user git; no SSH passthrough needed |
| storage repo: Postgres, Garage | native services.postgresql, services.garage (2.x pinned) |
monitoring, monitoring_agent |
native Prometheus, Loki, Grafana, Alloy, exporters, cAdvisor (Podman) |
restic_backup, s3_replicate, crawler_alert, xmpp_cert_sync |
systemd units from Nix, same scripts and schedules |
forgejo_runner, forgejo_runner_interop |
native runner module; interop as its own unit |
sillytavern, minecraft, xray, acme_forward |
native modules |
xmpp |
Podman containers (a custom build, not nixpkgs' ejabberd) |
| App stacks (asxpio, datacenterbingo, blog, czsk, vhpbot) | unchanged compose stacks on Podman, deployed by their own CI |
Phase 0: preparation (no downtime)
Everything here can be done while Ansible still runs production.
-
Push
nixand let CI evaluate it. It does not deploy: the job skips any host without/etc/NIXOS, and branches never deploy. -
OpenTofu. Once, from a workstation:
cd terraform tofu init -reconfigure tofu plan # expected: only the new user_data output tofu apply # writes the output; changes no resourceThe state is the one Terraform wrote; the servers ignore
user_data,imageandssh_keys, so nothing can be replaced. -
App repositories: healthcheck in the compose file. Podman ignores the
HEALTHCHECKof OCI-format images, which is what the app CI builds, so the deploy's wait for "healthy" would time out on NixOS. Copy each image's healthcheck into its compose file (asxpio, datacenterbingo, blog; czsk has none). Under Docker the result is the same, so this ships now, through the normal deploy. The exact block for asxpio:healthcheck: test: ["CMD-SHELL","ruby -rnet/http -e 'port = ENV.fetch(\"PUMA_PORT\", \"3000\"); exit(Net::HTTP.get_response(URI(\"http://127.0.0.1:#{port}/healthz\")).code == \"200\" ? 0 : 1)'"] interval: 30s timeout: 5s start_period: 20s retries: 3Read the others with
skopeo inspect --raw --config docker://<image>. -
Storage repo: stop its deploys. Its CI starts Postgres and Garage containers in
/opt/storage; after the ops conversion those would fight the native services for their ports. Disable the deploy workflow (or archive the repo) before Phase 2. Its secrets (ASXPIO_*,BINGO_*,PUBLIC_*) stay: the app repos use the same values. -
Secrets.
nixos/secrets/asxpio-*.yamlwere filled on 2026-11-16 and 17 from the Ansible vault,/opt/storage/.envand the Garage RPC secret. If any of those changed since, update the sops file (sops nixos/secrets/asxpio-ops.yaml). Keys: WireGuard, Xray config, restic password, S3, alert SMTP, Forgejo (secret key, internal token, JWT secrets, SMTP, metrics token), Postgres roles, Garage keys and RPC secret, Traefik ACME email, runner tokens, Grafana admin password, Hetzner read-only token. The vault and sops must agree until Ansible is gone. -
Freeze. On conversion day: no pushes to the app repos, to
iac, or Renovate merges, from the start of a phase until its checks pass. -
Rehearse once more against the latest backups (both hosts), on a fresh rehearsal pair, following
nixos/README.md→ Rehearsal and the rules at the end of this file. It costs about €0.02 an hour and is the only way to know that the night's data still fits the config.
Phase 1 is done (2026-11-17); the steps stay as the record and for a reinstall. Go/no-go for Phase 1: steps 1–7 done, last night's restic backup of both hosts green, a Hetzner snapshot of the host just taken.
Phase 1: asxpio-services (owner's go-ahead required)
Downtime about 15–20 minutes for Minecraft, SillyTavern, XMPP, Xray and CI. asxpio-ops and everything on it stays up. Services first: it has less data and nothing on ops depends on it.
From a workstation on WireGuard, in the repo:
-
Snapshot (the rollback):
hcloud server create-image --type snapshot --description pre-nixos asxpio-services -
Stop what writes, then Docker:
ssh root@10.20.0.20 'docker stop -t 120 minecraft; cd /opt/xmpp && docker compose stop; \ docker stop forgejo-runner forgejo-runner-interop sillytavern xray acme-forward; \ systemctl stop docker.socket docker containerd' -
Convert (the same lines cloud-init runs;
tofu output -json user_data | jq -r '."asxpio-services"'shows them filled in). Write/etc/nixos/bootstrap.nixwith the content from there, then:ssh root@10.20.0.20 'umount -l /tmp; curl -fsSL -o /root/nixos-infect \ https://raw.githubusercontent.com/elitak/nixos-infect/40f62a680bb0e8f2f607d79abfaaecd99d59401c/nixos-infect \ && echo "4354bd68773b41da65c0e815202c43c8549713b3ed3ff6381c71fbc0b0a840ab /root/nixos-infect" | sha256sum -c \ && PROVIDER=hetznercloud NIX_CHANNEL=nixos-26.05 NIXOS_IMPORT=./bootstrap.nix \ setsid bash /root/nixos-infect > /var/log/nixos-infect.log 2>&1 < /dev/null &'It reboots into a bare NixOS after about four minutes. WireGuard is down until step 4; reach it through ops over the private network:
ssh -J root@10.20.0.10 root@10.10.0.20. -
Switch to the flake (through ops, as above):
cd nixos NIX_SSHOPTS="-J root@10.20.0.10" nixos-rebuild switch --flake .#asxpio-services \ --target-host root@10.10.0.20 --build-host root@10.10.0.20Services that own data stay stopped: the marker file is missing.
-
Move the data and start everything:
ssh root@10.20.0.20 'bash -s /old-root' < migration/services-data.sh ssh root@10.20.0.20 reboot -
Check:
wg showon ops: the services peer has a fresh handshake.systemctl --failedempty; Grafana's security panel shows nothing unknown for asxpio-services.- Minecraft log reaches
Done; join once (Java and Bedrock). - SillyTavern on
http://10.20.0.20:8000. - An XMPP client logs in; a message to a contact goes through.
- Forgejo → Site administration → Runners: the new runner idle (it registered itself again); delete the old, offline entry. Push an empty commit to an app repo and watch a full CI run.
systemctl start restic-backupsucceeds (it continues the same repositories).
Rollback: until step 3, start Docker and the containers again.
After it: hcloud server disable-protection asxpio-services rebuild,
rebuild from the snapshot, tofu apply. Debian comes back as it was at
the snapshot.
After a week without surprises: rm -rf /old-root, delete the snapshot.
Phase 2: asxpio-ops (owner's go-ahead required)
Before it, in addition to Phase 0: disable .forgejo/workflows/deploy.yaml
(Ansible) on main, or a later push would apply the Debian roles to the
converted ops. Build on the host (Phase 1 lessons), and check IPv6 from
outside for every site.
Downtime about 30–45 minutes for every site, Forgejo, the registry and monitoring: the conversion itself, the first build of the system, then pulling each app image again (Docker's image store does not carry over). Announce it.
ops is the WireGuard hub, so during the window the admin network is down. Until step 4, ops is reachable as root on its public address (nixos-infect's bootstrap system has no address restriction); from step 4 on, over WireGuard again, or from services over the private network.
-
Snapshot, as in Phase 1.
-
Dumps and stop, the staged copies the data script loads:
ssh root@10.20.0.10 'set -e; d=/var/backups/restic-stage; install -d -m 700 $d; \ docker exec storage-postgres pg_dumpall -U postgres > $d/pg_dumpall.sql; \ sqlite3 /opt/forgejo/data/gitea/gitea.db ".backup $d/forgejo-gitea.db"; \ docker exec storage-garage /garage meta snapshot --all; \ docker ps -q | xargs -r docker stop -t 60; systemctl stop docker.socket docker containerd' -
Convert, as in Phase 1 step 3, on 135.181.27.77.
-
Switch to the flake on its public address:
nixos-rebuild switch --flake .#asxpio-ops --target-host root@135.181.27.77 \ --build-host root@135.181.27.77From here on root logs in from WireGuard and the private network only; WireGuard is up again with the same keys, so clients reconnect.
-
Move the data, start everything, pull the app images:
ssh root@10.20.0.10 'bash -s /old-root' < migration/ops-data.sh ssh root@10.20.0.10 rebootThe script moves Forgejo, Garage, Traefik's certificates, the monitoring history and
/opt/<app>; loads the Postgres dump (roles then get their passwords from sops); regenerates Forgejo's hooks and SSH keys file; and runsdocker compose pull && up -dfor each app asdeploy(vhpbot-prod builds locally). -
Check:
- Every site answers (Grafana → Probes). Certificates are the old ones
from
acme.json; Traefik renews as before. git cloneover SSH and HTTPS; a push; the package registry serves a pull.- asxpio: log in to the admin, render an invoice (the boot test that catches what a database check does not).
- datacenterbingo shows photos (Garage through the app) and
/data.json. garage status;curl -I https://s3.asxp.io/public/<a known key>.- Grafana: targets up, logs from both hosts, security panel clean.
- CI: push an empty commit to an app repo; the deploy reaches "healthy" (Phase 0 step 3).
systemctl start restic-backup xmpp-cert-syncsucceed.- Remove the Debian ops key from
xmppCertSyncKeysExtra(hosts/asxpio-services/xmpp-cert-install.nix).
- Every site answers (Grafana → Probes). Certificates are the old ones
from
Rollback: as in Phase 1. If services has already moved, it keeps working against a Debian ops: nothing on services depends on how ops is built.
Phase 3: clean-up
-
Merge
nixintomain:deploy.yaml(Ansible) then finds no change to apply,nixos.yamldeploys both hosts from now on. -
Delete
ansible/,.forgejo/workflows/deploy.yaml, theANSIBLE_VAULT_PASSWORDsecret,ansible/known_hosts(the root copyknown_hostsstays), the Ansible parts ofrenovate.json. -
Retire the storage repo (archive). Optional, later: drop the
storagenetwork from the app compose files and point theirDATABASE_URL/S3_ENDPOINTat the host, then removeapps.nix's compatibility entries. -
On both hosts after a week:
rm -rf /old-root; delete the snapshots; on ops remove the old Postgres 17 volume data, which went with/old-root. -
Delete the rehearsal servers, network and firewall (
managed-by=claudein the personal project), andclaude-test-xrayonce the admin's client points at the production Xray again. -
Rotate what sat in the old history and the vault for a year (
INTERNAL_TOKEN, the JWT secrets, runner tokens), now in sops; and give Grafana its ownsecret_key(production keeps the default for now because its database was written with it; Grafana documents the rotation). -
Update the workspace
CLAUDE.md(servers, deploys, gotchas). -
Branches, last, because until then
mainis what deploys:git push origin main:retired/ansible # keep the Ansible line # Forgejo → Settings → Branches: default branch nix; unprotect main git push origin --delete main git push origin nix:main # Forgejo: default branch main again; then git push origin --delete nix git branch -m main retired/ansible && git branch -m nix main
Rules for the rehearsal pair
Restored data carries production credentials inside it. Anything that
would use them outward is kept from starting in hosts/rehearsal/, and
the following must hold every time:
- Before a runner meets restored Forgejo data, run
migration/rehearsal-sanitize-forgejo.shon the rehearsal ops. A restored database still has production's queued jobs, schedules and Actions secrets; on 2026-11-16 the test runner picked up a queued production job and was stopped only by the different secret key. - XMPP and the Telegram gateway, Forgejo's mail, mirrors and webhooks, Grafana mail, the crawler mail, the bucket replica and the interop runner do not start there. The Minecraft plugins that talk outward (DiscordSRV, ServerBackup, AutoUpdateGeyser) are set aside before every start: on 2026-11-16 DiscordSRV was missed on the first start and logged the production bot in from the test server for 15 seconds.
- Apps run with mail pointed at nothing and
INDEXNOW_KEYempty; the asxpio monitor worker (external probes) stays off. - SSH is limited by a Hetzner firewall to the admin; backup credentials
are on the servers only in
/run, only during a restore. - Delete the servers when testing ends: they hold production data.
Risks
| Risk | Mitigation |
|---|---|
| Lock-out after the switch | Phase steps say how each host is reached at each point; services through ops and the other way round; Hetzner console last |
| nixos-infect breaks with a later NixOS | pinned by commit and checksum; nix_channel pinned to 26.05; nixos-anywhere as the fallback (nixos/README.md) |
| ops disk (38 GB) | 18 GB free today; the Nix store adds 3–5 GB, /old-root holds the old images until deleted; delete /old-root/var/lib/docker once the apps run |
| Data of newer versions than 26.05 ships | Prometheus, Grafana, Xray, SillyTavern from nixos-unstable; Garage 2.x pinned; checked on the rehearsal pair with prod data |
| Something only production does | XMPP on real data and Let's Encrypt issuance were not rehearsed; both have a snapshot rollback and are checked first in their phase |
| A deploy during the window | freeze (Phase 0 step 6); the storage repo's deploy is off (step 4) |