Nix has many advantages over most of the other package managers, today, I am going to write about one of them.
Nix is a package manager for the NixOS, but you can install it to other operating systems. One problem on so-called normal systems is that you are/should be very jumpy when installing packages, because you just do not know what and specially where it is going to be installed. Try removing that then…
Well there is a solution - Nix - which works like this: when you install the package, it goes directly to some long-named folder with hash in its name to /nix/store/… and linked into the profile which is available to specific user or all of the users, you can create environments with those profiles.
There are three ways to install a package with Nix
Example (declare inside /etc/nixos/configuration.nix):
Not much to say, packages will be downloaded from cache or compiled if binaries are not available.
Remember: when installing i.e. some perl application, in one environment, you will not see perl executable itself, you will have to specify it separately.
Example (as normal user, without sudo of course):
This package will be installed to something like /nix/store/qv39sv3981kk5h4p260y879wilfc3c26-emacs-24.3 let’s see what is inside (two levels deep)
You can see that everything from this package is in one place. Dependencies are also taken care of in similar manner and are reused for other packages of course.
Well how the hell do users see/use right package? The answer is simple: each user has its own profile which is a folder and links are created into it.
If some other user wants emacs23 and not emacs24, that is not the problem, dependencies are going to be reused and new links into other user profile will be created.
Now here the fun starts!
If you want emacs23 side by side with emacs24, you need to create a custom environment. In this example I will create two custom environments, one for each package, but you can have one emacs in user env. and one in custom env.
Edit/create a new file ~/.nixpkgs/config.nix:
Then you need to build those two profiles:
I choose to create profiles inside /nix/var/nix/profiles/per-user/YOUR_USERNAME/ folder, but you can use ~/.profiles/ or something. The -i flag is the –install flag that we seen before and we are this time installing profiles named homeEnv and workEnv. Let us see what is inside homeEnv (this time one level deep):
You can see that profile is actually created inside /nix/store and if some other user chooses to create the same packages for his profile, only one link will be created - his profile will be the same as yours.
One thing remains… create two more files, one for home environment
and one for work environment
you will need to source those files to set environment variables for the profile or make them executable (also make them available in PATH env variable) and run commands as CLI arguments (hence the $@)
This was child’s play, environments are more useful in development, you can have one environment for Perl development and the other one for Python, of course in every profile, applications will see only each other, well that depends also on shell environment variables, for my Python 2.7 profile, this file now looks like this
This concludes this post and do please leave a comment bellow.