Deploying NixOS using Terraform

https://nix.dev/tutorials/deploying-nixos-using-terraform.html

4 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: 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 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.

6 Likes