Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Kiara Grouwstra 0694ec75a5
All checks were successful
check / treefmt (push) Successful in 13s
check / docs (push) Successful in 15s
check / nix-unit (push) Successful in 19s
publish-nix-tf-schema / publish-nix-tf-schema (push) Successful in 6m5s
publish the docs to nix-tf-schema.pages.fediversity.eu
The git-pages server of the forge now serves the sites below `pages.fediversity.eu`. The names below `pages.git.fediversity.eu` redirect there, but an upload to them fails.

Assisted-by: Claude:claude-fable-5-2
2026-11-13 16:26:51 +01:00
.forgejo/workflows publish the docs to nix-tf-schema.pages.fediversity.eu 2026-11-13 16:26:51 +01:00
md add Spec Kit, a constitution and the specifications of the current library (#13) 2026-10-02 09:09:45 +02:00
npins npins: update nix-docs-lib for the content width and table breaks (#25) 2026-11-11 16:58:37 +01:00
specs mkTfModuleJson: take the state backend as an optional parameter (breaking) (#21) 2026-10-02 21:44:05 +02:00
.gitignore add Spec Kit, a constitution and the specifications of the current library (#13) 2026-10-02 09:09:45 +02:00
conversion-test.nix mkTfModuleJson: take the state backend as an optional parameter (breaking) (#21) 2026-10-02 21:44:05 +02:00
conversion.nix mkTfModuleJson: take the state backend as an optional parameter (breaking) (#21) 2026-10-02 21:44:05 +02:00
default.nix npins: update nix-docs-lib, and link the docs site to this repository (#23) 2026-10-03 12:33:48 +02:00
flake.nix docs: remove consumer references and fix the doc-comment links (#14) 2026-10-01 19:46:05 +02:00
lib.nix docs: remove consumer references and fix the doc-comment links (#14) 2026-10-01 19:46:05 +02:00
LICENSE Add LICENSE (EUPL-1.2) to match the fediversity monorepo 2026-07-13 10:07:31 +02:00
README.md publish the docs to nix-tf-schema.pages.fediversity.eu 2026-11-13 16:26:51 +01:00
treefmt.nix Initial import: Terraform<->Nix schema conversion library 2026-07-12 13:30:11 +02:00

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 tfvars variable blocks;
  • walk a resource schema, with its nested blocks, to Nix options, Terraform variable blocks 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.

The function reference gives each helper with its inputs, type and an example.

Layout

  • conversion.nix -- the library ({ lib, pkgs, ... }: -> conversion attrset).
  • lib.nix -- the cast and evalOption helpers that the library uses.
  • conversion-test.nix -- the nix-unit suite, with one test- case for each tested behavior.
  • default.nix -- source of truth: builds a pkgs from the local nixpkgs pin, callPackages conversion.nix under lib, and exposes formatter + checks + packages.docs.
  • flake.nix -- thin wrapper adding the per-system layer over default.nix.
  • md/ -- the doc-site prose pages (the landing page, the ecosystem comparison, and the flake-attributes intro), rendered with the shared nix-docs-lib.

Consuming it

default.nix is callPackage-shaped and exposes the helpers under lib (so they do not clash with the flake-schema attrs), so a consumer that already has its own nixpkgs passes it in and eval uses that nixpkgs:

# in a repo that pins this via npins as `nix-tf-schema`
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

Documentation

The doc-commented API renders to a static site (via the shared nix-docs-lib ndg machinery), published on push to main at https://nix-tf-schema.pages.fediversity.eu/.

It has the landing page, a comparison with the other Nix + Terraform projects, the doc-commented conversion functions, and this repo's own flake attributes (generated from default.nix). To build it locally:

nix build .#packages.x86_64-linux.docs

Known limitations

Places where the translation is lossy. None of them is observable in what we generate today; each is recorded so the next person hitting one knows it is deliberate rather than an oversight.

  • null means unset, with no way to say "explicitly null". Optional attributes become nullOr T with default = null. Terraform needs no distinction here -- an argument set to null behaves exactly as if omitted, and the optional(...) type constraints we render carry no default, so the two coincide inside object types too. tofunix carries an unset sentinel because it bakes literals into the module JSON, where an unset option would emit a null key into blocks that reject one; our unset values are simply absent from the environment. It would start to matter if a code path rendered optional(T, default), or needed a key absent from a JSON payload rather than present-and-null.
  • int64 is typed as ints.s32. nixpkgs has no ints.s64, so the wider range is not enforced at eval time.
  • set is typed as listOf. Nix has no unordered collection type, so a set's ordering is not normalized and duplicates are not rejected.

Debugging

The schema walks tag every recursion step with builtins.addErrorContext, so a failure names the resource, attribute and block it was walking rather than surfacing as a bare attribute 'foo' missing. Every such breadcrumb is prefixed with [nix-tf-schema], which is what makes them greppable out of an otherwise enormous trace:

nix ... --show-trace 2> >(grep "… \[nix-tf-schema\]")

The contexts nest, so the surviving lines read back as the path into the schema, innermost first.

Specifications

specs/ contains the specifications and the principles for all changes. specs/README.md tells how to use them.

Development

# format
nix fmt

# run the unit tests (needs `nix` on PATH for the IFD test-extractProviderSchemas)
nix develop --command nix-unit ./conversion-test.nix

# sandbox-safe checks
nix build .#checks.x86_64-linux.treefmt -L
nix build .#checks.x86_64-linux.docs -L

CI builds those two checks as separate jobs rather than running nix flake check, so a formatting failure and a docs failure are told apart by job name.

The nix-unit suite is run by CI through the Forgejo workflow rather than as a nix flake check derivation, because test-extractProviderSchemas performs an import-from-derivation (tofu providers schema) that needs a recursive nix build the flake-check sandbox forbids.