Deploying NixOS using Terraform


In case the destination provider doesn’t have built-in support for spawning the machine with a custom image (read: hetzner cloud), I’ve found okay to use this snippet found here: Hetzner Cloud with NixOS :: OroBlog
It runs nixos-infect by using cloud-init.

resource "hcloud_server" "nameofserver" {
  [... other attributes ... ]
  ssh_keys = [ "name of ssh key on hetzner" ]
  user_data = "#cloud-config\nruncmd:\n- curl | PROVIDER=hetznercloud NIX_CHANNEL=nixos-unstable bash 2>&1 | tee /tmp/infect.log\n"

The server will reboot into nixos once the setup is finished, it will keep the same SSH keys specified in the ssh_keys attribute.