HORIZON HASKELLDocslts/ghc-10.0.xc62e4de2026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · c62e4de · 2026-10-19

Modulehnix-0.17.0Haskell2010

Nix.Expr.Types

The Nix expression type and supporting types.

Brief introduction of the Nix expression language.

This module is a beginning of a deep embedding (term) of a Nix language into Haskell. Brief on shallow & deep embedding.

(additiona info for dev): Big use of TemplateHaskell in the module requires proper (top-down) organization of declarations.

  • 19 types
  • 1 class
  • 57 values
  • Packagehnix-0.17.0
  • Exports77
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTypes.hs
datadata Antiquoted v r
#

Antiquoted represents an expression that is either antiquoted (surrounded by ${...}) or plain (not antiquoted).

Constructors

Instances33Eq2, Ord2, Read2, Show2, Hashable2, FromJSON2, …
datadata Binding r
#

A single line of the bindings section of a let expression or of a set.

Constructors

  • NamedVar !(NAttrPath r) !r !NSourcePos

    An explicit naming.

    NamedVar (StaticKey "x" :| [StaticKey "y"]) z NSourcePos{}  ~  x.y = z;
  • Inherit !(Maybe r) ![VarName] !NSourcePos

    Inheriting an attribute (binding) into the attribute set from the other scope (attribute set). No denoted scope means to inherit from the closest outside scope.

    Hask

    Nix

    pseudocode

    Inherit Nothing [StaticKey "a"] NSourcePos{}

    inherit a;

    a = outside.a;

    Inherit (pure x) [StaticKey "a"] NSourcePos{}

    inherit (x) a;

    a = x.a;

    Inherit (pure x) [StaticKey "a", StaticKey "b"] NSourcePos{}

    inherit (x) a b;

    a = x.a;

    b = x.b;

    (2021-07-07 use details): Inherits the position of the first name through unsafeGetAttrPos. The position of the scope inherited from else - the position of the first member of the binds list.

Instances22Functor, Foldable, Traversable, Eq1, Ord1, Show1, …
typetype NAttrPath r = NonEmpty (NKeyName r)
#

A selector (for example in a let or an attribute set) is made up of strung-together key names.

StaticKey "x" :| [DynamicKey (Antiquoted y)]  ~  x.${y}
datadata NBinaryOp
#

Binary operators expressible in the nix language.

