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:
parent
be68c5adef
commit
933b086bb1
5 changed files with 566 additions and 3 deletions
35
.forgejo/workflows/nixos.yaml
Normal file
35
.forgejo/workflows/nixos.yaml
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
name: nixos
|
||||
|
||||
# Every push: evaluate each host, so a broken config never reaches main.
|
||||
# Deploying from here is added when the first host runs NixOS
|
||||
# (MIGRATION.md, step 4); until then hosts are switched by hand.
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'nixos/**'
|
||||
- '.forgejo/workflows/nixos.yaml'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
evaluate:
|
||||
runs-on: nix-latest
|
||||
env:
|
||||
NIX_CONFIG: |
|
||||
experimental-features = nix-command flakes
|
||||
build-users-group =
|
||||
sandbox = false
|
||||
steps:
|
||||
- name: Prepare container for actions
|
||||
run: nix-env -iA nixpkgs.nodejs_22
|
||||
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v7.0.1
|
||||
|
||||
- name: Evaluate
|
||||
working-directory: nixos
|
||||
run: |
|
||||
for host in asxpio-ops asxpio-services rehearsal; do
|
||||
nix eval --raw ".#nixosConfigurations.$host.config.system.build.toplevel.drvPath"
|
||||
echo
|
||||
done
|
||||
206
MIGRATION.md
Normal file
206
MIGRATION.md
Normal 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).
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
# asxp.io infrastructure
|
||||
|
||||
Two Hetzner Cloud servers, described by Terraform and configured by
|
||||
Ansible. Pushing to `main` deploys: a Forgejo Actions workflow applies
|
||||
`ansible/site.yaml` to both servers.
|
||||
Two Hetzner Cloud servers, described by OpenTofu. They are moving from
|
||||
Debian configured by Ansible to NixOS (`MIGRATION.md`); until a host has
|
||||
moved, pushing to `main` still applies `ansible/site.yaml` to it.
|
||||
|
||||
- Build the servers from nothing: `nixos/README.md`
|
||||
- Restore data from backups: `RESTORE.md`
|
||||
|
||||
## Servers
|
||||
|
||||
|
|
|
|||
127
RESTORE.md
Normal file
127
RESTORE.md
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
# Restoring from backups
|
||||
|
||||
Each host backs itself up nightly with restic (ops 03:30, services 04:15
|
||||
UTC) into two independent repositories:
|
||||
|
||||
| Name | Repository |
|
||||
|------|------------|
|
||||
| `hel1` | `s3:https://hel1.your-objectstorage.com/asxpio-backups-main/restic/<host>` |
|
||||
| `fsn1` | `s3:https://fsn1.your-objectstorage.com/asxpio-backups-repl/restic/<host>` |
|
||||
|
||||
A snapshot holds `/etc`, `/opt`, the listed container volumes and
|
||||
`/var/backups/restic-stage`, where hooks put consistent copies of the
|
||||
databases just before the run:
|
||||
|
||||
| Host | In `/var/backups/restic-stage` | Volumes |
|
||||
|------|--------------------------------|---------|
|
||||
| asxpio-ops | `pg_dumpall.sql`, `asxpio.dump` (`pg_dump -Fc`), `forgejo-gitea.db` | `storage_garage_meta`, `storage_garage_data` (with a fresh Garage metadata snapshot) |
|
||||
| asxpio-services | `ejabberd.sqlite`; ejabberd's Mnesia as `nightly.backup` inside `xmpp_database` | `xmpp_database`, `xmpp_tackygram` |
|
||||
|
||||
Not in the backup: Postgres' data directory (restore from the dump),
|
||||
`/opt/containerd`, and what Minecraft downloads again (`cache`,
|
||||
`libraries`, `versions`). The exact lists are `restic_paths`,
|
||||
`restic_excludes` and `restic_pre_hooks` in
|
||||
`ansible/inventory/host_vars/`.
|
||||
|
||||
Three secrets are needed, and none of them may exist only on the
|
||||
servers: the Object Storage access key and secret key, and the restic
|
||||
password (`vault_restic_password` in the Ansible vault). **Without the
|
||||
password nothing can be read.**
|
||||
|
||||
## One file or one service, on a running host
|
||||
|
||||
```sh
|
||||
restic-backup hel1 snapshots
|
||||
restic-backup hel1 ls latest /opt/xmpp
|
||||
restic-backup fsn1 restore latest --target /tmp/r --include /opt/xmpp
|
||||
```
|
||||
|
||||
Restore beside the live data, compare, then stop the service and move
|
||||
the files into place.
|
||||
|
||||
## A whole host, onto a fresh system
|
||||
|
||||
On a host built by `nixos/README.md` (or any Linux with restic):
|
||||
|
||||
```sh
|
||||
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=...
|
||||
export RESTIC_PASSWORD=...
|
||||
export RESTIC_REPOSITORY=s3:https://hel1.your-objectstorage.com/asxpio-backups-main/restic/asxpio-ops
|
||||
restic snapshots --host asxpio-ops # pick one; "latest" below
|
||||
```
|
||||
|
||||
Keep every service stopped until its data is back, so nothing starts on
|
||||
an empty directory and creates fresh state where the restore will land.
|
||||
|
||||
1. **Files.** `/etc` is restored aside, for reference only: the system's
|
||||
configuration comes from the flake.
|
||||
|
||||
```sh
|
||||
restic restore latest --target / --include /opt --include /var/backups/restic-stage
|
||||
restic restore latest --target /root/restored-etc --include /etc
|
||||
```
|
||||
|
||||
2. **Volumes** (while services still run in containers): create each
|
||||
volume, then restore into it.
|
||||
|
||||
```sh
|
||||
docker volume create storage_garage_meta # one per volume in the table
|
||||
restic restore latest --target / --include /var/lib/docker/volumes/storage_garage_meta
|
||||
```
|
||||
|
||||
Under Podman the volume lives in
|
||||
`/var/lib/containers/storage/volumes/<name>`: restore to a temporary
|
||||
target and move `_data` there.
|
||||
|
||||
3. **Databases**, from the staged copies rather than the live files:
|
||||
|
||||
- **Postgres** (ops): start Postgres alone on its empty data
|
||||
directory, then load everything, roles included:
|
||||
|
||||
```sh
|
||||
psql -U postgres -f /var/backups/restic-stage/pg_dumpall.sql
|
||||
```
|
||||
|
||||
`asxpio.dump` is the same database in `pg_restore` format, for
|
||||
restoring only that one.
|
||||
- **Forgejo** (ops): `cp /var/backups/restic-stage/forgejo-gitea.db
|
||||
/opt/forgejo/data/gitea/gitea.db`, owner as the file it replaces.
|
||||
- **Garage** (ops): if it does not start on the restored metadata, use
|
||||
the snapshot inside the metadata volume (`snapshots/`), as described
|
||||
in Garage's [recovery documentation](https://garagehq.deuxfleurs.fr/documentation/operations/recovering/).
|
||||
- **ejabberd** (services): replace `ejabberd.sqlite` in `xmpp_database`
|
||||
with the staged copy; if Mnesia does not come up clean,
|
||||
`ejabberdctl restore /opt/ejabberd/database/nightly.backup`.
|
||||
|
||||
4. **Start the services**, then check: the sites answer, a `git clone`
|
||||
from Forgejo works, an XMPP client logs in, the Minecraft world is the
|
||||
expected one, and the next nightly backup succeeds.
|
||||
|
||||
The app stacks on ops (asxpio, datacenterbingo, blog, czsk, vhpbot,
|
||||
storage) come back with `/opt/<app>` (compose file and `.env`); their
|
||||
images are pulled from the registry in Forgejo, so Forgejo and storage
|
||||
come first. If the registry copy is gone, re-run each app's CI.
|
||||
|
||||
## Onto a NixOS host
|
||||
|
||||
The backup made by a NixOS host holds the new layout (`/var/lib/forgejo`,
|
||||
`/var/lib/garage`, `/var/lib/minecraft`, ...): restore to `/`, load the
|
||||
Postgres dump, `touch /var/lib/.data-in-place`, start the services.
|
||||
|
||||
A backup made while the host was still Debian holds the layout described
|
||||
above. Restore it to a side directory and let the migration script move
|
||||
it into place; this is what was rehearsed on 2026-11-16:
|
||||
|
||||
```sh
|
||||
restic restore latest --host asxpio-ops --target /restore
|
||||
bash nixos/migration/ops-data.sh /restore # or services-data.sh
|
||||
```
|
||||
|
||||
## Keeping this true
|
||||
|
||||
A restore that was never tried is a hope. After any change to what is
|
||||
backed up, and at least twice a year, restore the latest snapshot of
|
||||
each host onto a rehearsal server (`nixos/README.md`) and walk through
|
||||
this page. Last walk-through: 2026-11-16, both hosts, onto the NixOS
|
||||
rehearsal pair (Traefik, monitoring, the app stacks and XMPP not yet
|
||||
part of it).
|
||||
192
nixos/README.md
Normal file
192
nixos/README.md
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
# NixOS
|
||||
|
||||
Each server's whole system, as one flake. A server is created by OpenTofu
|
||||
(`../terraform`), converts itself to NixOS on first boot, and gets its
|
||||
configuration from here. Moving the services over from `../ansible` is in
|
||||
progress: see `../MIGRATION.md` for what already lives here.
|
||||
|
||||
```
|
||||
flake.nix inputs (nixpkgs 26.05, unstable for a few packages, sops-nix), hosts, dev shell
|
||||
modules/hetzner.nix disk, boot and network of a Hetzner Cloud VM
|
||||
modules/base.nix users, SSH, fail2ban, firewall, journal cap, Nix settings
|
||||
modules/wireguard.nix the admin network; ops is the hub
|
||||
modules/podman.nix Podman, for what has no native NixOS service
|
||||
modules/backup.nix nightly restic backup, `restic-backup` command
|
||||
modules/alert.nix mail when a unit fails
|
||||
hosts/asxpio-ops/ what only asxpio-ops has
|
||||
hosts/asxpio-services/ what only asxpio-services has, one file per service
|
||||
hosts/rehearsal/ overrides that turn the two hosts into a test pair
|
||||
migration/ one-off scripts that move Debian-era data into place
|
||||
secrets/*.yaml sops-encrypted, one file per host
|
||||
```
|
||||
|
||||
Services run as native NixOS services wherever nixpkgs has a module;
|
||||
the rest are Podman containers declared here. There is no Docker.
|
||||
|
||||
All tools used below come from the dev shell: `nix develop ./nixos`.
|
||||
|
||||
## Changing something
|
||||
|
||||
Edit, push a branch (CI evaluates every host), then switch the host:
|
||||
|
||||
```sh
|
||||
nixos-rebuild switch --flake .#asxpio-services \
|
||||
--target-host root@10.20.0.20 --build-host root@10.20.0.20
|
||||
```
|
||||
|
||||
Only units whose definition changed restart. Undo with
|
||||
`nixos-rebuild switch --rollback` on the host, or pick the previous
|
||||
generation in the boot menu (Hetzner console). Update all packages with
|
||||
`nix flake update`, then commit `flake.lock`.
|
||||
|
||||
## Building the servers from scratch
|
||||
|
||||
What you need: a Hetzner Cloud project and its API token, Object Storage
|
||||
keys (hel1 and fsn1), an SSH key listed in `../terraform/keys.tf`, and,
|
||||
to restore data, the restic password (`../RESTORE.md`).
|
||||
|
||||
The existing servers are billed below today's list price, and a new
|
||||
server is not. **For asxpio-ops and asxpio-services use "Reinstalling in
|
||||
place" below**; this section is for a new project or a lost server.
|
||||
|
||||
1. **Create the infrastructure** (network, firewalls, DNS, buckets,
|
||||
servers):
|
||||
|
||||
```sh
|
||||
cd terraform
|
||||
cp terraform.tfvars.example terraform.tfvars # fill in the token and keys
|
||||
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... # for the state bucket
|
||||
tofu init
|
||||
tofu apply
|
||||
```
|
||||
|
||||
In a new project the state bucket does not exist yet: comment out the
|
||||
`backend "s3"` block for the first apply, then restore it and run
|
||||
`tofu init -migrate-state`.
|
||||
|
||||
2. **Wait about four minutes.** Each server boots Debian, and cloud-init
|
||||
(`../terraform/cloud-init/nixos.yaml.tftpl`) runs a pinned
|
||||
nixos-infect, which installs NixOS over the running system and
|
||||
reboots. The result is a bare NixOS: root login with the keys from
|
||||
`keys.tf`, public and private network, nothing else.
|
||||
|
||||
```sh
|
||||
ssh root@<ops public IP> nixos-version
|
||||
ssh -J root@<ops public IP> root@10.10.0.20 nixos-version # services: no public SSH
|
||||
```
|
||||
|
||||
If a server still answers as Debian after ten minutes, read
|
||||
`/var/log/nixos-infect.log` on it.
|
||||
|
||||
3. **Set the host's facts** in `hosts/<name>/default.nix`: the public
|
||||
addresses (`tofu output`), and on ops the WireGuard endpoint the
|
||||
others use. Add the host's age recipient to `.sops.yaml` and
|
||||
re-encrypt its secrets:
|
||||
|
||||
```sh
|
||||
ssh root@<ip> cat /etc/ssh/ssh_host_ed25519_key.pub | ssh-to-age
|
||||
sops updatekeys secrets/<name>.yaml
|
||||
```
|
||||
|
||||
4. **Install the configuration** (same addresses as in step 2; add
|
||||
`NIX_SSHOPTS="-J root@<ops public IP>"` for services):
|
||||
|
||||
```sh
|
||||
cd nixos
|
||||
nixos-rebuild switch --flake .#asxpio-ops \
|
||||
--target-host root@<ip> --build-host root@<ip>
|
||||
ssh root@<ip> reboot # once: new kernel command line, interface names
|
||||
```
|
||||
|
||||
From here on root and the admin users log in only from WireGuard and
|
||||
the private network (`asxpio.sshFrom`): connect your WireGuard client
|
||||
to ops first, or go through the other server over the private network.
|
||||
|
||||
5. **Restore the data**: `../RESTORE.md`.
|
||||
|
||||
6. Remove what the conversion left behind: `rm -rf /old-root` (the
|
||||
Debian image, about 2 GB).
|
||||
|
||||
## Reinstalling in place
|
||||
|
||||
Keeps the server, its addresses and its price; **wipes the disk**.
|
||||
|
||||
```sh
|
||||
cd terraform
|
||||
hcloud server disable-protection asxpio-services rebuild
|
||||
tofu output -json user_data | jq -r '."asxpio-services"' |
|
||||
hcloud server rebuild --image debian-13 --user-data-from-file - asxpio-services
|
||||
tofu apply # puts the rebuild protection back
|
||||
```
|
||||
|
||||
Then continue at step 2 above. The server gets new SSH host keys, so
|
||||
step 3's `sops updatekeys` is needed, and `known_hosts` entries change.
|
||||
|
||||
## Secrets
|
||||
|
||||
`secrets/<host>.yaml` is encrypted with [sops](https://github.com/getsops/sops)
|
||||
for the keys in `.sops.yaml`: the admin's age key and the host's SSH host
|
||||
key. The host decrypts at boot into `/run/secrets`.
|
||||
|
||||
```sh
|
||||
sops secrets/asxpio-services.yaml # edit; needs ~/.config/sops/age/keys.txt
|
||||
```
|
||||
|
||||
The SSH host key is therefore part of a host's identity. It survives the
|
||||
in-place conversion and is in the backup (`/etc/ssh`); a rebuilt server
|
||||
has a new one and needs `sops updatekeys`.
|
||||
|
||||
## Data and the marker file
|
||||
|
||||
Services that own data (`asxpio.waitForData`: Postgres, Garage, Forgejo,
|
||||
Minecraft, SillyTavern, XMPP, the backup itself) do not start until
|
||||
`/var/lib/.data-in-place` exists. A fresh install
|
||||
therefore never creates empty state where a restore is about to land,
|
||||
and never backs up an empty host over a full one. After restoring or
|
||||
moving the data: `touch /var/lib/.data-in-place`, then start the units
|
||||
or reboot.
|
||||
|
||||
## Rehearsal
|
||||
|
||||
`.#rehearsal-ops` and `.#rehearsal-services` are the two hosts' own
|
||||
configurations plus `hosts/rehearsal/`: other keys and addresses, dummy
|
||||
secrets, SSH open to the world, backups into local directories, and
|
||||
anything that would act as production (the XMPP server) started by hand
|
||||
only. **Every change is tried there first**, and the pair should be able
|
||||
to do everything production does before a production host is converted.
|
||||
|
||||
Two servers on a private network with the production addresses:
|
||||
|
||||
```sh
|
||||
hcloud network create --name rehearsal --ip-range 10.10.0.0/20
|
||||
hcloud network add-subnet rehearsal --type cloud --network-zone eu-central --ip-range 10.10.0.0/23
|
||||
tofu -chdir=../terraform output -json user_data | jq -r '."asxpio-ops"' > /tmp/ops.yaml
|
||||
tofu -chdir=../terraform output -json user_data | jq -r '."asxpio-services"' > /tmp/services.yaml
|
||||
hcloud server create --name rehearsal-ops --type cx23 --image debian-13 --location hel1 \
|
||||
--ssh-key <your key> --network rehearsal --user-data-from-file /tmp/ops.yaml
|
||||
hcloud server create --name rehearsal-services --type cx23 --image debian-13 --location hel1 \
|
||||
--ssh-key <your key> --network rehearsal --user-data-from-file /tmp/services.yaml
|
||||
```
|
||||
|
||||
Then, per server: put its age recipient in `.sops.yaml` (`&rehearsal_*`)
|
||||
and `sops updatekeys secrets/rehearsal-<role>.yaml`; put its addresses in
|
||||
`hosts/rehearsal/<role>.nix`; `nixos-rebuild switch --flake
|
||||
.#rehearsal-<role> --target-host root@<ip> --build-host root@<ip>`;
|
||||
reboot once; `touch /var/lib/.data-in-place`.
|
||||
|
||||
## How the first boot works, and its limits
|
||||
|
||||
- nixos-infect is pinned by commit and checksum in
|
||||
`../terraform/servers.tf`; it replaces the operating system, so bump it
|
||||
deliberately and rehearse.
|
||||
- It keeps the Debian image's partitions (`modules/hetzner.nix` matches
|
||||
them) and moves the old system to `/old-root`.
|
||||
- `bootstrap.nix` in the cloud-init corrects three things for the first
|
||||
generation: the scripted initrd (the takeover does not exist in the
|
||||
systemd initrd, which NixOS 26.05 uses by default), the IPv4 default
|
||||
route, and the private network's route. All three were found and
|
||||
verified on test servers on 2026-11-16.
|
||||
- The scripted initrd is deprecated upstream. When a NixOS release drops
|
||||
it, keep `nix_channel` at the last release that has it (the flake then
|
||||
upgrades the system), or switch the first boot to
|
||||
[nixos-anywhere](https://github.com/nix-community/nixos-anywhere).
|
||||
Loading…
Reference in a new issue