Terrafix: Generate Terraform files from DRY Nix expressions

Hi there! I made a Nix eDSL which can be translated into HCL, a language used by Terraform. In other words, we can use the power of Nix to generate Terraform configurations. Here’s the project’s repository. Comments, issues and PRs are welcome! :grinning:

5 Likes

I’ve been using terranix, so I’m curious. How does terrafix differ and/or improve on what terranix has to offer? Thanks!

Hi @adamcstephens! Thank you for the question. In fact, I learned about terranix after I had implemented terrafix. I wrote a comparison section. Copy-pasted:

  1. terrafix is an eDSL and it doesn’t suggest any new infrastructure or mechanisms. It is a means to produce Terraform code (.tf, .tfvars) from Nix code. On the other hand, terranix suggests a module system, which is different from the Terraform’s one, documentation generation, and it compiles to .json.

  2. In author’s opinion, for debugging, finding the mapping between .tf files and .nix files is easier than between .json and .nix. Also, one may utilize Terraform’s Language Server to find the errors in the generated code. To add, terrafix looks pretty similar to HCL, so there is a highly error-prone script to convert the existing Terraform files into .nix. It worked for simple examples though.

  3. Currently, terrafix has similar syntactic sugar, and, hopefully, the same compile-time safety due to accessors. In terrafix, it’s possible to use an accessor generated from previous blocks + add the missing fields:

  image = config.resource.hcloud_server.myserver.image "id";

This will look like image = config.resource.hcloud_server.myserver.image.id in the Terraform code.
Additionally, there will be a Nix compile time error if such an accessor is missing. Please, search the word accessor on the README page of terrafix to see more examples.

In terranix, one may write

  image = config.resource.hcloud_server.myserver.image;

However, the author is not sure if in .json it won’t become a string stored in config.resource.hcloud_server.myserver.image.

  1. terrafix can (naively) render all Terraform’s standard functions. There seems to be no such functionality in terranix

  2. terrafix is an experimental language, so it has a lot of limitations. E.g. modules like dockerMain can only be mixed into an argument of mkBlocks_ of another module to make their variables available inside. See more limitations.

1 Like