Home Explore Blog CI



nix

2nd chunk of `doc/manual/source/language/syntax.md`
cc5f5e7ca698270fb7dd4434d0e3e95499b3e2986b47dd510000000100000faa
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**
>
> This defines an attribute set with attributes named:
> - `x` with the value `123`, an integer
> - `text` with the value `"Hello"`, a string
> - `y` where the value is the result of applying the function `f` to the attribute set `{ bla = 456; }`
>
> ```nix
> {
>   x = 123;
>   text = "Hello";
>   y = f { bla = 456; };
> }
> ```

Attributes in nested attribute sets can be written using *attribute paths*.

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

An attribute path is a dot-separated list of [names][name].

> **Syntax**
>
> *attrpath* = *name* { `.` *name* }

<!-- -->

> **Example**
>
> ```nix
> { a.b.c = 1; a.b.d = 2; }
> ```
>
>     {
>       a = {
>         b = {
>           c = 1;
>           d = 2;
>         };
>       };
>     }

Attribute names can also be set implicitly by using the [`inherit` keyword](#inheriting-attributes).

> **Example**
>
> ```nix
> { inherit (builtins) true; }
> ```
>
>     { true = true; }

Attributes can be accessed with the [`.` operator](./operators.md#attribute-selection).

Example:

```nix
{ a = "Foo"; b = "Bar"; }.a
```

This evaluates to `"Foo"`.

It is possible to provide a default value in an attribute selection using the `or` keyword.

Example:

```nix
{ a = "Foo"; b = "Bar"; }.c or "Xyzzy"
```

```nix
{ a = "Foo"; b = "Bar"; }.c.d.e.f.g or "Xyzzy"
```

will both evaluate to `"Xyzzy"` because there is no `c` attribute in the set.

You can use arbitrary double-quoted strings as attribute names:

```nix
{ "$!@#?" = 123; }."$!@#?"
```

```nix
let bar = "bar"; in
{ "foo ${bar}" = 123; }."foo ${bar}"
```

Both will evaluate to `123`.

Attribute names support [string interpolation]:

```nix
let bar = "foo"; in
{ foo = 123; }.${bar}
```

```nix
let bar = "foo"; in
{ ${bar} = 123; }.foo
```

Both will evaluate to `123`.

In the special case where an attribute name inside of a set declaration
evaluates to `null` (which is normally an error, as `null` cannot be coerced to
a string), that attribute is simply not added to the set:

```nix
{ ${if foo then "bar" else null} = true; }
```

This will evaluate to `{}` if `foo` evaluates to `false`.

A set that has a [`__functor`]{#attr-__functor} attribute whose value is callable (i.e. is
itself a function or a set with a `__functor` attribute whose value is
callable) can be applied as if it were a function, with the set itself
passed in first , e.g.,

```nix
let add = { __functor = self: x: x + self.x; };
    inc = add // { x = 1; }; # inc is { x = 1; __functor = (...) }
in inc 1 # equivalent of `add.__functor add 1` i.e. `1 + self.x`
```

evaluates to `2`. This can be used to attach metadata to a function
without the caller needing to treat it specially, or to implement a form
of object-oriented programming, for example.

## Recursive sets

Recursive sets are like normal [attribute sets](./types.md#attribute-set), but the attributes can refer to each other.

> *rec-attrset* = `rec {` [ *name* `=` *expr* `;` `]`... `}`

Title: Attribute Sets in Nix: Structure, Access, and Recursive Sets
Summary
This section explains Nix attribute sets, which are collections of name-value pairs. It details the syntax for defining attribute sets, accessing attributes using the `.` operator, and providing default values with the `or` keyword. It also covers the use of arbitrary strings as attribute names, string interpolation, and the special case of `null` attribute names. The section further introduces recursive sets, where attributes can refer to each other, and sets with a `__functor` attribute, allowing them to be applied as functions.