Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

TypeDescription
ExprThe main expression tree, with 19 constructors
AtomPrimitive constant values (Bool, Float, Int, Null, Uri)
VarNameIdentifier name (simple string)
AttrPathNon-empty list of KeyName for attribute paths
BindingLeft-hand side of attribute bindings (Inherit, NamedVar)
KeyNameAttribute path components (StaticKey, DynamicKey)
OperatorsBinary and unary operators
ParamsFunction parameter definitions (Param, ParamSet)
StringString literal contents (DoubleQuoted, Indented)
AntiquotedString 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