ariya.io About Talks Articles

Isolated Development Environment using Nix

4 min read

In the earlier blog post, I mentioned the use of Nix as a package manager on OS X. In this follow-up, you will witness the power of Nix to create isolated development environments.

Let us assume I just installed Nix and I don’t have any other packages installed yet:

$ nix-env -qs
IP-  nix-1.11.2
IPS  nss-cacert-3.21

I need to work on a project named Finch. This is a stable project, it is running in production, it relies on a set of solid and proven environments: Go 1.4, PUC-Lua 5.3, and Python 2.7.

On the other hand, I also have another unrelated project, Grove. With this project, I’m still experimenting and thus I want to use the latest cutting-edge technology. Its stack is based on the latest Python 3.5 and Go 1.6. For other reasons, I also needed a faster Lua and thereby I pick LuaJIT. As a version control, Fossil is chosen instead of Git.

For the first project, there is ~/projects/finch/default.nix with the following content:

with import <nixpkgs> {};
stdenv.mkDerivation rec {
  name = "env";
  env = buildEnv { name = name; paths = buildInputs; };
  buildInputs = [
    python
    python27Packages.virtualenv
    python27Packages.pip
    go_1_4
    lua5_3
  ];
}

Without going into Nix expression (refer to the manual for the details), the above file tells Nix to build a new environment with the given list of packages specified by the package’s attribute path, listed as buildInputs. How do I know the attribute path for e.g. Go 1.4? One way is to list all available packages:

$ nix-env -qaP | grep 'go-1.4'
nixpkgs.go_1_4             go-1.4.3

In the above example go_1_4 (or the fully qualified path nixpkgs.go_1_4) is the attribute path for our lovely go-1.4.3 package.

Once this Nix file is ready, every time I want to work on Finch, all I have to do is:

$ cd ~/projects/finch/
$ nix-shell
[nix-shell:~/projects/finch]$

This will start a new shell with all the packages specified in default.nix. That is, I’m going to get the exact specified version of Python, Go, and Lua. If this is done for the first time, Nix needs to install or build those packages but subsequent call to nix-shell will be very fast since it is reusing what is in the cache.

To verify that this is working:

[nix-shell:~/projects/finch]$ python --version
Python 2.7.11
[nix-shell:~/projects/finch]$ pip --version
pip 8.1.2 from /nix/store/3cag9i2pa52qjxq5yvjap6m7jvp6idqm-python2.7-pip-8.1.2/lib/python2.7/site-packages (python 2.7)
[nix-shell:~/projects/finch]$ go version
go version go1.4.3 darwin/amd64
[nix-shell:~/projects/finch]$ lua -v
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio

This is a completely sealed development environment to work on the Finch project. I can use Python, including virtualenv and pip, as expected:

[nix-shell:~/projects/finch]$ virtualenv env
New python executable in env/bin/python2.7
Also creating executable in env/bin/python
Installing setuptools, pip, wheel...done.
[nix-shell:~/projects/finch]$ source env/bin/activate
(env)
[nix-shell:~/projects/finch]$ pip install simplejson
Collecting simplejson
Installing collected packages: simplejson
Successfully installed simplejson-3.8.2
(env)
[nix-shell:~/projects/finch]$ pip list
pip (8.1.2)
setuptools (19.4)
simplejson (3.8.2)
virtualenv (13.1.2)
wheel (0.24.0)
(env)

If I exit the shell, I’m back to the default environment which may not have all the specified packages at all.

[nix-shell:~/projects/finch]$ exit
ariya:~/projects/finch $ go version
-bash: go: command not found
ariya:~/projects/finch $ pip --version
-bash: pip: command not found

env

Now I am switching back to Grove. Its default.nix looks slightly different:

with import <nixpkgs> {};
stdenv.mkDerivation rec {
  name = "env";
  env = buildEnv { name = name; paths = buildInputs; };
  buildInputs = [
    python35
    python35Packages.virtualenv
    python35Packages.pip
    luajit
    fossil
  ];
}

My initial step before working on Grove:

$ cd ~/projects/grove/
$ nix-shell
[nix-shell:~/projects/grove]$

And it’s easy to see what I get within this environment:

nix-shell:~/projects/grove]$ fossil version
This is fossil version 1.33 [9c65b5432e] 2015-05-23 11:11:31 UTC
[nix-shell:~/projects/grove]$ lua -v
LuaJIT 2.1.0-beta1 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
[nix-shell:~/projects/grove]$ virtualenv env
New python executable in env/bin/python3.5m
Also creating executable in env/bin/python
Installing setuptools, pip, wheel...done.
[nix-shell:~/projects/grove]$ source env/bin/activate
(env)
[nix-shell:~/projects/grove]$ pip list
pip (7.1.2)
setuptools (19.4)
virtualenv (13.1.2)
wheel (0.24.0)
(env)

As you can see, I keep my global environment as clean as possible and at the same time, I have the flexible working environment for my two (or more) different projects. The necessary dependent packages of one project will not interfere or pollute other projects, even if it is the same package with different versions (Python 2.7 vs Python 3.5, Go 1.4 vs Go 1.6, PUC-Lua 5.3 vs Lua-JIT 2.1).

Enjoy!

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook