Home Explore Blog CI



nix

2nd chunk of `doc/manual/source/command-ref/nix-instantiate.md`
4da497c02d0e4c7f1080dd59cba49c9263abcefb9d8e6b340000000100000eb4
  > For human-readable output, `nix eval` (experimental) is more informative:
  >
  > ```console
  > $ nix-instantiate --eval --expr 'a: a'
  > <LAMBDA>
  > $ nix eval --expr 'a: a'
  > «lambda @ «string»:1:1»
  > ```
  >
  > For machine-readable output, the `--xml` option produces unambiguous
  > output:
  >
  > ```console
  > $ nix-instantiate --eval --xml --expr '{ foo = <CODE>; }'
  > <?xml version='1.0' encoding='utf-8'?>
  > <expr>
  >   <attrs>
  >     <attr column="3" line="1" name="foo">
  >       <unevaluated />
  >     </attr>
  >   </attrs>
  > </expr>
  > ```

- `--find-file`

  Look up the given files in Nix’s search path (as specified by the
  `NIX_PATH` environment variable). If found, print the corresponding
  absolute paths on standard output. For instance, if `NIX_PATH` is
  `nixpkgs=/home/alice/nixpkgs`, then `nix-instantiate --find-file
  nixpkgs/default.nix` will print `/home/alice/nixpkgs/default.nix`.

- `--strict`

  When used with `--eval`, recursively evaluate list elements and
  attributes. Normally, such sub-expressions are left unevaluated
  (since the Nix language is lazy).

  > **Warning**
  >
  > This option can cause non-termination, because lazy data
  > structures can be infinitely large.

- `--raw`

  When used with `--eval`, the evaluation result must be a string,
  which is printed verbatim, without quoting, escaping or trailing newline.

- `--json`

  When used with `--eval`, print the resulting value as an JSON
  representation of the abstract syntax tree rather than as a Nix expression.

- `--xml`

  When used with `--eval`, print the resulting value as an XML
  representation of the abstract syntax tree rather than as a Nix expression.
  The schema is the same as that used by the [`toXML`
  built-in](../language/builtins.md).

- `--read-write-mode`

  When used with `--eval`, perform evaluation in read/write mode so
  nix language features that require it will still work (at the cost
  of needing to do instantiation of every evaluated derivation). If
  this option is not enabled, there may be uninstantiated store paths
  in the final output.

{{#include ./opt-common.md}}

{{#include ./env-common.md}}

# Examples

Instantiate [store derivation]s from a Nix expression, and build them using `nix-store`:

```console
$ nix-instantiate test.nix (instantiate)
/nix/store/cigxbmvy6dzix98dxxh9b6shg7ar5bvs-perl-BerkeleyDB-0.26.drv

$ nix-store --realise $(nix-instantiate test.nix) (build)
...
/nix/store/qhqk4n8ci095g3sdp93x7rgwyh9rdvgk-perl-BerkeleyDB-0.26 (output path)

$ ls -l /nix/store/qhqk4n8ci095g3sdp93x7rgwyh9rdvgk-perl-BerkeleyDB-0.26
dr-xr-xr-x    2 eelco    users        4096 1970-01-01 01:00 lib
...
```

You can also give a Nix expression on the command line:

```console
$ nix-instantiate --expr 'with import <nixpkgs> { }; hello'
/nix/store/j8s4zyv75a724q38cb0r87rlczaiag4y-hello-2.8.drv
```

This is equivalent to:

```console
$ nix-instantiate '<nixpkgs>' --attr hello
```

Parsing and evaluating Nix expressions:

```console
$ nix-instantiate --parse --expr '1 + 2'
1 + 2
```

```console
$ nix-instantiate --eval --expr '1 + 2'
3
```

```console
$ nix-instantiate --eval --xml --expr '1 + 2'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <int value="3" />
</expr>
```

The difference between non-strict and strict evaluation:

```console
$ nix-instantiate --eval --xml --expr '{ x = {}; }'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <attrs>
    <attr column="3" line="1" name="x">
      <unevaluated />
    </attr>
  </attrs>
</expr>

$ nix-instantiate --eval --xml --strict --expr '{ x = {}; }'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <attrs>
    <attr column="3" line="1" name="x">
      <attrs>
      </attrs>
    </attr>
  </attrs>
</expr>
```

Title: nix-instantiate Options and Examples
Summary
This section describes various options for `nix-instantiate`, including `--find-file` to locate files in Nix's search path, `--strict` for recursive evaluation, `--raw`, `--json`, and `--xml` for output formatting, and `--read-write-mode` for enabling read/write evaluation. It provides examples of using `nix-instantiate` to instantiate and build store derivations, pass Nix expressions via the command line, parse and evaluate expressions, and demonstrate the difference between strict and non-strict evaluation.