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:

  • wrap Terraform attribute schemas into tfvars variable blocks;
  • convert Terraform types and attributes into nixpkgs module types;
  • turn a resource schema into Nix options and ${var.*} references;
  • build cross-resource reference expressions for the hand-written parts of a module (ref);
  • type Terraform's own resource meta-arguments (count, for_each, depends_on, provider, lifecycle);
  • assemble a complete Terraform module as JSON; and
  • extract provider schemas at build time via tofu providers schema (an IFD).

See Conversion functions for the full, doc-commented API.

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