Skip to content

Commit b768630

Browse files
authored
Merge pull request #541 from nix-community/terraform-integration-test
Add Terraform integration test for hetzner cloud
2 parents 1501dc0 + ab22f34 commit b768630

27 files changed

+743
-146
lines changed

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
result*
2-
/.direnv
2+
.direnv
33
/docs/book
4+
5+
# terraform
6+
.terraform.lock.hcl
7+
terraform/tests/hcloud/modules/ssh-key/test_key
8+
terraform/tests/hcloud/modules/ssh-key/test_key.pub
9+
terraform/tests/hcloud/test_key
10+
terraform/tests/hcloud/test_key.pub
11+
errored_test.tfstate
12+
errored_test.tfstate.backup
13+
terraform.tfstate

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
./src/flake-module.nix
2727
./tests/flake-module.nix
2828
./docs/flake-module.nix
29+
./terraform/flake-module.nix
2930
# allow to disable treefmt in downstream flakes
3031
] ++ inputs.nixpkgs.lib.optional (inputs.treefmt-nix ? flakeModule) ./treefmt/flake-module.nix;
3132

terraform/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake .#terraform

terraform/all-in-one.md

Lines changed: 27 additions & 26 deletions
Large diffs are not rendered by default.

terraform/all-in-one/main.tf

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
module "system-build" {
2-
source = "../nix-build"
3-
attribute = var.nixos_system_attr
4-
file = var.file
5-
nix_options = var.nix_options
6-
special_args = var.special_args
2+
source = "../nix-build"
3+
attribute = var.nixos_system_attr
4+
debug_logging = var.debug_logging
5+
file = var.file
6+
nix_options = var.nix_options
7+
special_args = var.special_args
78
}
89

910
module "partitioner-build" {
10-
source = "../nix-build"
11-
attribute = var.nixos_partitioner_attr
12-
file = var.file
13-
nix_options = var.nix_options
14-
special_args = var.special_args
11+
source = "../nix-build"
12+
attribute = var.nixos_partitioner_attr
13+
debug_logging = var.debug_logging
14+
file = var.file
15+
nix_options = var.nix_options
16+
special_args = var.special_args
1517
}
1618

