Do you want to quickly test a software package without installing it in your Linux system? You're on the right page! In this tutorial, we will explain how to create ad hoc shell environments to test a package without installing it using Nix package manager in Linux.
Table of Contents
Ad Hoc Shell Environments
One of the useful feature of Nix package manager is Ad hoc shell environments.
Ad hoc shell environments in Nix allow you to experiment with software packages without permanently installing them. This can be helpful when you want to use a particular application temporarily.
Ad hoc shell environments have several practical use cases:
Experimentation and Testing:
- Ad hoc environments allow you to quickly test and experiment with different software packages without permanently installing them on your system.
- For example, you can try out various compilers (like gcc), interpreters (like Python), or other tools.
Isolated Development Environments:
- If you're working on a project that requires specific dependencies, you can create an isolated environment for development.
- This ensures that your project's dependencies don't interfere with your system-wide packages.
Version Control and Reproducibility:
- Nix allows you to specify exact package versions, ensuring reproducibility across different environments.
- With ad hoc environments, you can easily switch between different versions of tools for testing or debugging.
Sharing Environments:
- Suppose you're collaborating with others on a project. You can share the exact environment (including packages) using Nix expressions.
- This ensures that everyone works with the same set of dependencies.
Temporary Tool Usage:
- Need a specific tool for a one-time task? Create an ad hoc environment, use the tool, and discard the environment afterward.
- For instance, you might need curl, jq, or imagemagick for a specific task.
Learning and Tutorials:
- When following tutorials or learning new programming languages, you can set up an environment with the required tools.
- This avoids cluttering your system with unnecessary packages.
Remember that ad hoc shell environments are lightweight, temporary, and flexible. They provide a convenient way to work with specific tools without affecting your system globally.
Test a Package without Installing it in Linux
Make sure you have installed Nix package manager first. If you haven't installed it yet, refer the following guide.
For instance, let us say you want to test your C or C++ program. You don't have to install GCC compiler permanently. Just create a temporary shell environment with gcc, using command:
$ nix-shell -p gcc
This command downloads the gcc package and its dependencies, then drops you into a Bash shell where the gcc command is present, all without affecting your normal environment.
[...]
copying path '/nix/store/18c4pdd8shr33nyjiik9b5wlkv7via1d-mpfr-4.2.1' from 'https://cache.nixos.org'...
copying path '/nix/store/4ckvxdjxsh5gvbxc8bwh40a29w6mvdpr-libmpc-1.3.1' from 'https://cache.nixos.org'...
copying path '/nix/store/fdiknsmnnczx6brsbppyljcs9hqckawk-gcc-12.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/ihhhd1r1a2wb4ndm24rnm83rfnjw5n0z-gcc-wrapper-12.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/10i1kjjq5szjn1gp6418x8bc1hswqc90-stdenv-linux' from 'https://cache.nixos.org'...
[nix-shell:~]$
Check the GCC version:
[nix-shell:~]$ gcc -v
Using built-in specs.
COLLECT_GCC=/nix/store/fdiknsmnnczx6brsbppyljcs9hqckawk-gcc-12.3.0/bin/gcc
COLLECT_LTO_WRAPPER=/nix/store/fdiknsmnnczx6brsbppyljcs9hqckawk-gcc-12.3.0/libexec/gcc/x86_64-unknown-linux-gnu/12.3.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-12.3.0/configure --prefix=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gcc-12.3.0 --with-gmp-include=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gmp-6.3.0-dev/include --with-gmp-lib=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gmp-6.3.0/lib --with-mpfr-include=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-mpfr-4.2.1-dev/include --with-mpfr-lib=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-mpfr-4.2.1/lib --with-mpc=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-libmpc-1.3.1 --with-native-system-header-dir=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-glibc-2.38-44-dev/include --with-build-sysroot=/ --program-prefix= --enable-lto --disable-libstdcxx-pch --without-included-gettext --with-system-zlib --enable-static --enable-languages=c,c++ --disable-multilib --enable-plugin --disable-libcc1 --with-isl=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-isl-0.20 --disable-bootstrap --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=x86_64-unknown-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.3.0 (GCC)
Now, go ahead and test the code. Once you are done, type exit
or CTRL+D
to return back to your console.
[nix-shell:~]$ exit exit
Once you are exit from the nix-shell, you can't use GCC.
Here is another example.
$ nix-shell -p hello
This builds or downloads GNU Hello and its dependencies, then drops you into a Bash shell where the hello command is present, all without affecting your normal environment:
[nix-shell:~]$ hello Hello, world!
Type exit to return back to the console.
[nix-shell:~]$ exit
Now test if hello program is available or not.
$ hello hello: command not found
Create Ad hoc Shell Environments with Multiple Programs
Let's say you want to experiment with both gcc and Python. You can create a combined shell environment like this:
$ nix-shell -p gcc python3
Now you have access to both the gcc compiler and the Python interpreter. You can write and compile C/C++ code or run Python scripts.
Similarly, you can create a shell environment with any combination of the packages.
Not just programming languages, you can run any type of programs in an ad hoc shell environment. The following example shows how to quickly run two fun programs called cowsay and lolcat.
$ nix-shell -p cowsay lolcat
This command will download the necessary dependencies and provide you with a shell where you can use these programs.
Inside the Nix shell, you can run:
$ cowsay "Hello, Welcome to OSTechNix!" | lolcat
Create Nested Nix Shell Sessions
If you want to test another set of packages while working in a temporary shell environment, you can create nested shell sessions as well.
For example, the following command creates a new shell environment with Git, Node, and Ruby.
[nix-shell:~]$ nix-shell -p git nodejs ruby
Inside this session, you can run git
, node
and ruby
commands.
Type exit
to return back to the previous environment.
Running Programs Directly
If you want to run a specific program directly within the Nix shell, you can do so. For example:
$ nix-shell -p gcc --run "gcc -o hello hello.c"
This command tells gcc
to compile hello.c
and output an executable named hello
. After compilation, you can run the program by typing ./hello
in your terminal or command prompt, and you should see the message "Hello, World!" displayed.
Similarly, you can run Python scripts:
$ nix-shell -p python3 --run "python3 my_script.py"
As I said already, you can also run some fun command line utilities as well inside the shell environment.
$ nix-shell -p cowsay lolcat --run "cowsay Test a Package without Installing it using Nix" | lolcat
If the command consists only of the program name, no quotes are needed:
$ nix-shell -p hello --run hello
For more details about Nix package manager's usage, refer the following guide.
Conclusion
In this detailed tutorial, we explained how to create ad hoc shell environments to quickly test software packages without installing them using Nix package manager.
Remember that these ad hoc environments are temporary and won't affect your system outside of the shell. They're great for experimenting, testing, and trying out different tools without cluttering your system.
Related Read: