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

With (With Expression)

Definition

With { namespace :: Expr, body :: Expr }

Fields

FieldTypeDescription
namespaceExprThe set expression whose attributes are brought into scope
bodyExprBody expression evaluated with namespace attributes in scope

Description

With brings all attributes of the namespace set into the lexical scope of body. This allows referencing pkgs.hello as just hello within the body.

Nix Source ↔ AST

# Nix
with pkgs; hello

# AST
{
  "tag": "With",
  "namespace": { "tag": "Sym", "contents": "pkgs" },
  "body": { "tag": "Sym", "contents": "hello" }
}
  • Let: similar but with explicit bindings

Nix Library Access

syntax.mkWith (syntax.mkSym "pkgs") (syntax.mkSym "hello")