AST Reference
Complete reference for every node type in the Nix AST, organized by type hierarchy as defined in Types.hs.
Quick Overview
The AST is a sum type with 19 expression constructors and supporting types for atoms, bindings, keys, parameters, strings, and antiquoted parts. Every node has a tag field (a string identifying the constructor) plus constructor-specific fields.
Node Types
| Type | Description |
|---|---|
| Expr | The main expression tree, with 19 constructors |
| Atom | Primitive constant values (Bool, Float, Int, Null, Uri) |
| VarName | Identifier name (simple string) |
| AttrPath | Non-empty list of KeyName for attribute paths |
| Binding | Left-hand side of attribute bindings (Inherit, NamedVar) |
| KeyName | Attribute path components (StaticKey, DynamicKey) |
| Operators | Binary and unary operators |
| Params | Function parameter definitions (Param, ParamSet) |
| String | String literal contents (DoubleQuoted, Indented) |
| Antiquoted | String parts with interpolation |
JSON Representation
Every node is serializable to/from JSON. The tag field identifies the constructor:
{ "tag": "Constant", "contents": { "tag": "Int", "contents": 42 } }
This is the format used by the CLI for nix-ast parse output and nix-ast render input.
Quick Start
# Parse Nix code into AST
asts = lib.parse pkgs [./example.nix];
ast = builtins.head asts;
# Inspect the expression type using match
lib.match ast {
Set = { recursive, bindings }: "attribute set";
List = { contents }: "list of ${toString (builtins.length contents)} items";
_ = _: "other expression";
};
# Transform all integer constants: double their value
transformed = lib.traversal.transform (node:
if node.tag == "Constant" && node.contents.tag == "Int"
then lib.syntax.mkConstant (lib.syntax.mkInt (node.contents.contents * 2))
else node
) ast;
# Render back to Nix code
outPaths = lib.render pkgs [transformed];
builtins.head outPaths