Isolating Development Environments Using Nix
In this follow-up from an earlier post, you will witness the power of Nix for creating isolated development environments.
Join the DZone community and get the full member experience.
Join For Freein an 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 for creating 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, and 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 need a faster lua and so 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 expressions (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 calls 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
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:
Published at DZone with permission of Ariya Hidayat, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments