DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Isolating Development Environments Using Nix

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.

Ariya Hidayat user avatar by
Ariya Hidayat
·
Jun. 21, 16 · Tutorial
Like (1)
Save
Tweet
Share
6.00K Views

Join the DZone community and get the full member experience.

Join For Free

in 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

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:

  • nix as os x package manager
Package manager Python (language) Attribute (computing) POST (HTTP) Build (game engine) shell Lua (programming language)

Published at DZone with permission of Ariya Hidayat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Simulating and Troubleshooting StackOverflowError in Kotlin
  • Efficiently Computing Permissions at Scale: Our Engineering Approach
  • 5 Tips for Optimizing Your React App’s Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: