How to use the command 'nix develop' (with examples)
The ’nix develop’ command is used to run a bash shell that provides the build environment of a derivation. It allows developers to work within the environment of a specific package or derivation, ensuring that all the necessary dependencies are available. This is particularly useful for reproducible builds and development workflows with Nix.
Use case 1: Start a shell with all dependencies of a package from nixpkgs available
Code:
nix develop nixpkgs#pkg
Motivation: This use case is useful when you want to quickly access and work within the environment of a specific package from the nixpkgs collection. By using the ’nix develop’ command, you can automatically set up the necessary dependencies and enter a shell with all the required tools and libraries available.
Explanation:
nix develop
: This is the command itself.nixpkgs#pkg
: Replace ‘pkg’ with the desired package name from the nixpkgs collection. This specifies the package we want to set up the environment for.
Example output:
[nix-shell:~]$
Use case 2: Start a development shell for the default package in a flake in the current directory
Code:
nix develop
Motivation: This use case is useful when you are working with a flake and want to start a development shell for the default package defined in that flake. It saves time and ensures that you have all the necessary dependencies available within the shell.
Explanation:
nix develop
: This is the command itself. Since no additional arguments are specified, it automatically detects the flake file in the current directory and starts a development shell for the default package defined in that flake.
Example output:
[nix-shell:~]$
Use case 3: In that shell, configure and build the sources
Code:
configurePhase; buildPhase
Motivation: This use case is useful when you are inside a ’nix develop’ shell and want to configure and build the sources of the package you are working on. It allows you to build and test changes without leaving the development environment.
Explanation:
configurePhase
: This is a Nix lifecycle hook that runs the configuration stage of the package build. It ensures that the package is properly configured and ready for building.buildPhase
: This is another Nix lifecycle hook that runs the actual build process of the package. It compiles the sources and produces the desired output.
Example output:
...
running configure
configuring package...
configure completed successfully
running build
building package...
build completed successfully
...
Conclusion:
The ’nix develop’ command provides a convenient way to set up a development environment for working with specific packages or derivations. Whether you want to start a shell with all the dependencies of a package, work within a flake’s default package, or configure and build sources within a ’nix develop’ shell, this command simplifies the process and ensures a consistent build environment.