How to use the command 'nix shell' (with examples)
The nix shell
command is a powerful tool from the Nix package manager suite, enabling users to create temporary shell environments with specific software packages made available. It is a key tool for those working with the NixOS system or any Nix-enabled Linux distribution. This command serves multiple purposes, such as setting up development environments, testing different versions of packages, and running commands with dependencies temporarily sourced from Nix packages.
Use case 1: Start an interactive shell with some packages from nixpkgs
Code:
nix shell nixpkgs#pkg1 nixpkgs#packageSet.pkg2 ...
Motivation:
Oftentimes, developers need to test or utilize several packages together without permanently installing them on their system. This approach is perfect for experimenting or rapidly prototyping with various libraries and tools, ensuring that there are no long-term impacts on the existing environment. It allows easy switching between different sets of packages and ensures a clean slate whenever necessary.
Explanation:
nix shell
: The primary command to start a new shell with specified packages.nixpkgs#pkg1
: Specifies a package namedpkg1
from the default channel,nixpkgs
.nixpkgs#packageSet.pkg2
: Indicates another package,pkg2
, which is within a specific package set innixpkgs
.
Example Output:
Upon executing the command, an interactive shell session begins, with pkg1
and pkg2
available. You can verify this by executing package-specific commands or checking the version of the packages directly from the shell.
Use case 2: Start a shell providing a package from an older version of nixpkgs
(21.05)
Code:
nix shell nixpkgs/nixos-21.05#pkg
Motivation:
In certain situations, newer versions of packages may introduce breaking changes or incompatibilities. Reverting back to an older version helps ensure a smooth workflow, especially for maintaining legacy systems or ensuring consistency during an upgrade process. Having access to an older version allows users to test software with the dependencies it was originally built against.
Explanation:
nix shell
: Initiates a shell session with specific package versions.nixpkgs/nixos-21.05#pkg
: Defines thepkg
package from thenixos-21.05
release. It targets a specific historical snapshot of thenixpkgs
repository.
Example Output:
The shell starts, providing access to pkg
as it was available in version 21.05
. Users confirm the package version through commands within the shell, ensuring they have the right historical build.
Use case 3: Start a shell with the “default package” from a flake in the current directory, printing build logs if any builds happen
Code:
nix shell -L
Motivation:
When developing a Nix flake-based project, you may want to test if everything is working correctly by loading the default package defined in the flake. The -L
flag is particularly useful for debugging purposes, as it prints the build logs, allowing users to identify any issues that arise during the build phase.
Explanation:
nix shell
: The foundational command to open a shell environment.-L
: This flag enables the logging of build output, offering insights into potential errors or warnings during the process.
Example Output:
If builds are executed, relevant logs appear, providing comprehensive insights into compilation steps. If successful, the shell environment includes the project’s default package, readily usable for development or testing tasks.
Use case 4: Start a shell with a package from a flake on GitHub
Code:
nix shell github:owner/repo#pkg
Motivation:
Nix’s flake mechanism allows for version-controlled configuration, and GitHub repositories are a common way to distribute these configurations. By pulling a package from a flake stored in a GitHub repo, you ensure you’re working with the latest updates or specific revisions of publicly shared projects, easing collaboration and ensuring compatibility.
Explanation:
nix shell
: Initiates the shell environment.github:owner/repo#pkg
: Specifies the packagepkg
that you want to derive from the specified GitHub repository located underowner
andrepo
.
Example Output:
The shell begins with access to the specified package derived from the GitHub repository. Utilizing project components becomes seamless, with immediate verification and use of features or tools provided by the package encapsulated within the repository.
Use case 5: Run a command in a shell with a package
Code:
nix shell nixpkgs#pkg -c some-cmd --someflag 'Some other arguments'
Motivation:
Sometimes you want the ability to run a single command — rather than start an interactive shell session — with a specific version of a package and any necessary dependencies temporarily. This can be particularly useful for scripts or integrating Nix within larger automated workflows, improving speed and resource usage.
Explanation:
nix shell
: Initiates a temporary environment with needed packages.nixpkgs#pkg
: Specifies the package required to run the command in question.-c some-cmd
: Directsnix-shell
to runsome-cmd
within the prepared environment.--someflag 'Some other arguments'
: Represents any flags or arguments needed by the command defined.
Example Output:
The specified command runs within the environment where pkg
and its dependencies are available, producing any relevant results directly in the terminal. Following the command’s execution, the environment closes, ensuring a non-intrusive approach to package management.
Conclusion:
The nix shell
command is an instrumental part of the Nix toolset, providing various capabilities from starting temporary shell environments to running isolated commands efficiently. Whether embracing new technologies through flakes, maintaining historical packages, or simply experimenting, the command serves as a robust approach to managing and deploying software across different environments. Its flexible nature caters to both individual developers and complex automated systems, making it indispensable in contemporary software development and deployment pipelines.