Nix builtins

Nix exposes a minimal set of unprefixed builtins to avoid a crowded namespace. These functions, and all other builtins, are also available under the builtins namespace.

Since the set builtins contains all the built-in functions and other values, it can be used with Nix operators like ? to provide graceful fallback settings for older versions of Nix.

Unprefixed

  • abort s - abort evaluation with message s
  • baseNameOf s - basename of string s
  • derivation attrs - Nix derivations
  • dirOf s - dirname of string s
  • fetchTarball url - fetchTarball
  • import path - Nix import keyword
  • map f list - apply f to each element of list
  • removeAttrs set list - Remove the attributes with keys in list from set
  • throw s - Throw an error message s. Usually works like abort, but won’t break nix-env
  • toString e - Convert the expression e to, where e is one of:
    • a string - no change
    • a path - quotes the path
    • a set containing a __toString = self: ... attribute
    • an integer
    • a list - joined with spaces
    • a boolean - "" for false, "1" for true
    • null - ""

Prefixed

Functions

  • functionArgs f - Return a set whose keys are the formal parameters of f and whose values are booleans indicating whether a default is set for each one
  • isFunction e - true if e is a function, otherwise false

Fetching

  • fetchurl url - Download the URL to the Nix store and return the path
  • fetchGit - fetchGit

Packaging

  • compareVersions s1 s2 - Compare two version strings. -1 if s1 < s2, 0 if s1 == s2, 1 if s1 > s2
  • filterSource e1 e2 - filterSource
  • hashFile type p - Return a base-16 representation of the cryptographic hash of the file at path p under algorithm type, one of md5 or sha1/256/512
  • parseDrvName s - split s into { name, version }
  • splitVersion s - split a version string into its components

Values

  • currentSystem - platform identifier for the system on which the expression is being evaluated
  • getEnv s - gets the environment variable s
  • typeOf e - return a string representing the type of e as in Nix values

Evaluation

  • deepSeq e1 e2 - like seq, but e1’s elements are evaluated recursively if it’s a list or set
  • seq e1 e2 - Evaluate e1, then evaluate and return e2; this ensures strict evaluation in the value of e1
  • trace e1 e2 - evaluate e1, print its abstract syntax to stderr, then return e2
  • tryEval e - return { success ? false, value ? false }, with sucess set true and value set to e if e evaluates successfully