1719
locals {
@@ -20,26 +22,26 @@ locals {
2022
}
2123

2224
module "install" {
23-
source = "../install"
24-
kexec_tarball_url = var.kexec_tarball_url
25-
target_user = local.install_user
26-
target_host = var.target_host
27-
target_port = local.install_port
28-
nixos_partitioner = module.partitioner-build.result.out
29-
nixos_system = module.system-build.result.out
30-
ssh_private_key = var.install_ssh_key
31-
debug_logging = var.debug_logging
32-
extra_files_script = var.extra_files_script
33-
disk_encryption_key_scripts = var.disk_encryption_key_scripts
34-
extra_environment = var.extra_environment
35-
instance_id = var.instance_id
36-
phases = var.phases
37-
nixos_generate_config_path = var.nixos_generate_config_path
38-
nixos_facter_path = var.nixos_facter_path
39-
build_on_remote = var.build_on_remote
25+
source = "../install"
26+
kexec_tarball_url = var.kexec_tarball_url
27+
target_user = local.install_user
28+
target_host = var.target_host
29+
target_port = local.install_port
30+
nixos_partitioner = module.partitioner-build.result.out
31+
nixos_system = module.system-build.result.out
32+
ssh_private_key = var.install_ssh_key
33+
debug_logging = var.debug_logging
34+
extra_files_script = var.extra_files_script
35+
disk_encryption_key_scripts = var.disk_encryption_key_scripts
36+
extra_environment = var.extra_environment
37+
instance_id = var.instance_id
38+
phases = var.phases
39+
nixos_generate_config_path = var.nixos_generate_config_path
40+
nixos_facter_path = var.nixos_facter_path
41+
build_on_remote = var.build_on_remote
4042
# deprecated attributes
41-
stop_after_disko = var.stop_after_disko
42-
no_reboot = var.no_reboot
43+
stop_after_disko = var.stop_after_disko
44+
no_reboot = var.no_reboot
4345
}
4446

4547
module "nixos-rebuild" {
@@ -50,12 +52,12 @@ module "nixos-rebuild" {
5052
# Do not execute this step if var.stop_after_disko == true
5153
count = var.stop_after_disko ? 0 : 1
5254

53-
source = "../nixos-rebuild"
54-
nixos_system = module.system-build.result.out
55-
ssh_private_key = var.deployment_ssh_key
56-
target_host = var.target_host
57-
target_user = var.target_user
58-
target_port = var.target_port
55+
source = "../nixos-rebuild"
56+
nixos_system = module.system-build.result.out
57+
ssh_private_key = var.deployment_ssh_key
58+
target_host = var.target_host
59+
target_user = var.target_user
60+
target_port = var.target_port
5961
install_bootloader = var.install_bootloader
6062
}
6163

terraform/all-in-one/variables.tf

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
variable "kexec_tarball_url" {
2-
type = string
2+
type = string
33
description = "NixOS kexec installer tarball url"
4-
default = null
4+
default = null
55
}
66

77
# To make this re-usable we maybe should accept a store path here?
@@ -100,8 +100,8 @@ variable "extra_files_script" {
100100
}
101101

102102
variable "disk_encryption_key_scripts" {
103-
type = list(object({
104-
path = string
103+
type = list(object({
104+
path = string
105105
script = string
106106
}))
107107
description = "Each script will be executed locally. Output of each will be created at the given path to disko during installation. The keys will be not copied to the final system"
@@ -115,9 +115,9 @@ variable "extra_environment" {
115115
}
116116

117117
variable "nix_options" {
118-
type = map(string)
118+
type = map(string)
119119
description = "the options of nix"
120-
default = {}
120+
default = {}
121121
}
122122

123123
variable "nixos_generate_config_path" {
@@ -133,8 +133,8 @@ variable "nixos_facter_path" {
133133
}
134134

135135
variable "special_args" {
136-
type = any
137-
default = {}
136+
type = any
137+
default = {}
138138
description = "A map exposed as NixOS's `specialArgs` thru a file."
139139
}
140140

@@ -145,7 +145,7 @@ variable "build_on_remote" {
145145
}
146146

147147
variable "install_bootloader" {
148-
type = bool
148+
type = bool
149149
description = "Install/re-install the bootloader"
150-
default = false
150+
default = false
151151
}

terraform/flake-module.nix

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{ inputs, ... }:
2+
{
3+
flake.nixosConfigurations.terraform-test = inputs.nixpkgs.lib.nixosSystem {
4+
system = "x86_64-linux";
5+
modules = [
6+
../tests/modules/system-to-install.nix
7+
inputs.disko.nixosModules.disko
8+
(args: {
9+
# Example usage of special args from terraform
10+
networking.hostName = args.terraform.hostname or "nixos-anywhere";
11+
12+
# Create testable files in /etc based on terraform special_args
13+
environment.etc = {
14+
"terraform-config.json" = {
15+
text = builtins.toJSON args.terraform or { };
16+
mode = "0644";
17+
};
18+
};
19+
})
20+
];
21+
};
22+
23+
perSystem = { pkgs, ... }: {
24+
devShells.terraform = pkgs.mkShell {
25+
buildInputs = with pkgs; [
26+
terraform-docs
27+
(opentofu.withPlugins (p: [
28+
p.tls
29+
p.hcloud
30+
p.local
31+
p.external
32+
p.null
33+
]))
34+
];
35+
36+
shellHook = ''
37+
echo "🚀 Terraform development environment"
38+
echo "Available tools:"
39+
echo " - terraform-docs"
40+
echo " - opentofu"
41+
echo ""
42+
echo "To run tests: cd terraform/tests && tofu test"
43+
echo "To update docs: cd terraform && ./update-docs.sh"
44+
'';
45+
};
46+
};
47+
}

0 commit comments

Comments
 (0)