Writing Terraform configurations using Nix, validated by the NixOS/nixpkgs module system. Inspired by Terranix but stricter and mostly for use with OpenTofu.
Tofunix bridges the Nix and Terraform/Opentofu ecosystems by allowing you to define your infrastructure as Nix expressions (everything, not just NixOS and friends). It provides:
- Type-safe resource definitions & correctness using Nix's module system & generators
- Reproducible environments with locked dependencies
- Developer Experience: Nix is arguably better and more flexible than HCL
Create a my-infra.nix file:
{ref, ...}: {
variable."bunny_api_key" = {
type = "string";
};
provider.bunnynet."default" = {
api_key = ref.var.bunny_api_key;
};
}{
inputs.tofunix.url = "gitlab:TECHNOFAB/tofunix?dir=lib";
# perSystem
# tofunix-lib = inputs.tofunix.lib { inherit pkgs lib; };
packages.tofunix = tofunix-lib.mkCliAio {
plugins = [
(tofunix-lib.mkOpentofuProvider {
owner = "bunnyway";
repo = "bunnynet";
version = "0.7.0";
hash = "sha256-GvgAD+E/3potxlZJ3QF3UKB0r4I7lU/NGoV+/8R7RuU=";
})
];
moduleConfig = ./my-infra.nix; # nix module, so either a path, an attrset, a function etc.
};
}nix run .#tofunix -- apply # , validate, etc.See the docs.