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

Select (Attribute Selection)

Definition

Select { defaultValue :: Maybe Expr, expr :: Expr, selectPath :: AttrPath }

Fields

FieldTypeDescription
defaultValueMaybe ExprDefault value if attribute not found (Nothing = no default, throws error)
exprExprThe set expression to select from
selectPathAttrPathThe attribute path to select

Nix Source ↔ AST

Without Default (throws if missing)

# Nix
set.attr

# AST
{
  "tag": "Select",
  "defaultValue": null,
  "expr": { "tag": "Sym", "contents": "set" },
  "selectPath": [{ "tag": "StaticKey", "contents": "attr" }]
}

With Default (or)

# Nix
set.attr or "default"

# AST
{
  "tag": "Select",
  "defaultValue": { "tag": "Str", "contents": { "tag": "DoubleQuoted", "contents": [{ "tag": "Plain", "contents": "default" }] } },
  "expr": { "tag": "Sym", "contents": "set" },
  "selectPath": [{ "tag": "StaticKey", "contents": "attr" }]
}

Nested Path

# Nix
set.a.b.c or 0

# AST
{
  "tag": "Select",
  "defaultValue": { "tag": "Constant", "contents": { "tag": "Int", "contents": 0 } },
  "expr": { "tag": "Sym", "contents": "set" },
  "selectPath": [
    { "tag": "StaticKey", "contents": "a" },
    { "tag": "StaticKey", "contents": "b" },
    { "tag": "StaticKey", "contents": "c" }
  ]
}

Nix Library Access

syntax.mkSelect null (syntax.mkSym "set") [syntax.mkStaticKey "attr"]
syntax.mkSelect (syntax.mkStr (syntax.mkDoubleQuoted [syntax.mkPlain "default"])) (syntax.mkSym "set") [syntax.mkStaticKey "attr"]