Home Explore Blog Models CI



nix

1st chunk of `doc/manual/source/language/index.md`
06faafd69bb30a8661ecc18712b911a8a8f217cbd2e905b30000000100000fa5
# Nix Language

The Nix language is designed for conveniently creating and composing [derivations](@docroot@/glossary.md#gloss-derivation) – precise descriptions of how contents of existing files are used to derive new files.

> **Tip**
>
> These pages are written as a reference.
> If you are learning Nix, nix.dev has a good [introduction to the Nix language](https://nix.dev/tutorials/nix-language).

The language is:

- *domain-specific*

  The Nix language is purpose-built for working with text files.
  Its most characteristic features are:

  - [File system path primitives](@docroot@/language/types.md#type-path), for accessing source files
  - [Indented strings](@docroot@/language/string-literals.md) and [string interpolation](@docroot@/language/string-interpolation.md), for creating file contents
  - [Strings with contexts](@docroot@/language/string-context.md), for transparently linking files

  It comes with [built-in functions](@docroot@/language/builtins.md) to integrate with the [Nix store](@docroot@/store/index.md), which manages files and enables [realising](@docroot@/glossary.md#gloss-realise) derivations declared in the Nix language.

- *declarative*

  There is no notion of executing sequential steps.
  Dependencies between operations are established only through data.

- *pure*

  Values cannot change during computation.
  Functions always produce the same output if their input does not change.

- *functional*

  Functions are like any other value.
  Functions can be assigned to names, taken as arguments, or returned by functions.

- *lazy*

  Values are only computed when they are needed.

- *dynamically typed*

  Type errors are only detected when expressions are evaluated.

# Overview

This is an incomplete overview of language features, by example.

<table>
 <tr>
  <th>
   Example
  </th>
  <th>
   Description
  </th>
 </tr>
 <tr>
  <td>


   *Basic values ([primitives](@docroot@/language/types.md#primitives))*


  </td>
  <td>



  </td>
 </tr>
 <tr>
  <td>

   `"hello world"`

  </td>
  <td>

   A [string](@docroot@/language/types.md#type-string)

  </td>
 </tr>
 <tr>
  <td>

   ```
   ''
     multi
      line
       string
   ''
   ```

  </td>
  <td>

   <!-- FIXME: using two no-break spaces, because apparently mdBook swallows the second regular space! -->
   A multi-line string. Strips common prefixed whitespace. Evaluates to `"multi\n line\n  string"`.

  </td>
 </tr>
 <tr>
  <td>

   `# Explanation`

  </td>
  <td>

   A [comment](@docroot@/language/syntax.md#comments).

  </td>
 </tr>
 <tr>
  <td>

   `"hello ${ { a = "world"; }.a }"`

   `"1 2 ${toString 3}"`

   `"${pkgs.bash}/bin/sh"`

  </td>
  <td>

   [String interpolation](@docroot@/language/string-interpolation.md) (expands to `"hello world"`, `"1 2 3"`, `"/nix/store/<hash>-bash-<version>/bin/sh"`)

  </td>
 </tr>
 <tr>
  <td>

   `true`, `false`

  </td>
  <td>

   [Booleans](@docroot@/language/types.md#type-boolean)

  </td>
 </tr>
 <tr>
  <td>

   `null`

  </td>
  <td>

   [Null](@docroot@/language/types.md#type-null) value

  </td>
 </tr>
 <tr>
  <td>

   `123`

  </td>
  <td>

   An [integer](@docroot@/language/types.md#type-int)

  </td>
 </tr>
 <tr>
  <td>

   `3.141`

  </td>
  <td>

   A [floating point number](@docroot@/language/types.md#type-float)

  </td>
 </tr>
 <tr>
  <td>

   `/etc`

  </td>
  <td>

   An absolute [path](@docroot@/language/types.md#type-path)

  </td>
 </tr>
 <tr>
  <td>

   `./foo.png`

  </td>
  <td>

   A [path](@docroot@/language/types.md#type-path) relative to the file containing this Nix expression

  </td>
 </tr>
 <tr>
  <td>

   `~/.config`

  </td>
  <td>

   A home [path](@docroot@/language/types.md#type-path). Evaluates to the `"<user's home directory>/.config"`.

  </td>
 </tr>
 <tr>
  <td>

   `<nixpkgs>`

  </td>
  <td>

   A [lookup path](@docroot@/language/constructs/lookup-path.md) for Nix files. Value determined by [`$NIX_PATH` environment variable](../command-ref/env-common.md#env-NIX_PATH).

Title: Nix Language: Introduction and Basic Syntax
Summary
The Nix language is a domain-specific, declarative, pure, functional, lazy, and dynamically typed language primarily designed for creating and composing software derivations. Its key features include primitives for file system paths, indented strings, and string interpolation to facilitate file content management and integration with the Nix store. The provided text also offers an overview of basic Nix language elements, showcasing examples of different string types (single, multi-line, interpolated), comments, booleans, null, integers, floating-point numbers, and various forms of paths (absolute, relative, home, and lookup).