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
3 changed files with 189 additions and 377 deletions

View file

@ -0,0 +1,44 @@
#cloud-config
# First boot of the Debian image: convert it to NixOS in place with
# nixos-infect, then reboot. The result is a bare NixOS that keeps the
# image's partitions and the SSH host keys, with root's authorized_keys
# taken from the server's ssh_keys. Everything else comes from the flake:
#
# nixos-rebuild switch --flake ../nixos#${hostname} --target-host root@<ip>
#
# 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

View file

@ -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}')
==========================================

View file

@ -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}')
==========================================