Skip to content

Commit 80a2e7d

Browse files
Mic92mergify[bot]
authored andcommitted
terraform: document special_args and nixos-vars.json
1 parent 1a0b60a commit 80a2e7d

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

terraform/all-in-one.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ module "deploy" {
3333
# script is below
3434
script = "${path.module}/decrypt-zfs-key.sh"
3535
}]
36+
# Optional, arguments passed to special_args here will be available from a NixOS module in this example the `terraform` argument:
37+
# { terraform, ... }: {
38+
# networking.interfaces.enp0s3.ipv4.addresses = [{ address = terraform.ip; prefixLength = 24; }];
39+
# }
40+
# Note that this will means that your NixOS configuration will always depend on terraform!
41+
# Skip to `Pass data persistently to the NixOS` for an alternative approach
42+
#special_args = {
43+
# terraform = {
44+
# ip = "192.0.2.0"
45+
# }
46+
#}
3647
}
3748
```
3849

@@ -80,6 +91,87 @@ sops --extract '["zfs-key"]' --decrypt "$SCRIPT_DIR/secrets.yaml"
8091
- [nixos-wiki setup](https://github.com/NixOS/nixos-wiki-infra/blob/main/terraform/nixos-wiki/main.tf)
8192
for hetzner-cloud
8293

94+
## Pass data persistently to the NixOS
95+
96+
This guide outlines how to pass data from Terraform to NixOS by generating a
97+
file during Terraform execution and including it in your NixOS configuration.
98+
This approach works well if your Terraform and NixOS configurations are stored
99+
in the same Git repository.
100+
101+
### Why Use This Method?
102+
103+
This method provides a straightforward way to transfer values from Terraform to
104+
NixOS without relying on special_args.
105+
106+
- **Advantages**:
107+
- You can continue to use nix build or nixos-rebuild to evaluate your
108+
configuration without interruption. Simplifies configuration management by
109+
centralizing state in a single repository.
110+
- **Disadvantages**:
111+
- Deploying new machines requires tracking additional state. Every time
112+
Terraform updates the JSON file, you’ll need to commit these changes to your
113+
repository.
114+
115+
### Implementation
116+
117+
Add the following snippet to your Terraform configuration to create and manage a
118+
JSON file containing the necessary variables for NixOS. This file will be
119+
automatically added to your Git repository, ensuring the data persists.
120+
121+
Assuming you have your terraform and nixos configuration in the same git
122+
repository. You can use the following snippet to `git add` a file generated by
123+
`terraform` during execution to pass data from terraform to NixOS. These changes
124+
should be committed afterwards. This is an alternative over using
125+
`special_args`. Advantage: you can still use nix build or nixos-rebuild on your
126+
flake to evaluate your configuration. Disadvantage: Deploying new machines also
127+
means you need to track additional state and make additional commits whenever
128+
terraform updates the json file.
129+
130+
```hcl
131+
locals {
132+
nixos_vars_file = "nixos-vars.json" # Path to the JSON file containing NixOS variables
133+
nixos_vars = {
134+
ip = "192.0.2.0" # Replace with actual variables
135+
}
136+
}
137+
resource "local_file" "nixos_vars" {
138+
content = jsonencode(local.nixos_vars) # Converts variables to JSON
139+
filename = local.nixos_vars_file # Specifies the output file path
140+
file_permission = "600"
141+
142+
# Automatically adds the generated file to Git
143+
provisioner "local-exec" {
144+
interpreter = ["bash", "-c"]
145+
command = "git add -f '${local.nixos_vars_file}'"
146+
}
147+
}
148+
```
149+
150+
After applying the Terraform changes, ensure you commit the updated
151+
`nixos-vars.json` file to your Git repository:
152+
153+
```bash
154+
git commit -m "Update NixOS variables from Terraform"
155+
```
156+
157+
You can import this json file into your configuration like this:
158+
159+
```nix
160+
let
161+
nixosVars = builtins.fromJSON (builtins.readFile ./nixos-vars.json);
162+
in
163+
{
164+
# Example usage of imported variables
165+
networking.hostName = "example-machine";
166+
networking.interfaces.eth0.ipv4.addresses = [
167+
{
168+
address = nixosVars.ip; # Use the IP from nixos-vars.json
169+
prefixLength = 24;
170+
}
171+
];
172+
}
173+
```
174+
83175
<!-- BEGIN_TF_DOCS -->
84176

85177
## Requirements

0 commit comments

Comments
 (0)