This tutorial shows you how to use the Nix package manager to create temporary shell environments for testing software without installation. This is perfect for quick experimentation and avoiding system clutter.
Table of Contents
Ad Hoc Shell Environments with Nix
Nix's ad hoc shell environments are a powerful feature. They let you try out software packages without permanently adding them to your system. This is ideal for temporary use or testing.
Key benefits include:
gcc
), interpreters (like python
), or other tools.curl
, jq
, or imagemagick
.These environments are lightweight, temporary, and flexible, providing a clean way to work with specific tools.
Testing Packages Without Installation
Ensure Nix is installed. (See "How To Install Nix Package Manager In Linux" for instructions if needed).
To test a C/C program without installing gcc
, create a temporary shell environment:
$ nix-shell -p gcc
This downloads gcc
and dependencies, launching a Bash shell with gcc
available. Check the version:
$ gcc -v
After testing, type exit
or press CTRL D
to leave the environment. gcc
will no longer be accessible outside this shell.
Another example: Test the hello
program:
$ nix-shell -p hello $ hello Hello, world! $ exit
hello
is only available within the nix-shell
session.
Multiple Programs in One Environment
To use gcc
and python3
together, create a single environment:
$ nix-shell -p gcc python3
This gives you access to both. You can compile C/C code and run Python scripts within this shell. The same approach works for any combination of packages. For example, to use cowsay
and lolcat
:
$ nix-shell -p cowsay lolcat $ cowsay "Hello!" | lolcat
Nested Nix Shell Sessions
You can create nested shells. For instance, starting within an existing nix-shell
, you can create another:
$ nix-shell -p git nodejs ruby
This adds git
, nodejs
, and ruby
to the current temporary environment. exit
returns you to the previous shell.
Running Programs Directly
Run programs directly within the nix-shell
:
$ nix-shell -p gcc --run "gcc -o hello hello.c"
This compiles hello.c
. Run the compiled program with ./hello
. Similarly for Python:
$ nix-shell -p python3 --run "python3 my_script.py"
You can also run command-line utilities:
$ nix-shell -p cowsay lolcat --run "cowsay Testing Nix" | lolcat
If the command is just the program name, quotes aren't needed:
$ nix-shell -p hello --run hello
Summary
This tutorial demonstrated how to use Nix's ad hoc shell environments for quick and clean software testing without installation. These temporary environments are invaluable for experimentation and managing dependencies. Refer to "Getting Started With Nix Package Manager" and "How To Create Development Environments With Nix-shell In Linux" for more advanced usage.
The above is the detailed content of How To Test A Package Without Installing It Using Nix In Linux. For more information, please follow other related articles on the PHP Chinese website!