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

DynamicKey (Dynamic Attribute Key)

Definition

DynamicKey (Antiquoted String)

Fields

FieldTypeDescription
contentsAntiquoted StringA string that may contain antiquotations

Description

DynamicKey is used for attribute keys that are not static identifiers: either quoted strings like "foo bar" or antiquoted expressions like ${name}. In Nix, any key in quotes or with antiquotation is parsed as a DynamicKey.

The contents is an Antiquoted String, meaning it can be:

  • Plain string parts
  • Antiquoted expressions
  • Escaped newlines (in indented strings)

Nix Source ↔ AST

Quoted String Key

# Nix
{ "foo bar" = 1; }

# AST (key part)
{
  "tag": "DynamicKey",
  "contents": {
    "tag": "Plain",
    "contents": {
      "tag": "DoubleQuoted",
      "contents": [
        { "tag": "Plain", "contents": "foo bar" }
      ]
    }
  }
}

Antiquoted Key

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

# AST (key part)
{
  "tag": "DynamicKey",
  "contents": {
    "tag": "Antiquoted",
    "contents": { "tag": "Sym", "contents": "name" }
  }
}

Nix Library Access

syntax.mkDynamicKey (syntax.mkAntiquoted (syntax.mkSym "name"))
# Plain + String variant (rare): syntax.mkDynamicKey (syntax.mkPlain (syntax.mkDoubleQuoted [...]))