Nix Package Manager for OS X, Linux and Unix

Nix Functional Package Manager

If you are using OS X or Linux or unix based systems then you must be familiar with package managers like HomeBrew, MacPorts, Cask apt, yum, etc. Each of these package managers have their advantages and disadvantages but most of them are known for polluting the /usr/local in the operating system which sometimes can cause headaches (destructive model of package management). Also some package managers cannot handle multiple versions and multi-user package management as well. Nix is a purely functional package manager which is built to overcome these problems.

Nix is also the foundation for NixOS which is linux distribution with configuraiton and package mangement built-in which I will discuss it in a separate article. Nix is nothing but the result of mkDerivation function which is purely functional making package management immutable. To acheive that Nix stores the components in a store called Nix store using hasing function (dervied from inputs to the derivation function, dependencies, sources and build scripts) to generate a unique directory name. The directory name is nothing but a cryptographic hash which will look like below,

Install NIX in OS X

Read Anatomy of Nix Package Management for more information. Also Nix runs all the build scripts in isolated environments and creates a profile for every user. You can have different sets of packages installed in multiple profiles which can be upgraded, reverted all independently. Every time when your profile changes, the older version of profile is kept around to rollback if needed. Apart from using Nix as a package manager you can also use it for setting up build environments for your project. Take a look at Zef Hemel’s Setting up Development Environments with Nix for more information.

Installing and setting up Nix is very easy. Please check out Nix Package Manage Guide which has step by step instructions to get started. I will introuduce some basic commands here to get going as well,

Installing Nix:

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

Searching for packages:

nix-env -qaP | grep nodejs

Test a package without installing:

nix-shell -p cowsay-3.03

This will download the package in an isolated environment and present you in nix-shell without affecting your normal environment which is very handy.

NIX test package without installing

Installing a package:

nix-env -i cowsay-3.03

Uninstalling a package:

nix-env -e cowsay-3.03

More learning resources:

Leave a Comment

Your email address will not be published. Required fields are marked *