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