Nix as an OS X Package Manager
Linux users have long enjoyed package management and its useful grasp of dependencies. Nix is a tool now available on OSX, which translates some of the simplicity of *nix-style management.
Join the DZone community and get the full member experience.Join For Free
power users on os x are familiar with homebrew or macports for installing and managing software packages conveniently. yet, those two well-known tools are not the exclusive players. there is a growing interest in nix , particularly for its use on os x.
package management using nix is quite simple and intuitive. it does work quite well to replace homebrew and macports. to get started, install nix following the instructions :
curl https://nixos.org/nix/install | sh
nix only needs access to
, it does not touch any other top-level directories (nix will never pollute your
). hence, removing nix is a matter to nuking that
once it is installed, the main command-line tool you will interact the most will be
. try installing a trivial package like this:
$ nix-env -i hello installing ‘hello-2.10’ these paths will be fetched (0.02 mib download, 0.07 mib unpacked): /nix/store/b6bxihaz9s5c79dsgbbxvjg8w44a036i-hello-2.10 fetching path ‘/nix/store/b6bxihaz9s5c79dsgbbxvjg8w44a036i-hello-2.10’... $ hello --version hello (gnu hello) 2.10
note the installation path, a peculiar subdirectory under
. the name contains the cryptographic hash of all inputs necessary to build the package, essentially capturing the complete build dependencies. this enables powerful
such as easy handling of multiple package versions, atomic installation, and many more.
nix also creates a profile for every user, which you once you search for an executable (the importance of nix profile itself will be more obvious once you start to be more familiar with nix).
$ which hello /users/ariya/.nix-profile/bin/hello
removing a package is as easy as installing it:
$ nix-env -e hello uninstalling ‘hello-2.10’
in many cases, nix will install a package in its binary form (as built and cached by the hydra-based build farm).
wondering what you can install with nix? well, nix’s collection of packages (especially on os x, around seven thousand) is not as impressive as homebrew and macports. yet, you may find the common packages already available, from git to vim (and its plugins). to list all available packages:
just like every package manager, nix is also useful to upgrade your arsenal of tools. for instance, os x el capitan is armed with git 2.6 by default. but perhaps you want to use the most recent git 2.8 instead. this is not a difficult endeavor:
$ git --version git version 2.6.4 (apple git-63) $ nix-env -i git warning: there are multiple derivations named ‘git-2.8.0’; using the first one installing ‘git-2.8.0’ $ which git /users/ariya/.nix-profile/bin/git $ git --version git version 2.8.0
later on, if you decide that you don’t like the latest version and you prefer to stick with the default one, the rollback leaves no meaningful left-over and it returns the state of the system exactly before you installed git 2.8:
$ nix-env -e git uninstalling ‘git-2.8.0’ $ which git /usr/bin/git $ git --version git version 2.6.4 (apple git-63)
these package management tasks are not unique to nix. wait for the sequel of this post, where we learn the power of nix to comfortably handle multiple environments (e.g. python 2.7 vs python 3.5).
Published at DZone with permission of Ariya Hidayat, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.