How to use the command `nix repl` (with examples)
The nix repl
command is used to start an interactive environment for evaluating Nix expressions. It provides a way to experiment and test Nix expressions in an interactive manner.
Use case 1: Start an interactive environment for evaluating Nix expressions
Code:
nix repl
Motivation:
Starting a REPL environment is useful when you want to experiment with Nix expressions. It allows you to quickly evaluate Nix code and see the results in real-time.
Explanation:
The nix repl
command starts the Nix REPL (Read-Eval-Print Loop), which provides an interactive environment for evaluating Nix expressions. It allows you to enter Nix expressions and see their results immediately.
Example output:
Welcome to Nix version 3.0.4. Type :? for help.
nix-repl> 2 + 2
4
Use case 2: Load all packages from a flake into scope
Code:
:lf nixpkgs
Motivation:
Loading all packages from a flake into scope is useful when you want to work with a specific set of packages and have them readily available for evaluation.
Explanation:
The :lf
command is used within the Nix REPL to load all packages from a flake into scope. In this example, we are using the nixpkgs
flake, which contains a collection of packages.
Example output:
nix-repl> :lf nixpkgs
Added 10 packages from flake:/nix/store/abcd1234-nixpkgs to the scope.
Use case 3: Build a package from an expression
Code:
:b expression
Motivation:
Building a package from an expression is useful when you want to compile a Nix expression into a package that can be installed or executed.
Explanation:
The :b
command is used within the Nix REPL to build a package from a Nix expression. In this example, expression
refers to the Nix expression that defines the package to be built.
Example output:
nix-repl> :b pkgs.hello
building '/nix/store/efgh5678-hello'...
Use case 4: Start a shell with a package from the expression available
Code:
:u expression
Motivation:
Starting a shell with a package from the expression available is useful when you want to explore the contents or interact with a specific package without installing it.
Explanation:
The :u
command is used within the Nix REPL to start a shell with a package from a Nix expression available. In this example, expression
refers to the Nix expression that defines the package.
Example output:
nix-repl> :u pkgs.hello
Spawning a shell...
Entering /nix/store/efgh5678-hello...
Use case 5: Start a shell with dependencies of the package from the expression available
Code:
:s expression
Motivation:
Starting a shell with the dependencies of the package available is useful when you want to test or debug a package along with its dependencies.
Explanation:
The :s
command is used within the Nix REPL to start a shell with the dependencies of a package from a Nix expression available. In this example, expression
refers to the Nix expression that defines the package.
Example output:
nix-repl> :s pkgs.hello
Spawning a shell...
Entering /nix/store/efgh5678-deps...
Conclusion:
The nix repl
command is a powerful tool for working with Nix expressions. It allows you to evaluate and experiment with expressions, load packages into scope, build packages, and start shells with packages or their dependencies available. By understanding each of the use cases and their examples, you can leverage the nix repl
command effectively in your Nix development workflow.