terraform: install NixOS at first boot, run with OpenTofu

A new or rebuilt server should come up as NixOS without a manual
install. cloud-init now runs a pinned nixos-infect on the Debian image
and reboots into a bare NixOS; the system itself comes from nixos/.
One template replaces the two near-identical Debian cloud-init files.

nixos-infect's output needs three corrections on NixOS 26.05, written
to bootstrap.nix: the takeover of the old root only exists in the
scripted initrd, the IPv4 default route is never added without an
interface, and the private network gets no route. Found and verified
on throwaway servers.

The servers still ignore changes to user_data, so nothing happens to
the running ones. The new user_data output feeds a rebuild in place.

Switch to OpenTofu: same HCL and state, and it is free software in
nixpkgs. Commit the provider lock file so everyone gets the same
providers.

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

View file

@ -1,3 +1,24 @@
# Debian on first boot installs NixOS over itself (cloud-init runs
# nixos-infect); the system itself is described in ../nixos.
locals {
user_data = {
for host in ["asxpio-ops", "asxpio-services"] : host => templatefile(
"${path.module}/cloud-init/nixos.yaml.tftpl", {
hostname = host
domain = "asxp.io"
# Pinned: this script replaces the operating system
infect_rev = "40f62a680bb0e8f2f607d79abfaaecd99d59401c"
infect_sha256 = "4354bd68773b41da65c0e815202c43c8549713b3ed3ff6381c71fbc0b0a840ab"
nix_channel = "nixos-26.05"
private_net = split("/", hcloud_network.main.ip_range)[0]
private_prefix = split("/", hcloud_network.main.ip_range)[1]
private_gateway = cidrhost(hcloud_network.main.ip_range, 1)
}
)
}
}
# Ops Server (2 vCPU, 4GB RAM)
resource "hcloud_server" "ops" {
name = "asxpio-ops"
@ -29,17 +50,17 @@ resource "hcloud_server" "ops" {
firewall_ids = [hcloud_firewall.ops_fw.id]
labels = {
role = "ops"
env = "production"
managed_by = "terraform"
project = "asxpio"
role = "ops"
env = "production"
managed_by = "terraform"
project = "asxpio"
}
# Cloud-init for automated server setup
user_data = file("${path.module}/cloud-init/ops.yaml")
# cloud-init only runs at first boot, and hcloud replaces the server
# when user_data, image or ssh_keys change. Never let an edit to those
# destroy a running machine: changes there apply to a rebuild only.
# First boot converts the Debian image to NixOS (see local.user_data).
# hcloud replaces the server when user_data, image or ssh_keys change.
# Never let an edit to those destroy a running machine: they apply to
# a new server or to a rebuild in place (../nixos/README.md) only.
user_data = local.user_data["asxpio-ops"]
lifecycle {
ignore_changes = [user_data, image, ssh_keys]
prevent_destroy = true
@ -77,17 +98,17 @@ resource "hcloud_server" "services" {
firewall_ids = [hcloud_firewall.services_fw.id]
labels = {
role = "services"
env = "production"
managed_by = "terraform"
project = "asxpio"
role = "services"
env = "production"
managed_by = "terraform"
project = "asxpio"
}
# Cloud-init for automated server setup
user_data = file("${path.module}/cloud-init/services.yaml")
# cloud-init only runs at first boot, and hcloud replaces the server
# when user_data, image or ssh_keys change. Never let an edit to those
# destroy a running machine: changes there apply to a rebuild only.
# First boot converts the Debian image to NixOS (see local.user_data).
# hcloud replaces the server when user_data, image or ssh_keys change.
# Never let an edit to those destroy a running machine: they apply to
# a new server or to a rebuild in place (../nixos/README.md) only.
user_data = local.user_data["asxpio-services"]
lifecycle {
ignore_changes = [user_data, image, ssh_keys]
prevent_destroy = true