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:
parent
ebd4f110f4
commit
c51522d371
1 changed files with 189 additions and 377 deletions
44
terraform/cloud-init/nixos.yaml.tftpl
Normal file
44
terraform/cloud-init/nixos.yaml.tftpl
Normal 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
|
||||
Loading…
Reference in a new issue