Terraform nixOS on Hetzner

Does anyone have a working setup to deploy NixOS machines on Hetzner using Terraform/OpenTofu?

2 Likes

i just deployed a few machines with nixos-infect and terraform last week… what problems did you run into?

Are you looking for a single command that both provisions a VPS and deploys NixOS with a set configuration on it?

For just the provisioning, Terranix: Getting Started has an example of how you can provision a Hetzner VPS with Debian using Terraform/OpenTofu using Nix syntax. Debian since Hetzner doesn’t have NixOS image in its image registry.

For deploying NixOS on Hetzner you can:

  • use nixos-infect – it’s a little dated, but worked fine for me on Hetzner earlier this year. For Hetzner you can inject a curl command into cloud-config, making it “infect” the machine on first boot.
  • use nixos-anywhere – it’s somewhat taken over nixos-infect and is very well-maintained. The readme doesn’t contain a simple one-liner example, which is a pity.
  • use hcloud-upload-image – it’s a tool made by Julian Tölle who does open source integrations at Hetzner Cloud. It lets you create custom VPS images by abusing Hetzner’s “snapshot” functionality. It has some great potential for maintaining custom images so you don’t need a second installation phase after booting your new VPS’es the first time. I’ve linked to the (unmerged) PR that adds Nix wrapping for the tool, since it’s not in nixpkgs yet. There’s no tutorial for this, as far as I know. Its main use case is more configuration-free distros like Talos (the Kubernetes Linux distro), but you can make a generic NixOS image that outsources the updating parts to an external repository.