Deploying NixOS using Terraform

2 Likes

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: https://blog.oro.nu/posts/hetzner-cloud-with-nixos/
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 https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | 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.

2 Likes