LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

nix-eval

evaluates Nix expressions

TLDR

Evaluate an expression
$ nix eval --expr "[1 + 1]"
copy
Evaluate a flake attribute
$ nix eval [.#packages.x86_64-linux.default.name]
copy
Get a package version from nixpkgs
$ nix eval --raw nixpkgs#[hello].version
copy
Apply a function to the result
$ nix eval --apply builtins.attrNames --expr "{a=1; b=2;}"
copy
Output as JSON
$ nix eval --json --expr "[{a = 1;}]"
copy
Evaluate a file
$ nix eval -f [file.nix]
copy
Evaluate an attribute from a file
$ nix eval -f [default.nix] [attribute]
copy
Print a string without quotes
$ nix eval --raw --expr "\"hello\""
copy
Read a NixOS configuration option from a flake
$ nix eval .#nixosConfigurations.[hostname].config.[networking.hostName]
copy

SYNOPSIS

nix eval [options] [installable]

DESCRIPTION

nix eval evaluates the given Nix expression or installable and prints the result on standard output. Nested attribute values and list items are evaluated too.By default the result is printed as a Nix expression; --json and --raw give output suitable for scripts. It is the new-CLI replacement for nix-instantiate --eval.

PARAMETERS

INSTALLABLE

Flake output attribute, or attribute path when used with --expr or --file.
--expr EXPR
Interpret installables as attribute paths relative to this Nix expression.
-f, --file FILE
Interpret installables as attribute paths relative to the expression in FILE (- reads stdin). Implies --impure.
--json
Output as JSON. Fails if the result contains functions.
--raw
Print strings without quotes or escaping. The result must be a string.
--apply FUNC
Apply function to result.
--write-to PATH
Write a string, or a nested attribute set of strings, as files under PATH.
--read-only
Do not instantiate evaluated derivations. Faster, but can fail when store paths of derivations are accessed.
--impure
Allow access to mutable paths, environment variables and the current time.
--arg NAME EXPR, --argstr NAME STRING
Pass arguments to Nix functions.
--debugger
Start an interactive debugger if evaluation fails.
--help
Display help information.

INSTALL

sudo apt install nix-bin
copy
sudo dnf install nix
copy
sudo pacman -S nix
copy
sudo apk add nix
copy
sudo zypper install nix
copy
nix profile install nixpkgs#nix
copy

CAVEATS

Part of the experimental new Nix CLI: requires the nix-command experimental feature, plus flakes for flake references. Evaluation is pure by default, so builtins.getEnv, builtins.currentTime and paths outside a flake need --impure.

HISTORY

nix eval appeared with the new nix command in Nix 2.0 (2018) and was reworked for flakes and installables in Nix 2.4 (2021).

SEE ALSO

RESOURCES

Copied to clipboard
Kai