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

NamedVar (Named Variable Binding)

Definition

NamedVar { attrPath :: AttrPath, value :: Expr }

Fields

FieldTypeDescription
attrPathAttrPathAttribute path (non-empty list of KeyName)
valueExprThe bound expression

Nix Source ↔ AST

Simple Binding

# Nix
x = 1;

# AST
{
  "tag": "NamedVar",
  "attrPath": [{ "tag": "StaticKey", "contents": "x" }],
  "value": { "tag": "Constant", "contents": { "tag": "Int", "contents": 1 } }
}

Nested Path Binding

# Nix
a.b.c = 1;

# AST
{
  "tag": "NamedVar",
  "attrPath": [
    { "tag": "StaticKey", "contents": "a" },
    { "tag": "StaticKey", "contents": "b" },
    { "tag": "StaticKey", "contents": "c" }
  ],
  "value": { "tag": "Constant", "contents": { "tag": "Int", "contents": 1 } }
}

Dynamic Key Binding

# Nix
let name = "foo"; in { ${name} = 1; }

# AST
{
  "tag": "NamedVar",
  "attrPath": [
    { "tag": "DynamicKey", "contents": {
        "tag": "Antiquoted",
        "contents": { "tag": "Sym", "contents": "name" }
      }
    }
  ],
  "value": { "tag": "Constant", "contents": { "tag": "Int", "contents": 1 } }
}

Nix Library Access

syntax.mkNamedVar [syntax.mkStaticKey "x"] (syntax.mkInt 1)
syntax.mkNamedVar [syntax.mkStaticKey "a", syntax.mkStaticKey "b"] (syntax.mkInt 1)