nix-tf-schema
A small, self-contained library of pure Nix helpers that translate between
OpenTofu/Terraform provider schemas (tofu providers schema -json) and Nix
module types / tfvars JSON.
It has no domain logic -- nothing about any particular infrastructure, service, or deployment. Given a provider schema it can:
- convert Terraform types and attribute schemas to nixpkgs option types and
tfvarsvariableblocks; - walk a resource schema, with its nested blocks, to Nix options, Terraform
variableblocks and a resource body that refers to them; - build reference expressions and type the resource meta-arguments of Terraform, for the hand-written parts of a module;
- assemble a complete Terraform module as JSON; and
- extract provider schemas with
tofu providers schema, and write the schema of one provider to a file that a consumer can commit.
See Conversion functions for the full, doc-commented API, and Comparison with other Nix + Terraform projects for how this library relates to the rest of the ecosystem.
The specifications tell what the library must do.
Debugging
The schema walks tag every recursion step with builtins.addErrorContext, so a
failure names the resource, attribute and block it was walking. Every breadcrumb
is prefixed with [nix-tf-schema], so they can be grepped out of a trace:
nix ... --show-trace 2> >(grep "… \[nix-tf-schema\]")
Consuming it
default.nix is callPackage-shaped and exposes the helpers under lib, so a
consumer that already has its own nixpkgs passes it in and eval uses that
nixpkgs:
let
sources = import ./npins;
tfSchema = pkgs.callPackage "${sources.nix-tf-schema}" { };
in
tfSchema.lib.mkTfModuleJson { /* ... */ }
Pin it with npins:
npins add forgejo https://git.fediversity.eu fediversity nix-tf-schema --branch main