nix3-shell
Enter a Nix environment with specified dependencies
TLDR
Start an interactive shell with some packages from nixpkgs
Start a shell providing a package from an older version of nixpkgs (21.05)
Start a shell with the "default package" from a flake in the current directory, printing build logs if any builds happen
Start a shell with a package from a flake on GitHub
Run a command in a shell with a package
SYNOPSIS
nix3-shell [flake-url] [options] [--command cmd] [--arg name value] [--argstr name value] [-- args...]
PARAMETERS
flake-url
The URL of the Nix flake to build or use. This can be a local path, a GitHub URL (e.g., github:NixOS/nixpkgs#hello), or other flake references. If omitted, Nix will look for a flake.nix in the current directory or parent directories.
--command <cmd>
Execute the specified command <cmd> within the created shell environment instead of dropping into an interactive shell.
--arg <name> <value>
Pass an arbitrary Nix value as an argument to the flake's `shell` function, if defined.
--argstr <name> <value>
Pass a string value as an argument to the flake's `shell` function, if defined.
--input <name> <flake-url>
--override-input <name> <flake-url>
Override a specific input of the flake. This is useful for testing local changes to dependencies or using alternative versions.
--offline
Avoid fetching new paths from the Nix store or flake inputs. Useful for reproducible builds when internet access is not guaranteed or undesirable.
--refresh
Force a refresh of flake inputs before building, ensuring the latest versions are used. This updates the flake.lock file.
--show-trace
Show a detailed stack trace on Nix evaluation errors, aiding in debugging complex Nix expressions.
--print-build-logs
Print build logs to standard output, even if the build succeeds, providing verbose output on the build process.
DESCRIPTION
nix3-shell (or nix shell in modern Nix) is a command from the Nix package manager that allows users to create isolated and reproducible development environments. Unlike nix-shell (the older command), nix3-shell is typically used in conjunction with Nix flakes, enabling a more declarative and structured way to define project dependencies. It makes specific packages available in the current shell session without installing them globally on the system, ensuring that different projects can have conflicting dependencies without interference. This command is crucial for maintaining consistent development setups across various machines and collaborators, fostering reproducibility in software development. It fetches and builds the necessary dependencies and then drops the user into a new shell where these dependencies are available in the PATH.
CAVEATS
nix3-shell (or nix shell) is part of the experimental Nix 3.x command set. While stable for many uses, it might still see changes or additions. It relies heavily on Nix flakes, which are also considered experimental by default and require experimental-features = nix-command flakes to be enabled in your nix.conf. Using this command can lead to large downloads or builds if dependencies are complex or not cached in your local Nix store or binary cache.
NIX FLAKES INTEGRATION
nix3-shell is primarily designed to work with Nix flakes, which provide a standardized, reproducible way to define inputs and outputs for Nix projects. This allows for precise version pinning of dependencies and consistent builds across different systems, greatly improving the reliability of development environments.
REPRODUCIBILITY
By leveraging the declarative nature of Nix and its content-addressed store, nix3-shell ensures that environments are fully reproducible. This means that if a project's flake.nix specifies certain dependencies, anyone using nix3-shell with that flake will get the exact same environment, significantly reducing 'it works on my machine' issues across development teams.
GARBAGE COLLECTION ROOTS
When you enter a nix3-shell, a 'garbage collection root' is typically created. This mechanism prevents the Nix garbage collector from prematurely deleting the packages and dependencies required by your active shell environment, ensuring stability and preventing unexpected breakages while you are working.
HISTORY
The concept of nix-shell has existed in Nix for a long time, providing isolated development environments. With the introduction of Nix flakes and the experimental Nix 3.x command-line interface, nix shell (often referred to as nix3-shell during its early experimental phase) was designed to provide a more declarative and standardized way of managing environments, specifically integrating with flake-based projects. It aims to unify various aspects of Nix usage under a consistent CLI, moving away from the older, more ad-hoc nix-shell usage, especially for managing project dependencies, and providing a more reproducible and shareable development experience.
SEE ALSO
nix-shell(1), nix develop(1), nix build(1), nix run(1), nix profile(1)