Nix is more than a package manager, it is also a programming language and it turns out that those two together can make reproducible enrivonments.

In this blog post I will show its usage only as package manager.

My desktop is Arch Linux, I managed to install and use Nix without a problem. To write this blog post I installed Ubuntu in a virtual machine, and it also worked well.

Install Nix

If you are like me that don't like shell scripts asking for password, run this command:

$ sudo mkdir /nix
$ sudo chown $(id -u):$(id -u) /nix

Close your terminal and open it again, run sudo ls to make sure it is asking your password and don't type it.

In this new terminal run this command to install Nix:

$ curl https://nixos.org/nix/install | sh

Everything it needs will be installed on /nix and linked on ~/.nix-profile.

NOTE: It also creates ~/.nix-channels and ~/.nix-defexpr/ but it is not important for the purpose of this blog post 😉.

Finally, you need to activate it with this code:

. ~/.nix-profile/etc/profile.d/nix.sh

Run that in your terminal or add to your ~/.profile.

Test Before Installing

You can install packages, but the coolest thing is that you can test the package before installing. In order to do that you can run a command like this:

$ nix-shell -p hello

It will download and prepare a shell for you with that package installed. At the end of your test you can type exit and the package won't be there any more:

$ nix-shell -p hello
[nix-shell:~]$ hello
Hello, world!

[nix-shell:~]$ exit
$ hello
zsh: command not found: hello

Install Packages

If you really want to install a package you have to type:

$ nix-env -i hello

An important note here: Nix caches. The second time you run the same command (i. e., nix-env -p hello) you will notice it has less output than before, it is because of that.

Generations

Every time you install packages Nix changes the generation you are.

If you need to undo something you can, of course, uninstall package(s) OR you can rollback to the previous generation (or to any other) and delete the one you were.

Let's test this, assuming you had run only nix-env -i hello you will have two generations:

$ nix-env --list-generations
   1   2020-05-16 17:55:13
   2   2020-05-16 18:33:29   (current)

If you type hello the command will be there. Now rollback with:

dmitry@dmitry-VirtualBox:~$ nix-env --rollback
switching from generation 2 to 1

And try to run hello again. It won't be there.

Check the generations again. You will see there are still two generations and you are back to the first one:

$ nix-env --list-generations
   1   2020-05-16 17:55:13   (current)
   2   2020-05-16 18:33:29

In order to delete one generation you can run:

$ nix-env --delete-generations 2
removing generation 2

After that there is only one generation:

$ nix-env --list-generations
   1   2020-05-16 17:55:13   (current)

If you need to rollback to a particular generation use the command:

$ nix-env -G 3

That flag is called --delete-generations because you can delete more than one:

$ nix-env --delete-generations 2 3

Uninstall Packages

Use this:

$ nix-env -e hello

This also creates a generation.

List Installed Packages

$ nix-env -q

This will list only the packages you installed with nix-env -i pkg it won't list the dependencies. It is equivalent to pacman -Qe.

Search Packages

I prefer to go to https://nixos.org/nixos/packages.html to find something.

To list all the packages you can install:

$ nix-env -qa

You can also run any one of these two to search for a package:

$ nix-env -qa 'bash'
bash-4.4-p23
bash-5.0-p16
$ nix-env -qa 'bash.*'
bash-4.4-p23
bash-5.0-p16
bash-completion-2.10
bash-completion-2.10
bash-interactive-4.4-p23
bash-interactive-5.0-p16
bash-my-aws-20200111
bash-supergenpass-unstable-2018-04-18
bashburn-3.1.0
bashdb-4.4-1.0.0
bashmount-3.2.0
bashplotlib-2019-01-02
bashSnippets-1.23.0
bash_unit-1.7.1

Remove Nix from Your System

Delete /nix and those other three directories:

  • /nix
  • ~/.nix-profile
  • ~/.nix-channels
  • ~/.nix-defexpr/