Home Explore Blog CI



nix

1st chunk of `doc/manual/source/language/syntax.md`
8402d340872de2402b30b8f8f00dc77a6be2d68287281c100000000100000fac
# Language Constructs

This section covers syntax and semantics of the Nix language.

## Basic Literals

### String {#string-literal}

See [String literals](string-literals.md).

### Number {#number-literal}

<!-- TODO(@rhendric, #10970): split this into int and float -->

  Numbers, which can be *integers* (like `123`) or *floating point*
  (like `123.43` or `.27e13`).

  Integers in the Nix language are 64-bit [two's complement] signed integers, with a range of -9223372036854775808 to 9223372036854775807, inclusive.


  Note that negative numeric literals are actually parsed as unary negation of positive numeric literals.
  This means that the minimum integer `-9223372036854775808` cannot be written as-is as a literal, since the positive number `9223372036854775808` is one past the maximum range.

  See [arithmetic] and [comparison] operators for semantics.


### Path {#path-literal}

  *Paths* can be expressed by path literals such as `./builder.sh`.

  A path literal must contain at least one slash to be recognised as such.
  For instance, `builder.sh` is not a path:
  it's parsed as an expression that selects the attribute `sh` from the variable `builder`.

  Path literals are resolved relative to their [base directory](@docroot@/glossary.md#gloss-base-directory).
  Path literals may also refer to absolute paths by starting with a slash.

  > **Note**
  >
  > Absolute paths make expressions less portable.
  > In the case where a function translates a path literal into an absolute path string for a configuration file, it is recommended to write a string literal instead.
  > This avoids some confusion about whether files at that location will be used during evaluation.
  > It also avoids unintentional situations where some function might try to copy everything at the location into the store.

  If the first component of a path is a `~`, it is interpreted such that the rest of the path were relative to the user's home directory.
  For example, `~/foo` would be equivalent to `/home/edolstra/foo` for a user whose home directory is `/home/edolstra`.
  Path literals that start with `~` are not allowed in [pure](@docroot@/command-ref/conf-file.md#conf-pure-eval) evaluation.

  Path literals can also include [string interpolation], besides being [interpolated into other expressions].


  At least one slash (`/`) must appear *before* any interpolated expression for the result to be recognized as a path.

  `a.${foo}/b.${bar}` is a syntactically valid number division operation.
  `./a.${foo}/b.${bar}` is a path.

  [Lookup path](./constructs/lookup-path.md) literals such as `<nixpkgs>` also resolve to path values.

## List {#list-literal}

Lists are formed by enclosing a whitespace-separated list of values
between square brackets. For example,

```nix
[ 123 ./foo.nix "abc" (f { x = y; }) ]
```

defines a list of four elements, the last being the result of a call to
the function `f`. Note that function calls have to be enclosed in
parentheses. If they had been omitted, e.g.,

```nix
[ 123 ./foo.nix "abc" f { x = y; } ]
```

the result would be a list of five elements, the fourth one being a
function and the fifth being a set.

Note that lists are only lazy in values, and they are strict in length.

Elements in a list can be accessed using [`builtins.elemAt`](./builtins.md#builtins-elemAt).

## Attribute Set {#attrs-literal}

An attribute set is a collection of name-value-pairs called *attributes*.

Attribute sets are written enclosed in curly brackets (`{ }`).
Attribute names and attribute values are separated by an equal sign (`=`).
Each value can be an arbitrary expression, terminated by a semicolon (`;`)

An attribute name is a string without context, and is denoted by a [name] (an [identifier](./identifiers.md#identifiers) or [string literal](string-literals.md)).


> **Syntax**
>
> *attrset* → `{` { *name* `=` *expr* `;` } `}`

Attributes can appear in any order.
An attribute name may only occur once in each attribute set.

> **Example**

Title: Nix Language Constructs: Basic Literals, Lists, and Attribute Sets
Summary
This section details fundamental syntax and semantics of the Nix language, covering basic literals like strings, numbers (integers and floating points), and paths. It explains how path literals are resolved and the significance of slashes and the tilde (~) for home directories. It also describes lists, formed with square brackets, and attribute sets, collections of name-value pairs enclosed in curly brackets, with names and values separated by an equal sign and terminated by a semicolon.