LinuxCommandLibrary

nix3-repl

Evaluate Nix expressions interactively

TLDR

Start an interactive environment for evaluating Nix expressions

$ nix repl
copy

Load all packages from a flake (e.g. nixpkgs) into scope
$ :lf [nixpkgs]
copy

Build a package from an expression
$ :b [expression]
copy

Start a shell with package from the expression available
$ :u [expression]
copy

Start a shell with dependencies of the package from the expression available
$ :s [expression]
copy

SYNOPSIS

nix3 repl [options...] [path]

PARAMETERS

--file
    Load a Nix expression from path into the REPL environment.

--expr
    Evaluate expr directly before entering the REPL.

--strict
    Prohibit undefined variables and attributes during evaluation.

--pure
    Disallow access to the outside world (e.g., network, /dev/urandom) during evaluation.

--impure
    Allow access to the outside world (this is often the default behavior for the REPL to enable common workflows).

--arg
    Pass an argument named name with Nix value value to the top-level expression loaded into the REPL.

--argstr
    Pass an argument named name with string value value to the top-level expression loaded into the REPL.


    Optional. The path to a Nix expression (file or directory, e.g., ./default.nix or ) to be loaded and made available in the REPL's top-level scope. If a directory, it looks for default.nix.

DESCRIPTION

nix3-repl is the Read-Eval-Print Loop for Nix expressions, part of the modern Nix 3 command-line interface. It provides an interactive shell where users can evaluate Nix code snippets, import files, and inspect Nix values and derivations in real-time. This command is invaluable for debugging Nix expressions, learning the Nix language, and quickly testing configuration fragments or package definitions. It starts with a clean environment unless a specific Nix expression file or path is provided, allowing users to build up their context incrementally. The nix3-repl offers a powerful way to understand how Nix resolves paths, handles attributes, and evaluates lazy expressions, making it an essential tool for Nix developers and users alike.

CAVEATS

Performance: Large Nix expressions or those involving complex computations can lead to slow evaluation within the REPL.
Statefulness: Variables defined in the REPL persist within the session, which can sometimes lead to unexpected interactions if not managed carefully.
Environment Differences: The REPL's evaluation environment might differ slightly from a pure build environment, especially if --impure is used implicitly or explicitly, or due to network access.

INTERACTIVE FEATURES

The REPL supports tab completion for variables, attributes, and built-ins. It also allows multiline input and history navigation, similar to other interactive shell environments.

CONTEXT LOADING

When a path like is provided as an argument (e.g., nix repl ), the REPL makes the expression at that path available. For example, pkgs will then be directly accessible, allowing you to access pkgs.hello or pkgs.lib without explicit imports inside the REPL.

HISTORY

The interactive REPL functionality has been a cornerstone of the Nix ecosystem for a long time, evolving alongside the Nix language itself. The nix3-repl command represents the modern incarnation of this tool, integrated into the unified nix CLI (often referred to as 'new Nix commands') that started gaining prominence around Nix 2.0. This new CLI aims to provide a more consistent and user-friendly interface compared to the older, separate nix-* commands, while retaining the core interactive evaluation capabilities crucial for Nix development and exploration.

SEE ALSO

nix(1), nix build(1), nix shell(1), nix eval(1), nix instantiate(1)

Copied to clipboard