LinuxCommandLibrary

nix-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

nix-repl [options] [path-or-expression]

PARAMETERS

--help
    Displays help information and exits.

--version
    Displays version information and exits.

--arg name value
    Passes an argument name with Nix expression value to the evaluated Nix expression.

--argstr name value
    Passes an argument name with string value to the evaluated Nix expression.

--file path
    Loads the specified Nix file into the REPL environment upon startup. If omitted, default.nix in the current directory is loaded, or an empty scope is provided.

--impure
    Allows impure operations, such as accessing arbitrary files or environment variables.

--option name value
    Sets a Nix configuration option for the duration of the REPL session.

path-or-expression
    Optional. One or more paths to Nix expressions or directories to load. If a directory, default.nix inside it is loaded. Also can add to the Nix search path (NIX_PATH) for the session.

DESCRIPTION

nix-repl is the interactive Read-Eval-Print Loop (REPL) for the Nix expression language. It provides a powerful environment for developers to experiment with Nix code, explore the Nixpkgs set, and debug Nix expressions directly. Users can type Nix expressions at the prompt, and nix-repl immediately evaluates and displays the result. This interactive nature makes it an invaluable tool for understanding how Nix evaluates expressions, inspecting attribute sets, and testing package definitions without having to build them. It supports features like tab completion for attributes and paths, multiline input, and the ability to load a Nix file or context at startup, enabling rapid prototyping and iterative development within the Nix ecosystem.

CAVEATS

The REPL operates within the Nix sandbox environment by default, which can restrict access to external files or network resources. Use --impure for broader access.
Changes made within the REPL are not persistent; they are lost once the session exits.
Evaluating large expressions or building complex derivations can be resource-intensive within the REPL.

INTERACTIVE COMMANDS

nix-repl supports several built-in commands accessible with a colon prefix, such as :l <file> to load a file, :cd <dir> to change directory, :p <expr> to pretty-print an expression, :t <expr> to show the type, and :? for help. These commands significantly enhance the interactive experience beyond simple expression evaluation.

LOADING CONTEXT

By default, nix-repl attempts to load default.nix from the current directory. If a path is provided as an argument (e.g., nix-repl ./my-nix-file.nix), it loads that specific Nix expression, making it easy to jump into the context of a particular project or library or to explore specific Nixpkgs attributes.

HISTORY

nix-repl is an integral part of the Nix package manager, which was first conceived by Eelco Dolstra around 2003 as a research project at Utrecht University. The REPL provides a crucial interactive component to the purely functional Nix language, allowing users to rapidly prototype and debug expressions. Its development has evolved alongside the Nix ecosystem, remaining a stable and essential tool for interacting with Nix expressions, particularly useful for exploring Nixpkgs and developing new packages. With the advent of the unified nix command in newer Nix versions, nix-repl is often invoked as nix repl, signifying its core role within the Nix CLI.

SEE ALSO

nix-build(1), nix-shell(1), nix-instantiate(1), nix(1), nix(1) repl, ghci(1), python(1)

Copied to clipboard