API Reference
Functions for parsing, constructing, matching, and transforming Nix AST nodes.
Overview
The API is organized into three layers:
| Layer | Functions | Description |
|---|---|---|
| IFD bridge | parse, render, eval | Bridge to the Haskell parser/evaluator via Import From Derivation |
| Value conversion | toAST, fromAST | Pure Nix conversion between Nix values and AST nodes |
| AST tools | match, syntax, traversal | Pattern matching, constructors, and tree operations |
Pages
| Page | Description |
|---|---|
| Core Functions | parse, render, eval, toAST, fromAST |
| syntax: Constructors & Predicates | mk* builders and is* tag checks for all node types |
| match: Pattern Matching | Type-safe tag dispatch with match ast { ... } |
| traversal: Tree Operations | children, rebuild, transform, universe, contexts |
How IFD Works
parse, render, and eval use pkgs.runCommand to create a derivation that:
- Serializes the input (paths, ASTs) to a JSON file via
pkgs.writeText - Runs the
nix-astCLI binary inside the derivation, piping the JSON file to stdin - Captures stdout as the derivation output
- Reads back the output with
builtins.readFile+builtins.fromJSON
These functions have IFD (Import From Derivation) semantics: they work in nix build and nix eval but are unavailable in restricted evaluation modes like nix-instantiate --eval.
Quick Example
# Parse, transform, and render
asts = lib.parse pkgs [./file.nix];
transformed = lib.traversal.transform (node:
if lib.syntax.isConstant node && lib.syntax.isInt node.contents then
lib.syntax.mkConstant (lib.syntax.mkInt (node.contents.contents * 2))
else node
) (builtins.head asts);
outPaths = lib.render pkgs [transformed];
builtins.head outPaths
See also CLI Reference for the nix-ast eval / nix-ast parse / nix-ast render command-line tool.