Nix A Por­ta­ble and Pow­er­ful Pack­age Man­ager

This ar­ti­cle in­tro­duces the reader to Nix, a re­li­able, multi-user, multi-ver­sion, por­ta­ble, re­pro­ducible and purely func­tional pack­age man­ager. Soft­ware en­thu­si­asts will find it a pow­er­ful pack­age man­ager for Linux and UNIX sys­tems.

OpenSource For You - - FOR U & ME LET'S TRY -

Linux is ver­sa­tile and full of choices. Ev­ery other day you wake up to hear about a new dis­tro. Most of th­ese are based on a more fa­mous dis­tro and use its pack­age man­ager. There are many pack­age man­agers like Zyp­per and Yum for Red Hat-based sys­tems; Ap­ti­tude and apt-get for De­bian-based sys­tems; and oth­ers like Pac­man and Emerge. No mat­ter how many pack­age man­agers you have, you may still run into de­pen­dency hell or you may not be able to in­stall mul­ti­ple ver­sions of the same pack­age, es­pe­cially for tin­ker­ing and test­ing. If you fre­quently mess up your sys­tem, you should try out Nix, which is more than “just another pack­age man­ager.”

Nix is a purely func­tional pack­age man­ager. Ac­cord­ing to its site, “Nix is a pow­er­ful pack­age man­ager for Linux and other UNIX sys­tems that makes pack­age man­age­ment re­li­able and re­pro­ducible. It pro­vides atomic up­grades and roll-backs, side-by-side in­stal­la­tion of mul­ti­ple ver­sions of a pack­age, multi-user pack­age man­age­ment and easy set-up of build en­vi­ron­ments.” Here are some rea­sons for which the site rec­om­mends you ought to try Nix. Re­li­able: Nix’s purely func­tional ap­proach en­sures that in­stalling or up­grad­ing one pack­age can­not break other pack­ages. Re­pro­ducible: Nix builds pack­ages in iso­la­tion from each other. This en­sures that they are re­pro­ducible and do not have un­de­clared de­pen­den­cies. So if a pack­age works on one ma­chine, it will also work on another. It’s great for de­vel­op­ers: Nix makes it sim­ple to set up and share build en­vi­ron­ments for your projects, re­gard­less of what pro­gram­ming lan­guages and tools you’re us­ing. Multi-user, multi-ver­sion: Nix sup­ports multi-user pack­age man­age­ment. Mul­ti­ple users can share a common Nix store se­curely with­out the need to have root priv­i­leges to in­stall soft­ware, and can in­stall and use dif­fer­ent ver­sions of a pack­age. Source/bi­nary model: Con­cep­tu­ally, Nix builds pack­ages from source, but can trans­par­ently use bi­na­ries from a bi­nary cache, if avail­able. Por­ta­ble: Nix runs on Linux, Mac OS X, FreeBSD and other sys­tems. Nix­p­kgs, the Nix pack­ages col­lec­tion, con­tains thou­sands of pack­ages, many pre-com­piled.


In­stal­la­tion is pretty straight­for­ward for Linux and Macs; ev­ery­thing is han­dled mag­i­cally for you by a script, but there are some pre-req­ui­sites like sudo, curl and bash, so make sure you have them in­stalled be­fore mov­ing on. Type the fol­low­ing com­mand at a ter­mi­nal:

bash <(curl­stall)

It will ask for sudo ac­cess to cre­ate a di­rec­tory named Nix. You may see some­thing sim­i­lar to what’s shown in Fig­ure 1.

There are bi­nary pack­ages avail­able for Nix but we are look­ing for a new pack­age man­ager, so us­ing another pack­age man­ager to in­stall it is bad form (though you can, if you want to). If you are run­ning another dis­tro with no bi­nary pack­ages while also run­ning Dar­win or OpenBSD, you have the op­tion of in­stalling it from source. To set the en­vi­ron­ment vari­ables right, use the fol­low­ing com­mand:

./~/.nix­pro­file/ etc/pro­file.d/


Now that we have Nix in­stalled, let’s use it for fur­ther test­ing. To see a list of in­stal­lable pack­ages, run the fol­low­ing:

nix­env ­qa

This will list the in­stal­lable pack­ages. To search for a spe­cific pack­age, pipe the out­put of the pre­vi­ous com­mand to Grep with the name of the tar­get pack­age as the ar­gu­ment. Let’s search for Ruby, with the fol­low­ing com­mand:

nix­env ­qa | grep ruby

It in­forms us that there are three ver­sions of Ruby avail­able.

Let’s in­stall Ruby 2.0. There are two ways to in­stall a pack­age. Pack­ages can be re­ferred to by two iden­ti­fiers. The first one is the name of the pack­age, which might not be unique, and the sec­ond is the at­tribute set value. As the re­sult of our search for the var­i­ous Ruby ver­sions showed that the name of the pack­age for Ruby 2.0 is Ruby-2.0.0-p353, let’s try to in­stall it, as fol­lows:

nix­env ­i ruby­2.0.0­p353

It gives the fol­low­ing er­ror as the out­put:

er­ror: un­able to fork: Can­not al­lo­cate mem­ory nix­env: src/libu­til/ int nix::Pid::wait(bool): As­ser­tion `pid != ­1’ failed. Aborted (core dumped)

As per the Nix wiki, the name of the pack­age might not be unique and may yield an er­ror with some pack­ages. So we could try things out with the at­tribute set value. For Ruby 2.0, the at­tribute set value is nix­p­kgs.ruby2 and can be used with the fol­low­ing com­mand:

nix­env ­iA nix­p­kgs.ruby2

This worked. No­tice the use of -iA flag when us­ing the at­tribute set value.

I talked to Nix de­vel­oper Domen Kožar about this and he said, “Mul­ti­ple pack­ages may share the same name and ver­sion; that’s why us­ing at­tribute sets is a bet­ter idea, since it guar­an­tees unique­ness. This is some kind of a down­side of Nix, but this is how it func­tions :)”

To see the at­tribute name and the pack­age name, use the fol­low­ing com­mand:

nix­env ­qaP | grep pack­age_­name

In case of Ruby, I re­placed the pack­age_­name with ruby2 and it yielded:

nix­p­kgs.ruby2 ruby­2.0.0­p353

To up­date a spe­cific pack­age and all its de­pen­den­cies, use:

nix­env ­uA nix­p­kgs.pack­age_at­tribute_­name

To up­date all the in­stalled pack­ages, use:

nix­env ­u

To unin­stall a pack­age, use:

nix­env ­e pack­age_­name

In my case, while us­ing Ruby 2.0, I re­placed it with Ruby2.0.0-p353, which was the pack­age name and not the at­tribute name.

Well, that’s just the tip of the ice­berg. To learn more, re­fer to the Nix man­ual man­ual.

There is a dis­tro named NixOS, which uses Nix for both con­fig­u­ra­tion and pack­age man­age­ment.

Fig­ure 1: Nix in­stal­la­tion

Fig­ure 3: Pack­age and at­tribute us­age

Fig­ure 2: Nix search re­sult

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.