- Nix 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
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 |
||
| .forgejo/workflows | ||
| md | ||
| npins | ||
| specs | ||
| .gitignore | ||
| conversion-test.nix | ||
| conversion.nix | ||
| default.nix | ||
| flake.nix | ||
| lib.nix | ||
| LICENSE | ||
| README.md | ||
| treefmt.nix | ||
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.
The function reference gives each helper with its inputs, type and an example.
Layout
conversion.nix-- the library ({ lib, pkgs, ... }:-> conversion attrset).lib.nix-- thecastandevalOptionhelpers that the library uses.conversion-test.nix-- thenix-unitsuite, with onetest-case for each tested behavior.default.nix-- source of truth: builds apkgsfrom the localnixpkgspin,callPackagesconversion.nixunderlib, and exposesformatter+checks+packages.docs.flake.nix-- thin wrapper adding the per-system layer overdefault.nix.md/-- the doc-site prose pages (the landing page, the ecosystem comparison, and the flake-attributes intro), rendered with the sharednix-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.
nullmeans unset, with no way to say "explicitly null". Optional attributes becomenullOr Twithdefault = null. Terraform needs no distinction here -- an argument set tonullbehaves exactly as if omitted, and theoptional(...)type constraints we render carry no default, so the two coincide inside object types too. tofunix carries anunsetsentinel because it bakes literals into the module JSON, where an unset option would emit anullkey into blocks that reject one; our unset values are simply absent from the environment. It would start to matter if a code path renderedoptional(T, default), or needed a key absent from a JSON payload rather than present-and-null.int64is typed asints.s32. nixpkgs has noints.s64, so the wider range is not enforced at eval time.setis typed aslistOf. 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.