|
| 1 | ++++ |
| 2 | +title = 'evalTerranixConfiguration' |
| 3 | +date = '2026-05-30' |
| 4 | +draft = false |
| 5 | +weight = 20 |
| 6 | +aliases = ['/eval-terranix-configuration.html'] |
| 7 | ++++ |
| 8 | + |
| 9 | +`lib.evalTerranixConfiguration` evaluates terranix modules and returns |
| 10 | +the Terraform configuration as a Nix attrset, without writing anything |
| 11 | +to the Nix store. |
| 12 | + |
| 13 | +```nix |
| 14 | +terranix.lib.evalTerranixConfiguration { |
| 15 | + system = "x86_64-linux"; # or set pkgs directly |
| 16 | + modules = [ ./config.nix ]; |
| 17 | + extraArgs = { }; # extra arguments passed to modules |
| 18 | + strip_nulls = true; # remove null values from the output |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +The return value is an attrset containing `config`, like so: |
| 23 | + |
| 24 | +```nix |
| 25 | +{ |
| 26 | + config = { |
| 27 | + resource = { ... }; |
| 28 | + provider = { ... }; |
| 29 | + # ... |
| 30 | + }; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Exploring in nix repl |
| 35 | + |
| 36 | +The primary use case is inspecting your Terraform configuration interactively. |
| 37 | + |
| 38 | +For example, with `nix repl` you can produce a terranix configuration like so: |
| 39 | + |
| 40 | +``` |
| 41 | +$ nix repl |
| 42 | +nix-repl> terranix = builtins.getFlake "github:terranix/terranix" |
| 43 | +
|
| 44 | +nix-repl> result = terranix.lib.evalTerranixConfiguration { |
| 45 | + system = "x86_64-linux"; |
| 46 | + modules = [{ |
| 47 | + resource.local_file.example = { |
| 48 | + content = "hello"; |
| 49 | + filename = "./example.txt"; |
| 50 | + }; |
| 51 | + }]; |
| 52 | + } |
| 53 | +
|
| 54 | +nix-repl> result.config.resource.local_file.example |
| 55 | +{ content = "hello"; filename = "./example.txt"; } |
| 56 | +``` |
| 57 | + |
| 58 | +You can also load modules from files: |
| 59 | + |
| 60 | +``` |
| 61 | +nix-repl> result = terranix.lib.evalTerranixConfiguration { |
| 62 | + system = "x86_64-linux"; |
| 63 | + modules = [ ./config.nix ]; |
| 64 | + } |
| 65 | +
|
| 66 | +nix-repl> builtins.attrNames result.config |
| 67 | +[ "provider" "resource" ] |
| 68 | +``` |
| 69 | + |
| 70 | +## Accessing config from `terranixConfiguration` |
| 71 | + |
| 72 | +`lib.terranixConfiguration` produces a `config.tf.json` store derivation. |
| 73 | +It now also carries the evaluated configuration as a passthru attribute, |
| 74 | +so you can access the attrset without a separate call: |
| 75 | + |
| 76 | +```nix |
| 77 | +let |
| 78 | + tfConfig = terranix.lib.terranixConfiguration { |
| 79 | + inherit system; |
| 80 | + modules = [ ./config.nix ]; |
| 81 | + }; |
| 82 | +in |
| 83 | +{ |
| 84 | + # tfConfig is the config.tf.json derivation |
| 85 | + # tfConfig.config is the Terraform attrset |
| 86 | + inherit (tfConfig) config; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +This is convenient when your flake already uses `terranixConfiguration` |
| 91 | +and you want to inspect or reuse the configuration in other Nix expressions. |
| 92 | + |
| 93 | +## Migrating from terranixConfigurationAst |
| 94 | + |
| 95 | +`lib.terranixConfigurationAst` is deprecated. Replace calls with `lib.evalTerranixConfiguration`: |
| 96 | + |
| 97 | +```nix |
| 98 | +# Old |
| 99 | +terranix.lib.terranixConfigurationAst { system = "x86_64-linux"; modules = [ ./config.nix ]; } |
| 100 | +
|
| 101 | +# New |
| 102 | +terranix.lib.evalTerranixConfiguration { system = "x86_64-linux"; modules = [ ./config.nix ]; } |
| 103 | +``` |
| 104 | + |
| 105 | +The return value is the same: `{ config = { ... }; }`. |
0 commit comments