Home Explore Blog Models CI



nix

1st chunk of `doc/manual/source/language/string-interpolation.md`
e6b686e27a43290864a9e6d2460cce725bd8d902fe62fa7100000001000009e2
# String interpolation

String interpolation is a language feature where a [string], [path], or [attribute name][attribute set] can contain expressions enclosed in `${ }` (dollar-sign with curly brackets).

Such a construct is called *interpolated string*, and the expression inside is an [interpolated expression](#interpolated-expression).


> **Syntax**
>
> *interpolation_element* → `${` *expression* `}`

## Examples

### String

Rather than writing

```nix
"--with-freetype2-library=" + freetype + "/lib"
```

(where `freetype` is a [derivation expression]), you can instead write


```nix
"--with-freetype2-library=${freetype}/lib"
```

The latter is automatically translated to the former.

A more complicated example (from the Nix expression for [Qt](http://www.trolltech.com/products/qt)):

```nix
configureFlags = "
  -system-zlib -system-libpng -system-libjpeg
  ${if openglSupport then "-dlopen-opengl
    -L${mesa}/lib -I${mesa}/include
    -L${libXmu}/lib -I${libXmu}/include" else ""}
  ${if threadSupport then "-thread" else "-no-thread"}
";
```

Note that Nix expressions and strings can be arbitrarily nested;
in this case the outer string contains various interpolated expressions that themselves contain strings (e.g., `"-thread"`), some of which in turn contain interpolated expressions (e.g., `${mesa}`).

To write a literal `${` in an regular string, escape it with a backslash (`\`).

> **Example**
>
> ```nix
> "echo \${PATH}"
> ```
>
>     "echo ${PATH}"

To write a literal `${` in an indented string, escape it with two single quotes (`''`).

> **Example**
>
> ```nix
> ''
>   echo ''${PATH}
> ''
> ```
>
>     "echo ${PATH}\n"

`$${` can be written literally in any string.

> **Example**
>
> In Make, `$` in file names or recipes is represented as `$$`, see [GNU `make`: Basics of Variable Reference](https://www.gnu.org/software/make/manual/html_node/Reference.html#Basics-of-Variable-References).
> This can be expressed directly in the Nix language strings:
>
> ```nix
> ''
>   MAKEVAR = Hello
>   all:
>   	@export BASHVAR=world; echo $(MAKEVAR) $${BASHVAR}
> ''
> ```
>
>     "MAKEVAR = Hello\nall:\n\t@export BASHVAR=world; echo $(MAKEVAR) $\${BASHVAR}\n"

See the [documentation on strings][string] for details.

### Path

Rather than writing

```nix
./. + "/" + foo + "-" + bar + ".nix"
```

or

```nix
./. + "/${foo}-${bar}.nix"
```

you can instead write

```nix
./${foo}-${bar}.nix
```

### Attribute name

<!--
FIXME: these examples are redundant with the main page on attribute sets.

Title: String Interpolation
Summary
String interpolation is a language feature that allows embedding expressions within strings, paths, or attribute names using the `${expression}` syntax. This construct, called an *interpolated string*, simplifies complex string concatenation and improves readability. Examples demonstrate its use in constructing build flags and file paths, including nested interpolations where expressions themselves can contain strings with further interpolations. The document also details how to escape the `${` characters to be treated literally in different string contexts: `\${` for regular strings, `''${` for indented strings, and `$${` for a literal `$` followed by `{`.