Home Explore Blog CI



nix

4th chunk of `doc/manual/source/language/index.md`
e7340cfd6c0dbb558f00e652b34d934b1e649782d44e0b8c0000000100000c41
   `with builtins; head [ 1 2 3 ]`

  </td>
  <td>

   Add all attributes from the given set to the scope (evaluates to `1`).

   See [`with`-expressions](@docroot@/language/syntax.md#with-expressions) for details and shadowing caveats.

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

   `inherit pkgs src;`

  </td>
  <td>

   Adds the variables to the current scope (attribute set or `let` binding).
   Desugars to `pkgs = pkgs; src = src;`.
   See [Inheriting attributes](@docroot@/language/syntax.md#inheriting-attributes).

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

   `inherit (pkgs) lib stdenv;`

  </td>
  <td>

   Adds the attributes, from the attribute set in parentheses, to the current scope (attribute set or `let` binding).
   Desugars to `lib = pkgs.lib; stdenv = pkgs.stdenv;`.
   See [Inheriting attributes](@docroot@/language/syntax.md#inheriting-attributes).

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

   *[Functions](@docroot@/language/syntax.md#functions) (lambdas)*

  </td>
  <td>



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

   `x: x + 1`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) that expects an integer and returns it increased by 1.

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

   `x: y: x + y`

  </td>
  <td>

   Curried [function](@docroot@/language/syntax.md#functions), equivalent to `x: (y: x + y)`. Can be used like a function that takes two arguments and returns their sum.

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

   `(x: x + 1) 100`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) call (evaluates to 101)

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

   `let inc = x: x + 1; in inc (inc (inc 100))`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) bound to a variable and subsequently called by name (evaluates to 103)

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

   `{ x, y }: x + y`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) that expects a set with required attributes `x` and `y` and concatenates them

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

   `{ x, y ? "bar" }: x + y`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) that expects a set with required attribute `x` and optional `y`, using `"bar"` as default value for `y`

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

   `{ x, y, ... }: x + y`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) that expects a set with required attributes `x` and `y` and ignores any other attributes

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

   `{ x, y } @ args: x + y`

   `args @ { x, y }: x + y`

  </td>
  <td>

   A [function](@docroot@/language/syntax.md#functions) that expects a set with required attributes `x` and `y`, and binds the whole set to `args`

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

   *Built-in functions*

  </td>
  <td>



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

   `import ./foo.nix`

  </td>
  <td>

   Load and return Nix expression in given file.
   See [import](@docroot@/language/builtins.md#builtins-import).

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

   `map (x: x + x) [ 1 2 3 ]`

  </td>
  <td>

   Apply a function to every element of a list (evaluates to `[ 2 4 6 ]`).
   See [`map`](@docroot@/language/builtins.md#builtins-map).

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

Title: Nix Language Examples: Functions and Built-in Functions
Summary
This section provides examples of Nix functions (lambdas), including basic functions, curried functions, function calls, functions bound to variables, functions expecting attribute sets (with required, optional, and ignored attributes), and functions binding the whole set to an argument. It then introduces built-in functions, specifically the `import` function for loading Nix expressions from files and the `map` function for applying a function to elements of a list.