Constructors

  • NEq

    Equality (==)

  • NNEq

    Inequality (!=)

  • NLt

    Less than (<)

  • NLte

    Less than or equal (<=)

  • NGt

    Greater than (>)

  • NGte

    Greater than or equal (>=)

  • NAnd

    Logical and (&&)

  • NOr

    Logical or (||)

  • NImpl

    Logical implication (->)

  • NUpdate

    Get the left attr set, extend it with the right one & override equal keys (//)

  • NPlus

    Addition (+)

  • NMinus

    Subtraction (-)

  • NMult

    Multiplication (*)

  • NDiv

    Division (/)

  • NConcat

    List concatenation (++)

Instances16Bounded, Enum, Eq, Data, Ord, Read, …
typetype NExpr = Fix NExprF
#

The monomorphic expression type is a fixed point of the polymorphic one.

datadata NExprF r
#

The main Nix expression type. As it is polimorphic, has a functor, which allows to traverse expressions and map functions over them. The actual NExpr type is a fixed point of this functor, defined below.

Constructors

  • NConstant !NAtom

    Constants: ints, floats, bools, URIs, and null.

  • NStr !(NString r)

    A string, with interpolated expressions.

  • NSym !VarName

    A variable. For example, in the expression f a, f is represented as NSym "f" and a as NSym "a".

    NSym "x"                                    ~  x
  • NList ![r]

    A list literal.

    NList [x,y]                                 ~  [ x y ]
  • NSet !Recursivity ![Binding r]

    An attribute set literal

    NSet Recursive    [NamedVar x y _]         ~  rec { x = y; }
    NSet NonRecursive [Inherit Nothing [x] _]  ~  { inherit x; }
  • NLiteralPath !Path

    A path expression, which is evaluated to a store path. The path here can be relative, in which case it's evaluated relative to the file in which it appears.

    NLiteralPath "/x"                           ~  /x
    NLiteralPath "x/y"                          ~  x/y
  • NEnvPath !Path

    A path which refers to something in the Nix search path (the NIX_PATH environment variable. For example, nixpkgs/pkgs.

    NEnvPath "x"                                ~  <x>
  • NApp !r !r

    Functional application (aka F.A., apply a function to an argument).

    NApp f x  ~  f x
  • NUnary !NUnaryOp !r

    Application of a unary operator to an expression.

    NUnary NNeg x                               ~  - x
    NUnary NNot x                               ~  ! x
  • NBinary !NBinaryOp !r !r

    Application of a binary operator to two expressions.

    NBinary NPlus x y                           ~  x + y
    NBinary NApp  f x                           ~  f x
  • NSelect !(Maybe r) !r !(NAttrPath r)

    Dot-reference into an attribute set, optionally providing an alternative if the key doesn't exist.

    NSelect Nothing  s (x :| [])                ~  s.x
    NSelect (pure y) s (x :| [])                ~  s.x or y
  • NHasAttr !r !(NAttrPath r)

    Ask if a set contains a given attribute path.

    NHasAttr s (x :| [])                        ~  s ? x
  • NAbs !(Params r) !r

    A function literal (lambda abstraction).

    NAbs (Param "x") y                          ~  x: y
  • NLet ![Binding r] !r

    Evaluate the second argument after introducing the bindings.

    NLet []                    x                ~  let in x
    NLet [NamedVar x y _]      z                ~  let x = y; in z
    NLet [Inherit Nothing x _] y                ~  let inherit x; in y
  • NIf !r !r !r

    If-then-else statement.

    NIf x y z                                   ~  if x then y else z
  • NWith !r !r

    Evaluate an attribute set, bring its bindings into scope, and evaluate the second argument.

    NWith x y                                   ~  with x; y
  • NAssert !r !r

    Checks that the first argument is a predicate that is true before evaluating the second argument.

    NAssert x y                                 ~  assert x; y
  • NSynHole !VarName

    Syntactic hole.

    See https://github.com/haskell-nix/hnix/issues/197 for context.

    NSynHole "x"                                ~  ^x
Instances36Functor, IsString, Foldable, Traversable, Eq1, Ord1, …
datadata NKeyName r
#

A KeyName is something that can appear on the left side of an equals sign. For example, a is a KeyName in { a = 3; }, let a = 3; in ..., {}.a or {} ? a.

Nix supports both static keynames (just an identifier) and dynamic identifiers. Dynamic identifiers can be either a string (e.g.: { "a" = 3; }) or an antiquotation (e.g.: let a = "example"; in { ${a} = 3; }.example).

Note: There are some places where a dynamic keyname is not allowed. In particular, those include:

  • The RHS of a binding inside let: let ${"a"} = 3; in ... produces a syntax fail.

  • The attribute names of an inherit: inherit ${"a"}; is forbidden.

Note: In Nix, a simple string without antiquotes such as "foo" is allowed even if the context requires a static keyname, but the parser still considers it a DynamicKey for simplicity.

Constructors

  • DynamicKey !(Antiquoted (NString r) r)
    DynamicKey (Plain (DoubleQuoted [Plain "x"]))     ~  "x"
    DynamicKey (Antiquoted x)                         ~  ${x}
    DynamicKey (Plain (DoubleQuoted [Antiquoted x]))  ~  "${x}"
  • StaticKey !VarName
    StaticKey "x"                                     ~  x
Instances22Functor, Foldable, Traversable, Eq1, Ord1, Show1, …
newtypenewtype NPos
#

Constructors

Instances14Eq, Data, Ord, Read, Show, Generic, …
datadata NSourcePos
#

Represents source positions. Source line & column positions change intensively during parsing, so they are declared strict to avoid memory leaks.

The data type is a reimplementation of Text.Megaparsec.Pos SourcePos.

Constructors

Instances22Eq, Data, Ord, Read, Show, Generic, …
datadata NString r
#

An NString is a list of things that are either a plain string or an antiquoted expression. After the antiquotes have been evaluated, the final string is constructed by concatenating all the parts.

Constructors

  • DoubleQuoted ![Antiquoted Text r]

    Strings wrapped with double-quotes (") can contain literal newline characters, but the newlines are preserved and no indentation is stripped.

    DoubleQuoted [Plain "x",Antiquoted y]   ~  "x${y}"
  • Indented !Int ![Antiquoted Text r]

    Strings wrapped with two single quotes ('') can contain newlines, and their indentation will be stripped, but the amount stripped is remembered.

    Indented 1 [Plain "x"]                  ~  '' x''
    
    Indented 0 [EscapedNewline]             ~  ''''\n''
    
    Indented 0 [Plain "x\n ",Antiquoted y]  ~  ''
                                               x
                                                ${y}''
Instances27Functor, Foldable, Traversable, Eq1, Ord1, Read1, …
datadata NUnaryOp
#

There are two unary operations: logical not and integer negation.

Constructors

Instances16Bounded, Enum, Eq, Data, Ord, Read, …
datadata Params r
#

Params represents all the ways the formal parameters to a function can be represented.

Constructors

  • Param !VarName

    For functions with a single named argument, such as x: x + 1.

    Param "x"                                  ~  x
  • ParamSet !(Maybe VarName) !Variadic !(ParamSet r)

    Explicit parameters (argument must be a set). Might specify a name to bind to the set in the function body. The bool indicates whether it is variadic or not.

    ParamSet  Nothing   False [("x",Nothing)]  ~  { x }
    ParamSet (pure "s") True  [("x", pure y)]  ~  s@{ x ? y, ... }
Instances27Functor, Foldable, Traversable, Eq1, Ord1, Read1, …
datadata Recursivity
#

Distinguishes between recursive and non-recursive. Mainly for attribute sets.

Constructors

Instances17Bounded, Enum, Eq, Data, Ord, Read, …
newtypenewtype VarName
#

Constructors

Instances28Eq, Data, Ord, Read, Show, IsString, …
datadata Variadic
#
Instances15Eq, Data, Ord, Read, Show, Generic, …
datadata SourcePos
#

The data type SourcePos represents source positions. It contains the name of the source file, a line number, and a column number. Source line and column positions change intensively during parsing, so we need to make them strict to avoid memory leaks.

Constructors

Instances8Eq, Data, Ord, Read, Show, Generic, …