1+ terraform {
2+ required_providers {
3+ digitalocean = {
4+ source = " digitalocean/digitalocean"
5+ version = " ~> 2.34"
6+ }
7+ tls = {
8+ source = " hashicorp/tls"
9+ version = " ~> 4.0"
10+ }
11+ local = {
12+ source = " hashicorp/local"
13+ version = " ~> 2.4"
14+ }
15+ }
16+ }
17+
18+ provider "digitalocean" {
19+ token = var. digitalocean_token
20+ }
21+
22+ variable "digitalocean_token" {
23+ description = " DigitalOcean API token"
24+ type = string
25+ sensitive = true
26+ }
27+
28+ variable "test_name_prefix" {
29+ description = " Prefix for test resource names"
30+ type = string
31+ default = " tftest-nixos-anywhere"
32+ }
33+
34+ variable "nixos_system_attr" {
35+ description = " NixOS system attribute to deploy"
36+ type = string
37+ }
38+
39+ variable "nixos_partitioner_attr" {
40+ description = " NixOS partitioner attribute"
41+ type = string
42+ }
43+
44+ variable "debug_logging" {
45+ description = " Enable debug logging"
46+ type = bool
47+ default = false
48+ }
49+
50+ # Generate SSH key pair
51+ resource "tls_private_key" "test_key" {
52+ algorithm = " ED25519"
53+ }
54+
55+ # Save private key to file
56+ resource "local_file" "private_key" {
57+ content = tls_private_key. test_key . private_key_openssh
58+ filename = " ${ path . root } /test_key"
59+ file_permission = " 0600"
60+ }
61+
62+ # Save public key to file
63+ resource "local_file" "public_key" {
64+ content = tls_private_key. test_key . public_key_openssh
65+ filename = " ${ path . root } /test_key.pub"
66+ }
67+
68+ # Create DigitalOcean SSH key
69+ resource "digitalocean_ssh_key" "test_key" {
70+ name = " ${ var . test_name_prefix } -deployment-key"
71+ public_key = tls_private_key. test_key . public_key_openssh
72+ }
73+
74+ # Create test droplet
75+ # Note: Using s-2vcpu-2gb (minimum 2GB RAM required for nixos-anywhere kexec)
76+ # DigitalOcean uses /dev/vda for disk devices (handled by digitalocean config)
77+ resource "digitalocean_droplet" "test_server" {
78+ name = " ${ var . test_name_prefix } -server"
79+ image = " ubuntu-22-04-x64"
80+ size = " s-2vcpu-2gb"
81+ region = " nyc3"
82+ ssh_keys = [digitalocean_ssh_key . test_key . id ]
83+
84+ tags = [
85+ " nixos-anywhere-test" ,
86+ replace (replace (replace (timestamp (), " :" , " -" ), " T" , " -" ), " Z" , " " )
87+ ]
88+ }
89+
90+ # nixos-anywhere all-in-one module
91+ # Uses digitalocean configuration from nixos-anywhere-examples which:
92+ # - Sets disk device to /dev/vda (DigitalOcean standard)
93+ # - Configures cloud-init for network setup
94+ # - Disables DHCP in favor of cloud-init provisioning
95+ module "nixos_anywhere" {
96+ source = " ../../all-in-one"
97+
98+ nixos_system_attr = var. nixos_system_attr
99+ nixos_partitioner_attr = var. nixos_partitioner_attr
100+ target_host = digitalocean_droplet. test_server . ipv4_address
101+ target_port = 22
102+ target_user = " root"
103+ debug_logging = var. debug_logging
104+ deployment_ssh_key = tls_private_key. test_key . private_key_openssh
105+ install_ssh_key = tls_private_key. test_key . private_key_openssh
106+
107+ special_args = {
108+ extraPublicKeys = [tls_private_key.test_key.public_key_openssh]
109+ }
110+ }
111+
112+ output "nixos_anywhere_result" {
113+ description = " nixos-anywhere module result"
114+ value = module. nixos_anywhere . result
115+ }
116+
117+ output "droplet_ip" {
118+ description = " DigitalOcean droplet public IP address"
119+ value = digitalocean_droplet. test_server . ipv4_address
120+ }
121+
122+ output "droplet_id" {
123+ description = " DigitalOcean droplet ID for cleanup"
124+ value = digitalocean_droplet. test_server . id
125+ }
126+
127+ output "ssh_key_id" {
128+ description = " DigitalOcean SSH key ID for cleanup"
129+ value = digitalocean_ssh_key. test_key . id
130+ }
131+
132+ output "ssh_connection_command" {
133+ description = " SSH command to connect to the deployed server"
134+ value = " ssh -i ${ local_file . private_key . filename } root@${ digitalocean_droplet . test_server . ipv4_address } "
135+ }
0 commit comments