LinuxCommandLibrary

nix-eval

TLDR

Evaluate Nix flake attributes in the current directory

$ nix eval .#[attributes]
copy

Evaluate a given Nix expression
$ nix eval --expr '[your_expression]'
copy

Evaluate a Nix expression from a specified file
$ nix eval --file [path/to/file]
copy

Print the store path of the specified package from nixpkgs
$ nix eval [nixpkgs#pkg] --raw
copy

Evaluate attributes from a flake directly from GitHub
$ nix eval [github:owner/repo#attributes]
copy

Evaluate a given lambda function passing the specified package as argument
$ nix eval [nixpkgs#pkg] --apply '[lambda_function]'
copy

SYNOPSIS

nix eval [option...] installable
nix eval [option...] --expr expression

PARAMETERS

--expr
    Evaluate the specified Nix expression provided as a string instead of an installable path.

--json
    Produce the output in JSON format, allowing for easy integration with tools like jq.

--raw
    Print the result as a raw string without any quoting or escaping characters.

--impure
    Allow the evaluation to access environment variables and files outside of the Nix store.

--apply
    Apply a Nix function to the result of the evaluation before printing.

--read-only
    Disallow any side effects, such as adding paths to the Nix store, during the evaluation process.

--write-to
    Write the evaluation result to the specified file instead of standard output.

DESCRIPTION

The nix eval command is a versatile utility used to parse and compute Nix expressions directly from the command line. It serves as a bridge between the functional Nix language and the shell, allowing users to inspect the values of variables, attributes, and complex logic without initiating a full build process.

When executed, the command evaluates the provided Nix code—which can be a simple string, a file, or a reference to a Nix Flake—and outputs the resulting value to the standard output. This is particularly useful for debugging Nix expressions, extracting configuration values (such as those in NixOS or Home Manager), and automating tasks that require logic defined within Nix files. Unlike legacy commands, nix eval is part of the modern, unified Nix CLI and offers advanced features like JSON output and raw string formatting, making it highly compatible with other data-processing tools. It operates strictly within the evaluation phase of the Nix lifecycle, focusing on logical computation rather than derivation realization.

CAVEATS

The command only performs evaluation; it does not build derivations or realize store paths. If an expression evaluates to a derivation (a .drv file), nix eval will return the path to that derivation rather than the build output. Additionally, when using Flakes, evaluation is pure by default, meaning it cannot access the system clock or arbitrary files unless --impure is explicitly passed.

USAGE WITH FLAKES

When working with Nix Flakes, you can evaluate specific outputs by using the syntax flake-url#output-path. For example, nix eval .#nixosConfigurations.myhost.config.networking.hostName allows for deep inspection of system configurations.

HANDLING COMPLEX TYPES

If an expression evaluates to a complex attribute set or list, the output will be formatted as a Nix literal. To handle these structures in scripts, it is highly recommended to use the --json flag to ensure data integrity.

HISTORY

Historically, evaluating Nix expressions required the use of nix-instantiate --eval. With the release of Nix 2.0 and the subsequent introduction of the unified nix command-line interface, nix eval was created to provide a more user-friendly and consistent experience. It has since become the standard tool for developers working with Nix Flakes and modern Nix configurations.

SEE ALSO

nix-instantiate(1), nix-build(1), nix-shell(1), jq(1)

Copied to clipboard