Skip to content

Commit 0a8ab32

Browse files
committed
add regression test for special-args issue in terraform
1 parent bf3564a commit 0a8ab32

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

terraform/flake-module.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
modules = [
66
../tests/modules/system-to-install.nix
77
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+
})
820
];
921
};
1022

terraform/nix-build/nix-build.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ else
3131

3232
# grab flake nar from error message
3333
flake_rel="$(echo "${attribute}" | cut -d "#" -f 1)"
34-
# e.g. flake_rel="."
35-
flake_dir="$(readlink -f "${flake_rel}")"
36-
flake_nar="$(nix flake prefetch "${flake_dir}" --json | jq -r '.hash')"
34+
35+
# Use nix flake prefetch to get the flake into the store, then use path:// URL with narHash
36+
prefetch_result="$(nix flake prefetch "${flake_rel}" --json)"
37+
store_path="$(echo "${prefetch_result}" | jq -r '.storePath')"
38+
nar_hash="$(echo "${prefetch_result}" | jq -r '.hash')"
39+
flake_url="path:${store_path}?narHash=${nar_hash}"
40+
3741
# substitute variables into the template
38-
nix_expr="(builtins.getFlake ''file://${flake_dir}/flake.nix?narHash=${flake_nar}'').${config_path}.extendModules { specialArgs = builtins.fromJSON ''${special_args}''; }"
42+
nix_expr="(builtins.getFlake ''${flake_url}'').${config_path}.extendModules { specialArgs = builtins.fromJSON ''${special_args}''; }"
3943
# inject `special_args` into nixos config's `specialArgs`
44+
4045
# shellcheck disable=SC2086
4146
out=$(nix build --no-link --json ${options} --expr "${nix_expr}" "${config_attribute}")
4247
fi
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Terraform Test Configuration for nix-build module with special_args
2+
# Tests the fix for nested flakes in git repositories
3+
# Run with: tofu test
4+
5+
variables {
6+
test_name_prefix = "tftest-nix-build-special-args"
7+
}
8+
9+
# Test: nix-build module with special_args using terraform-test configuration
10+
run "validate_nix_build_special_args" {
11+
command = plan
12+
13+
module {
14+
source = "../nix-build"
15+
}
16+
17+
variables {
18+
attribute = ".#nixosConfigurations.terraform-test.config.system.build.toplevel"
19+
special_args = {
20+
terraform = {
21+
hostname = "special-args-test-host"
22+
ip_address = "192.168.1.100"
23+
environment = "testing"
24+
deployment_id = "test-001"
25+
}
26+
}
27+
}
28+
29+
assert {
30+
condition = var.special_args.terraform.hostname == "special-args-test-host"
31+
error_message = "special_args should preserve hostname value"
32+
}
33+
34+
assert {
35+
condition = var.special_args.terraform.environment == "testing"
36+
error_message = "special_args should preserve environment value"
37+
}
38+
39+
assert {
40+
condition = var.attribute == ".#nixosConfigurations.terraform-test.config.system.build.toplevel"
41+
error_message = "should use terraform-test configuration"
42+
}
43+
}

tests/modules/system-to-install.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{ modulesPath, self, ... }: {
1+
{ modulesPath, self, lib, ... }: {
22
imports = [
33
(modulesPath + "/testing/test-instrumentation.nix")
44
(modulesPath + "/profiles/qemu-guest.nix")
55
(modulesPath + "/profiles/minimal.nix")
66
];
7-
networking.hostName = "nixos-anywhere";
7+
networking.hostName = lib.mkDefault "nixos-anywhere";
88
documentation.enable = false;
99
hardware.enableAllFirmware = false;
1010
networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs

0 commit comments

Comments
 (0)