From c51522d371d7a898146d3370337b2a7c5725bd92 Mon Sep 17 00:00:00 2001 From: Sergei Poljanski Date: Mon, 16 Nov 2026 22:59:58 +0400 Subject: [PATCH] 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 --- terraform/.gitignore | 3 +- terraform/.terraform.lock.hcl | 68 +++++++++++++ terraform/README.md | 111 +++++---------------- terraform/cloud-init/nixos.yaml.tftpl | 44 +++++++++ terraform/cloud-init/ops.yaml | 136 -------------------------- terraform/cloud-init/services.yaml | 134 ------------------------- terraform/outputs.tf | 8 ++ terraform/providers.tf | 5 +- terraform/servers.tf | 57 +++++++---- 9 files changed, 189 insertions(+), 377 deletions(-) create mode 100644 terraform/.terraform.lock.hcl create mode 100644 terraform/cloud-init/nixos.yaml.tftpl delete mode 100644 terraform/cloud-init/ops.yaml delete mode 100644 terraform/cloud-init/services.yaml diff --git a/terraform/.gitignore b/terraform/.gitignore index eef07c5..4ed7134 100644 --- a/terraform/.gitignore +++ b/terraform/.gitignore @@ -16,9 +16,8 @@ terraform.tfvars .terraformrc terraform.rc -# Terraform directories +# Terraform directories (the lock file is committed: it pins the providers) .terraform/ -.terraform.lock.hcl # Crash log files crash.log diff --git a/terraform/.terraform.lock.hcl b/terraform/.terraform.lock.hcl new file mode 100644 index 0000000..0bc2893 --- /dev/null +++ b/terraform/.terraform.lock.hcl @@ -0,0 +1,68 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/aminueza/minio" { + version = "3.46.3" + constraints = "~> 3.11" + hashes = [ + "h1:BucOu+t3X1LSQzolHrWUE4+tPoMEi/C+fjeOLxilysA=", + "h1:CHKkCVQ4mARCFbTl3CHOKZg6tLDIXhE4xBTY9/H8998=", + "h1:DOOZypFT5G5fknb5U4Kp59WeYZU1OgiBOaZq2xljW1Q=", + "h1:JufolSPtuQatY/9D0+V3iplgvlVPqd2hoU5P4QIyziM=", + "h1:bmRxF3I9fP1xokjhQB0K2ujFtidd3UNp88k/DBjR1lE=", + "h1:c8Kzjx+0EnpV37xtpfoxI/7GckVrLx0dmIfDuAx2CF8=", + "h1:cJr90p040Vk8J8DVEKvTtxdDr+uLkcA4Na6gfJdLfeQ=", + "h1:cRpLvfM+mNNsvLIgeogpn2MKkko3v7HtcYEBYxP9elI=", + "h1:jQfBea+ntcQk0eIXfJodo8Yrvs+8R7+1ejidKaNZXR0=", + "h1:luMw2XWKS2a8yi/yIqn2qAw2rLAdAkNnbIHapkzpT9c=", + "h1:o+nzoc77gQN6pqPUGa1q17r35usD1k9LbIMiAibg4q4=", + "h1:syn1oZHCH6mydfTks8+aWfs2oWQNF3ii3HvSXuA5oJM=", + "h1:tUraLqzJBd5KAyxz2wVSSqMWc8oh3RmBbZaoFHvg+w0=", + "zh:01733c14f9d0724c404a258b668732f99f86ce16e65747902f7e51fb276fffe0", + "zh:127371a47bd643ca3a84523f1426c00e81195e3f10e30c7bdb8ca7987cc3c7ef", + "zh:1b27dad321fbdd550d328a63bc85093c72281a6af91ad6d249bbf15774dd91dd", + "zh:6c28ac386489ed904d1cf88382d21dba4ca749b1da6fcb1a3ecb11c0d6234f9f", + "zh:7cced05a3b2f26a03ad76cc13f655aab842f26db38a47a72020ff2ac7fcede47", + "zh:87cd6395207e20f532891902e44f2fdb92028669176cef7a8a30f1dc33d2e50f", + "zh:94c299b67007310e0e57892465ae54c9591749376916782e242dad3ecf9c7332", + "zh:95537fd6ccf778917c25e25fcc2c75a0599210db993e23dcf1b4c5c8eae7d234", + "zh:be52ecb44f323cce598e85084987948e2d3c6f07bf31250c372f28503831d4b8", + "zh:c3e0f2bdde045b27ea3ad14d65d5f595e2e48fa4b840c4fd1d107d3330fe49a0", + "zh:d63c68cc4cf30d3173a3207f95460d8882b79c3679ee51e45b008dc734df1d89", + "zh:ef6a3f439cc877133597251953cffd6ed3574039e1a4efd241f6ecb232baac5e", + "zh:f426a35a940ae70aa2c2931b691700c8285c8af49a2d8060df8031ed2a567ee9", + ] +} + +provider "registry.opentofu.org/hetznercloud/hcloud" { + version = "1.70.2" + constraints = "~> 1.70" + hashes = [ + "h1:2eVvib40d9ki4E/wvPnT2wXxM704YjBRVxkozKHGTCs=", + "h1:BZWdJOileBlR+WMz0D0wrJXFhNLJpHcQJIgDqmPNlpw=", + "h1:EV7uBBPrEpNz8gJJ//iyBUu9/Tt+JJKjL3D3f9xVzX0=", + "h1:J5jjFM74jJrhwWKRdyQNFC3Ef7pbFBkLXGxbLSXheks=", + "h1:RYD1e858eOJstFN/bf9KonHtltMycZKIcYu7ngyiIRE=", + "h1:UB3bnUROLsLu1hreopppKw+Wm22//b3YZtM1k/SLloM=", + "h1:VB4xF79qoFHtcdxvEcajWgJZLJ59/uvbCReraaEHwqM=", + "h1:byNVX40XzpAO0MRuyxipatu4RWPfWKZ4Mf6PKyz0Tjk=", + "h1:cRkaW7N60DfWVvMhD8QtH2MdDDQzNOAJs0Gr5cmzwlE=", + "h1:eYR/chP/jFcSQlmPcfFBktyxZsoJuYRlJ57pOLVq1/k=", + "h1:hxmW37pH+dIDMH+9JQ9GSwjZg72SfvKIWUeJctmcy1M=", + "h1:qwPVmCN5bLh/2MjvsoatZhBvsvD8Nhs2J8OySiYN17M=", + "h1:wechRCpT64RPYT0BGWlkj/Y59BQa/qhO4elGaAlRpBo=", + "zh:05a474ecd2b019be03f857877c9bf1eca8f96c0897f6e76b91780adf3a8dd388", + "zh:1d4e4c0525c02618c015c079dc3e8d8b693ca796d698f95b59c4412114cbf166", + "zh:1f3bc703423f08a295e3e8f6d39c3f155e672da5e20b9a143ef7e7a465be37e3", + "zh:20ced3eec332b587a998ad3d87da322736aef49cb7e561a545a57f9a08000050", + "zh:619152b472b9ac266046db9d0c3f85946559ce0791bcb6fc78d278bb555d4572", + "zh:658a84427cc6b5385d68e1506678498dd7d3d04c0cf3410e3f2278c05b247ade", + "zh:6d9be3d79479345d21cef66e83002086153189535c82ab299d6ca5021aec7d0e", + "zh:72b0d26aadcc422ccb17e7a89abf92dfbe52dca6fc80025a934d7c4e058888cd", + "zh:865c0dc4912a5009f7b8c7ff374d47e496f9cfe96cbcebadc48f3434b7c12b46", + "zh:99f3f0778398d7348e5ab7dab42de0c918339a5250d140b6959272e50be4e372", + "zh:b3df6415bbe0f7021f94bff9e640c42e6d45b4a459ded3d267f10e2104a77760", + "zh:e7645b34e1aa45cb17dc473b33d7fa4d60e81d7e14332c7b04bc97535c867074", + "zh:f60b3e67be5cafe10001bb556ae7693e4a7696fb6946294643182946b36117f9", + ] +} diff --git a/terraform/README.md b/terraform/README.md index abc1138..3cb9f6a 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -1,6 +1,9 @@ # ASXP.IO Infrastructure -Hetzner Cloud infrastructure managed via Terraform. +Hetzner Cloud infrastructure, managed with [OpenTofu](https://opentofu.org) +(`tofu`; the dev shell of `../nixos` has it). The files are plain HCL and +the state is the one Terraform wrote; `.terraform.lock.hcl` pins the +providers and is committed. ## Servers @@ -11,10 +14,15 @@ Both have delete and rebuild protection. Hetzner's server backups are off; restic backs up the data instead (see ../README.md). Both servers carry `prevent_destroy` and ignore changes to `user_data`, -`image` and `ssh_keys`: cloud-init only runs at first boot, and hcloud -would otherwise replace a running server when those change. Edits to -`cloud-init/*.yaml` therefore describe a rebuild, not the live machine; -day-to-day configuration is in `../ansible`. +`image` and `ssh_keys`, because hcloud would replace a running server +when those change. + +A new server boots Debian and converts itself to NixOS: its cloud-init +(`cloud-init/nixos.yaml.tftpl`, one template for both) runs a pinned +nixos-infect and reboots into a bare NixOS, about three minutes after +creation. The system's configuration then comes from `../nixos`. Edits +to the template reach a server only when it is created or rebuilt in +place; both procedures are in `../nixos/README.md`. ## Storage (S3-compatible) @@ -68,94 +76,27 @@ delete protection guard against it. ## Setup -### 1. Initialize ```bash -terraform init +cp terraform.tfvars.example terraform.tfvars # token and Object Storage keys +export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... # state bucket +tofu init +tofu plan +tofu apply ``` -### 2. Configure secrets -Edit `terraform.tfvars`: -```hcl -hcloud_token = "your-token" -s3_access_key = "your-key" -s3_secret_key = "your-secret" -s3_endpoint_hel1 = "hel1.your-objectstorage.com" -s3_endpoint_fsn1 = "fsn1.your-objectstorage.com" -``` +In a new project the state bucket does not exist yet: comment out the +`backend "s3"` block in `providers.tf` for the first apply, restore it, +then `tofu init -migrate-state`. -### 3. Deploy -```bash -terraform plan -terraform apply -``` - -### 4. Enable S3 backend (optional) -After buckets are created: - -```bash -# Set credentials -export AWS_ACCESS_KEY_ID="your-key" -export AWS_SECRET_ACCESS_KEY="your-secret" - -# Or use ~/.aws/credentials file -mkdir -p ~/.aws -cat > ~/.aws/credentials < +# +# Log: /var/log/nixos-infect.log (after the reboot under /old-root). + +hostname: ${hostname} +fqdn: ${hostname}.${domain} +manage_etc_hosts: true +ssh_deletekeys: false + +write_files: + # Corrections to what nixos-infect generates, for this bootstrap + # generation only: the takeover of the old root (NIXOS_LUSTRATE) exists + # only in the scripted initrd; without an interface the IPv4 default + # route is never added (the gateway is not on the /32's link); and the + # private network gets an address but no route. + - path: /etc/nixos/bootstrap.nix + content: | + { lib, ... }: { + boot.initrd.systemd.enable = false; + networking.defaultGateway = lib.mkForce { + address = "172.31.1.1"; + interface = "eth0"; + }; + networking.interfaces.enp7s0.ipv4.routes = [ + { address = "${private_gateway}"; prefixLength = 32; } + { address = "${private_net}"; prefixLength = ${private_prefix}; via = "${private_gateway}"; } + ]; + } + +runcmd: + # Debian keeps /tmp in RAM; nixos-infect puts its swap file there + - >- + umount -l /tmp; + curl -fsSL -o /root/nixos-infect + https://raw.githubusercontent.com/elitak/nixos-infect/${infect_rev}/nixos-infect + && echo "${infect_sha256} /root/nixos-infect" | sha256sum -c + && PROVIDER=hetznercloud NIX_CHANNEL=${nix_channel} NIXOS_IMPORT=./bootstrap.nix + bash -x /root/nixos-infect > /var/log/nixos-infect.log 2>&1 diff --git a/terraform/cloud-init/ops.yaml b/terraform/cloud-init/ops.yaml deleted file mode 100644 index ddffdee..0000000 --- a/terraform/cloud-init/ops.yaml +++ /dev/null @@ -1,136 +0,0 @@ -#cloud-config -# Cloud-init configuration for asxpio-ops server -# Debian 13 (trixie) - Ops Server - -hostname: asxpio-ops -fqdn: asxpio-ops.asxp.io -manage_etc_hosts: true - -# Package management -package_update: true -package_upgrade: true - -packages: - - fail2ban - - curl - - gpg - - htop - - ncdu - - tree - - apt-transport-https - - ca-certificates - - gnupg - - lsb-release - -# Create users (docker group will be added later in runcmd) -users: - - name: asxpi - groups: sudo - shell: /bin/bash - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - lock_passwd: true - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMZ+/5F7SxdTVW47iiStLpGK77oWfR5NgaK4tTSR/aVB me@asxp.io - - - name: zerrtika - groups: sudo - shell: /bin/bash - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - lock_passwd: true - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIELE7hTD4sC5raqjSxy2fXyM+q1CwwVuy9KLFgDR6SFl zerrtika@zerrtika-mac - - - name: deploy - groups: sudo - shell: /bin/bash - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - lock_passwd: true - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGTDPg3mV55lO8uBnvpcGRxJ2vMRP0qB3Pxhg+C8rwj9 asxpio-deploy - -# SSH hardening -ssh_pwauth: false -disable_root: false -ssh_deletekeys: false - -# Write files -write_files: - # Docker daemon configuration - - path: /etc/docker/daemon.json - content: | - { - "live-restore": true, - "storage-driver": "overlay2", - "log-opts": { - "max-size": "20m" - } - } - owner: root:root - permissions: '0644' - - # Custom bash prompt for asxpi user - - path: /home/asxpi/.bashrc_custom - content: | - # Custom bash prompt styling for asxpio servers - export PS1="\[$(tput bold)\]\[\033[38;5;4m\][\[$(tput sgr0)\]\t\[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\]]\[$(tput sgr0)\] \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\][\[$(tput sgr0)\]\u@\h \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\]\w]\[$(tput sgr0)\] \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\]\\$\[$(tput sgr0)\] \[$(tput sgr0)\]" - owner: root:root - permissions: '0644' - - # SSH hardening config - - path: /etc/ssh/sshd_config.d/99-hardening.conf - content: | - PasswordAuthentication no - PermitRootLogin prohibit-password - PubkeyAuthentication yes - owner: root:root - permissions: '0644' - -runcmd: - # Install Docker - - curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg - - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null - - apt-get update - - apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin - - # Enable and start Docker - - systemctl enable docker - - systemctl start docker - - # Add users to docker group (after docker is installed) - - usermod -aG docker asxpi - - usermod -aG docker zerrtika - - usermod -aG docker deploy - - # Enable and start fail2ban - - systemctl enable fail2ban - - systemctl start fail2ban - - # Fix home directory ownership and setup bashrc for asxpi - - chown asxpi:asxpi /home/asxpi - - chown asxpi:asxpi /home/asxpi/.bashrc_custom - - cp /etc/skel/.bashrc /home/asxpi/.bashrc - - cp /etc/skel/.profile /home/asxpi/.profile - - chown asxpi:asxpi /home/asxpi/.bashrc /home/asxpi/.profile - - echo '[ -f ~/.bashrc_custom ] && source ~/.bashrc_custom' >> /home/asxpi/.bashrc - - # Fix home directory ownership for deploy user - - chown deploy:deploy /home/deploy - - # Set up weekly Docker image pruning - - echo '0 3 * * 1 docker image prune -af > /var/log/docker-prune.log 2>&1' | crontab - - - # Restart SSH to apply hardening - - systemctl restart sshd - -final_message: | - ========================================== - asxpio-ops server is ready! - ========================================== - Hostname: asxpio-ops - OS: Debian 13 (trixie) - User: asxpi (sudo + docker access) - SSH: Key-only authentication enabled - Docker: Installed with compose plugin - - You can now SSH: ssh asxpi@$(hostname -I | awk '{print $1}') - ========================================== diff --git a/terraform/cloud-init/services.yaml b/terraform/cloud-init/services.yaml deleted file mode 100644 index 2880e0b..0000000 --- a/terraform/cloud-init/services.yaml +++ /dev/null @@ -1,134 +0,0 @@ -#cloud-config -# Cloud-init configuration for asxpio-services server -# Debian 13 (trixie) - Services Server - -hostname: asxpio-services -fqdn: asxpio-services.asxp.io -manage_etc_hosts: true - -# Package management -package_update: true -package_upgrade: true - -packages: - - fail2ban - - curl - - gpg - - htop - - ncdu - - tree - - apt-transport-https - - ca-certificates - - gnupg - - lsb-release - -# Create users (docker group will be added later in runcmd) -users: - - name: asxpi - groups: sudo - shell: /bin/bash - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - lock_passwd: true - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMZ+/5F7SxdTVW47iiStLpGK77oWfR5NgaK4tTSR/aVB me@asxp.io - - name: zerrtika - groups: sudo - shell: /bin/bash - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - lock_passwd: true - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIELE7hTD4sC5raqjSxy2fXyM+q1CwwVuy9KLFgDR6SFl zerrtika@zerrtika-mac - - name: deploy - groups: sudo - shell: /bin/bash - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - lock_passwd: true - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGTDPg3mV55lO8uBnvpcGRxJ2vMRP0qB3Pxhg+C8rwj9 asxpio-deploy - -# SSH hardening -ssh_pwauth: false -disable_root: false -ssh_deletekeys: false - -# Write files -write_files: - # Docker daemon configuration - - path: /etc/docker/daemon.json - content: | - { - "live-restore": true, - "storage-driver": "overlay2", - "log-opts": { - "max-size": "20m" - } - } - owner: root:root - permissions: '0644' - - # Custom bash prompt for asxpi user - - path: /home/asxpi/.bashrc_custom - content: | - # Custom bash prompt styling for asxpio servers - export PS1="\[$(tput bold)\]\[\033[38;5;4m\][\[$(tput sgr0)\]\t\[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\]]\[$(tput sgr0)\] \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\][\[$(tput sgr0)\]\u@\h \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\]\w]\[$(tput sgr0)\] \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;4m\]\\$\[$(tput sgr0)\] \[$(tput sgr0)\]" - owner: root:root - permissions: '0644' - - # SSH hardening config - - path: /etc/ssh/sshd_config.d/99-hardening.conf - content: | - PasswordAuthentication no - PermitRootLogin prohibit-password - PubkeyAuthentication yes - owner: root:root - permissions: '0644' - -runcmd: - # Install Docker - - curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg - - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null - - apt-get update - - apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin - - # Enable and start Docker - - systemctl enable docker - - systemctl start docker - - # Add users to docker group (after docker is installed) - - usermod -aG docker asxpi - - usermod -aG docker zerrtika - - usermod -aG docker deploy - - # Enable and start fail2ban - - systemctl enable fail2ban - - systemctl start fail2ban - - # Fix home directory ownership and setup bashrc for asxpi - - chown asxpi:asxpi /home/asxpi - - chown asxpi:asxpi /home/asxpi/.bashrc_custom - - cp /etc/skel/.bashrc /home/asxpi/.bashrc - - cp /etc/skel/.profile /home/asxpi/.profile - - chown asxpi:asxpi /home/asxpi/.bashrc /home/asxpi/.profile - - echo '[ -f ~/.bashrc_custom ] && source ~/.bashrc_custom' >> /home/asxpi/.bashrc - - # Fix home directory ownership for deploy user - - chown deploy:deploy /home/deploy - - # Set up weekly Docker image pruning - - echo '0 3 * * 1 docker image prune -af > /var/log/docker-prune.log 2>&1' | crontab - - - # Restart SSH to apply hardening - - systemctl restart sshd - -final_message: | - ========================================== - asxpio-services server is ready! - ========================================== - Hostname: asxpio-services - OS: Debian 13 (trixie) - User: asxpi (sudo + docker access) - SSH: Key-only authentication enabled - Docker: Installed with compose plugin - - You can now SSH: ssh asxpi@$(hostname -I | awk '{print $1}') - ========================================== diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 5224cb9..87b04fd 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -27,3 +27,11 @@ output "vpc_network" { } description = "VPC network info" } + +# For a rebuild in place: +# tofu output -json user_data | jq -r '."asxpio-ops"' | +# hcloud server rebuild --image debian-13 --user-data-from-file - asxpio-ops +output "user_data" { + value = local.user_data + description = "cloud-init per server, as a new or rebuilt server gets it" +} diff --git a/terraform/providers.tf b/terraform/providers.tf index 5d1bbae..7c61e65 100644 --- a/terraform/providers.tf +++ b/terraform/providers.tf @@ -1,5 +1,6 @@ +# Run with OpenTofu (`tofu`, in the dev shell of ../nixos). terraform { - required_version = ">= 1.0.0" + required_version = ">= 1.8.0" # S3 backend - comment this section if bucket not yet created backend "s3" { @@ -11,7 +12,7 @@ terraform { skip_metadata_api_check = true skip_region_validation = true skip_requesting_account_id = true - force_path_style = true + use_path_style = true } required_providers { diff --git a/terraform/servers.tf b/terraform/servers.tf index f908ac7..c0a5087 100644 --- a/terraform/servers.tf +++ b/terraform/servers.tf @@ -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