docs: how to build, restore and migrate; ci: evaluate NixOS hosts

Anyone with the repo and the secrets should be able to rebuild the
servers. nixos/README.md covers building from nothing, reinstalling in
place and the rehearsal pair; RESTORE.md covers restoring from the
restic backups; MIGRATION.md is the plan and runbook for moving
production over, with what was learned on the test servers.

CI evaluates every host on a push under nixos/. It does not deploy yet:
no production host runs NixOS.

Assisted-by: Claude:fable-5-2
This commit is contained in:
Sergei Poljanski 2026-11-16 22:59:59 +04:00
commit 933b086bb1
Signed by: asxpi
GPG key ID: 4F8851660FA4121B

206
MIGRATION.md Normal file
View file

@ -0,0 +1,206 @@
# Moving from Ansible to NixOS
Plan and runbook. Delete this file when the last step is done.
**Goal:** both servers run NixOS built from `nixos/`; services are
native NixOS services where nixpkgs has a module and Podman containers
otherwise; no Docker, no Ansible. The servers themselves stay (they are
billed below list price): each is converted in place.
## Where things stand (2026-11-16)
Both servers still run Debian and are still deployed by Ansible from
`main`. Nothing below has touched them.
On branch `nix`, each item verified on the rehearsal pair
(`nixos/README.md`):
- OpenTofu renders one cloud-init for both servers; a new or rebuilt
server becomes NixOS by itself in about three minutes. OpenTofu plans
no change to the live servers.
- The same conversion run by hand on a Debian host with Docker data
keeps everything on disk (under `/old-root`), SSH host keys included.
- Base system: users with their Debian UIDs, SSH with the same crypto
settings, fail2ban, nftables firewall, journal cap, Podman.
- Secrets in sops, per host; the production files already hold the
WireGuard keys, the Xray config, the restic password, the S3 keys and
the alert mail account.
- WireGuard: hub on ops, spoke on services, IPv4 and IPv6 through the
tunnel, NAT on the hub.
- asxpio-services: Xray, SillyTavern, Minecraft (Paper, with Geyser)
and the ACME forwarder as native services; XMPP as Podman containers
(ejabberd started with test secrets; not started on production data).
- asxpio-ops: Postgres 18.7, Garage 2.5.0 and Forgejo 17.0.1 as native
services, with roles, buckets, keys and grants set from sops.
- Backup: the same script, schedule and retention; a run with hooks and
a restore were done against local repositories.
- **Real data:** last night's backups of both hosts were restored onto
the rehearsal pair and moved into the new layout with
`nixos/migration/*-data.sh`. Forgejo served the repositories, Postgres
had the 13 tables and the read-only `bingo` role, Garage the buckets
and objects, Minecraft the world (Java and Bedrock ports), SillyTavern
its users. Restore times: ops 6.3 GiB in under a minute, services
25 GiB in three and a half.
Found by rehearsing, fixed in the config: Garage refuses a credential
file with mode 0440; Geyser needs `libcrypto` and netlink, which the
container had and the NixOS unit did not; SillyTavern ignores the
module's listen flags.
Still to port, in this order:
- asxpio-services: `s3_replicate`, the monitoring agent, both CI runners
(against the rehearsal Forgejo).
- asxpio-ops: Traefik, monitoring, `crawler_alert`, `xmpp_cert_sync`,
the app stacks on Podman with their new database and S3 addresses.
- Retire the `storage` repo once ops is converted: Postgres and Garage
are defined here now.
## What each service becomes
Versions: nixpkgs 26.05 on 2026-11-16 against what runs today.
| Today (Ansible role, container) | On NixOS | Data move |
|---|---|---|
| `base`, `ssh_hardening`, `swap` | `modules/base.nix`, host files (done) | none |
| `wireguard` | `networking.wireguard` + sops | keys from the vault; same keys, so peers do not change |
| `traefik` 3.7.19 | native `services.traefik` (3.7.16), routes as Nix instead of container labels | `acme.json` |
| `forgejo` 17.0.1 | native `services.forgejo` (17.0.1) | `/opt/forgejo/data` to `/var/lib/forgejo`, SQLite as is |
| storage: Postgres 18.7 | native `services.postgresql` (18.7) | `pg_dumpall`, load (the image is musl, the package glibc: no reuse of the data directory) |
| storage: Garage 2.5.0 | native `services.garage` with a 2.x package (the default is 1.3.1: pin it) | metadata and data directories as is |
| `monitoring`: Prometheus, Grafana, Loki, blackbox | native modules | Grafana DB, Prometheus TSDB optional; Loki chunks are in S3 |
| `monitoring_agent`: Alloy, node-exporter, cAdvisor | native `services.alloy`, `services.prometheus.exporters.node`; cAdvisor against Podman | none |
| `restic_backup` | `services.restic.backups`, same repositories, hooks and schedule | none (repositories continue) |
| `s3_replicate`, `crawler_alert`, `xmpp_cert_sync` | systemd units from Nix | none |
| `forgejo_runner`, `forgejo_runner_interop` 13.2.1 | native `services.gitea-actions-runner` (forgejo-runner 13.2.1), jobs in Podman | re-register or copy `.runner` |
| `xray` 26.11.9 | native `services.xray` | config from the vault |
| `sillytavern` 1.19.0 | native `services.sillytavern` | data directory |
| `minecraft` (Paper on temurin 21) | native `services.minecraft-server` with `papermc` | world directory; check the Paper build matches the world |
| `xmpp` (anotherim-server, apushd, tackygram) | Podman containers: the server is a custom build, not nixpkgs' ejabberd | volumes |
| `acme_forward` (nginx) | native `services.nginx`, or dropped if XMPP certs move to `security.acme` | none |
| App stacks: asxpio, datacenterbingo, blog, czsk, vhpbot | Podman, started by their own CI as today, through the Docker-compatible socket; a flake per app is a later project in each app's repo | none (`/opt/<app>`) |
Traefik has a native module, so it stays. Its Docker provider reads
labels from Podman's socket as well; routes for native services go in
the file provider, written from Nix. Caddy (`services.caddy`) would be
the simpler native choice, but changing proxy and operating system in
one move doubles what can go wrong: reconsider after step 6.
## Steps
1. **Secrets** (done for what is ported). sops-nix:
`nixos/secrets/<host>.yaml`, encrypted for an admin age key and each
host's SSH host key. Values move over from the Ansible vault as their
services move. The vault password leaves CI at the end.
2. **Network and access first** (done): WireGuard, firewall, SSH. An
admin reaches a converted host the usual way.
3. **Port asxpio-services** (no chicken-and-egg: Forgejo and CI live or
depend on ops), service by service from the table, each one tried on
the rehearsal pair. Before the conversion, restore last night's
backup of the host onto the rehearsal server and check each service
comes up on real data (versions differ slightly from the containers:
table above).
4. **Convert asxpio-services** (runbook below). Add the deploy job to
`.forgejo/workflows/nixos.yaml`: on `main`, `nixos-rebuild switch
--target-host` as user `deploy`. Remove the services roles from
`ansible/site.yaml`.
5. **Port asxpio-ops** the same way. Order inside the host: Postgres and
Garage, Forgejo, Traefik, monitoring, then the app stacks on Podman.
Rehearse the app deploy workflows against Podman before the day.
6. **Convert asxpio-ops** (runbook below), from a workstation: its own
CI cannot deploy the host it runs through.
7. **Clean up.** Delete `ansible/`, `deploy.yaml`, the vault secret in
CI, `/old-root` on both hosts, the Postgres 17 volume, this file.
Then make the branch the default:
```sh
git push origin main:retired/ansible # keep the old line
# Forgejo: Settings -> Branches -> default branch: nix; unprotect main
git push origin --delete main
git push origin nix:main
# Forgejo: default branch: main; then
git push origin --delete nix
git branch -m main retired/ansible && git branch -m nix main
```
Do this last: until then `main` is what deploys the hosts.
Each step ends with both hosts working, so the work can stop between
any two.
## Converting a host in place
Tested end to end on 2026-11-16 (cx23, Debian 13, Docker 29.9.3): the
conversion took three and a half minutes, containers and volumes were
intact afterwards. Expect 15 to 30 minutes of downtime per host,
depending on how much data changes format (table above).
Before:
1. Announce the window; no CI job running.
2. `restic-backup run` on the host and check both repositories.
3. Take a Hetzner snapshot of the server. **This is the rollback:**
rebuilding from it returns the Debian system as it was, on the same
server.
4. Stop the services so nothing writes during the move
(`docker stop -t 120 minecraft` first on services), then stop Docker.
Take the dumps the new services load: `pg_dumpall` on ops.
Convert (as root on the host; the same commands cloud-init runs):
5. Write `/etc/nixos/bootstrap.nix` with the content from
`terraform/cloud-init/nixos.yaml.tftpl` (`tofu output user_data`
shows it with the values filled in).
6. Run the `runcmd` line from the same file. The host reboots into a
bare NixOS. Reach it as root: ops on its public address, services
through ops over the private network.
7. `nixos-rebuild switch --flake .#<host>` (see `nixos/README.md`).
Services that own data stay stopped: the marker file is missing.
8. Move the data and start everything, then reboot once:
```sh
ssh root@<host> 'bash -s /old-root' < nixos/migration/<ops|services>-data.sh
```
On ops the script loads the Postgres dump from
`/old-root/var/backups/restic-stage`: put `pg_dumpall.sql` and a
`sqlite3 .backup` of Forgejo's database (`forgejo-gitea.db`) there in
step 4. What the script does not move stays in `/old-root`: `/home`,
`/root/backups`, the app stacks' `/opt/<app>`.
9. Verify: WireGuard up, every service of the host answers,
`systemctl --failed` empty, one CI run, one backup run.
After a week without surprises: `rm -rf /old-root`, delete the snapshot.
Rollback, at any point after step 6: `hcloud server disable-protection
<host> rebuild`, rebuild from the snapshot, `tofu apply`.
## 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/`:
XMPP and the Telegram gateway, Forgejo's mail, mirrors and webhooks,
Minecraft's Discord plugin. On 2026-11-16 that plugin was missed on
the first start: the bot logged in from the test server for about 15
seconds and cleared its slash commands on shutdown. Check a service's
plugins and integrations before its first start on real data.
- A Hetzner firewall limits SSH to the admin; the backup credentials are
on the servers only in `/run` and only during a restore.
- Delete the servers when testing ends: they hold production data.
## Risks
- **Lock-out.** A converted host without WireGuard is reachable only as
described in step 6. Step 2 exists to remove that risk; the Hetzner
console is the last resort.
- **ops is small.** 18 GB free today; the Nix store takes 3 to 5 GB and
`/old-root` another 2 GB plus the old container images. Prune images
before, delete `/old-root/var/lib/docker` early.
- **App deploys on Podman.** The app workflows call `docker compose`;
compose against Podman's socket mostly works, healthcheck waits and
network names are where it differs. Rehearse (step 5).
- **The CI runner on Podman** starts job containers through the socket;
the earlier attempt (branch `nixos-services`) had this working piece
by piece but never under a full pipeline.
- **First-boot method.** nixos-infect depends on the scripted initrd,
which upstream is retiring (`nixos/README.md`, last section).