Saturday, July 26, 2014

Nix pill 4: the basics of the language

Welcome to the fourth Nix pill. In the previous third pill we entered the Nix environment. We installed software as user, managed the profile, switched between generations, and queried the nix store. That's the very basics of nix administration somehow.

The Nix language is used to write derivations. The nix-build tool is used to build derivations. Even as a system administrator that wants to customize the installation, it's necessary to master Nix. Using Nix for your jobs means you get the features we saw in the previous pills for free.

The syntax is very uncommon thus looking at existing examples may lead to thinking that there's a lot of magic behind. In reality, it's only about writing utility functions for making things convenient.
On the other hand, this same syntax is great for describing packages.

Important: in Nix, everything is an expression, there are no statements. This is common to many functional languages.
Important: values in Nix are immutable.

Value types

We've installed nix-repl in the previous pill. If you didn't, nix-env -i nix-repl. The nix-repl syntax is slightly different than nix syntax when it comes to assigning variables, but no worries.  I prefer playing with nix-repl with you before cluttering your mind with more complex expressions.

Launch nix-repl. First of all, nix supports basic arithmetic operations: +, -, and *. The integer division can be done with builtins.div.
nix-repl> 1+3
nix-repl> builtins.div 6 3
Really, why doesn't nix have basic operations such as division? Because it's not needed for creating packages. Nix is not a general purpose language, it's a domain-specific language for writing packages.
Just think that builtins.div is not being used in the whole of our nixpkgs repository: it's useless.

Other operators are ||, && and ! for booleans, and relational operators such as !=, ==, <, >, <=, >=. In Nix, <, >, <= and >= are not much used. There are also other operators we will see in the course of this series.

Nix has integer (not floating point), string, path, boolean and null simple types. Then there are lists, sets and functions. These types are enough to build an operating system.

Nix is strongly typed, but it's not statically typed. That is, you cannot mix strings and integers, you must first do the conversion. 

Try to use / between two numbers:
nix-repl> 2/3
Nix parsed 2/3 as a relative path to the current directory. Paths are parsed as long as there's a slash. Therefore to specify the current directory, use ./.
In addition, Nix also parses urls.

Not all urls or paths can be parsed this way. If a syntax error occurs, it's still possible to fallback to plain strings. Parsing urls and paths are convenient for additional safety.


Not much to say, except that dash (-) is allowed in identifiers. That's convenient since many packages use dash in its name. In fact:
nix-repl> a-b
error: undefined variable `a-b' at (string):1:1
nix-repl> a - b
error: undefined variable `a' at (string):1:1
As you can see, a-b is parsed as identifier, not as operation between a and b.


It's important to understand the syntax for strings. When reading Nix expressions at the beginning, you may find dollars ($) ambiguous in their usage.
Strings are enclosed by double quotes ("), or two single quotes ('').
nix-repl> "foo"
nix-repl> ''foo''
In python you can use also single quotes for strings like 'foo', but not in Nix.

It's possible to interpolate whole Nix expressions inside strings with ${...} and only with ${...}, not $foo or {$foo} or anything else.
nix-repl> foo = "strval"
nix-repl> "$foo"
nix-repl> "${foo}"
nix-repl> "${2+3}"
error: cannot coerce an integer to a string, at (string):1:2
Note: ignore the foo = "strval" assignment, it's nix-repl special syntax.

As said previously, you cannot mix integers and strings. You explicitly need conversion. We'll see this later: function calls are another story.

Using the syntax with two single quotes, it's useful for writing double quotes inside strings instead of escaping:
nix-repl> ''test " test''
"test \" test"
nix-repl> ''${foo}''
Escaping ${...} within double quoted strings is done with the backslash. Within two single quotes, it's done with '':
nix-repl> "\${foo}"
nix-repl> ''test ''${foo} test''
"test ${foo} test"
No other magic about strings for now.


Lists are a sequence of expressions delimited by space (not comma):
nix-repl> [ 2 "foo" true (2+3) ]
[ 2 "foo" true 5 ]
Lists, like anything else in Nix, are immutable. Adding or removing elements from a list is possible, but will return a new list.


Sets are an association between a string key and a Nix expression. Keys can only be strings. When writing sets you can also use identifiers as keys.
nix-repl> s = { foo = "bar"; a-b = "baz"; "123" = "num"; }
nix-repl> s
{ 123 = "num"; a-b = "baz"; foo = "bar"; }
Note: here the string representation from nix is wrong, you can't write { 123 = "num"; } because 123 is not an identifier.
You need semicomma (;) after every key-value assignment.

For those reading Nix expressions from nixpkgs: do not confuse sets with argument sets used in functions.

To access elements in the set:
nix-repl> s.a-b
nix-repl> s."123"
Yes, you can use strings for non-identifiers to address keys in the set.

You cannot refer inside a set to elements of the same set:
nix-repl> { a = 3; b = a+4; }
error: undefined variable `a' at (string):1:10
To do so, use recursive sets:
nix-repl> rec { a= 3; b = a+4; }
{ a = 3; b = 7; }
This will be very convenient when defining packages.

If expression

Expressions, not statements.
nix-repl> a = 3
nix-repl> b = 4
nix-repl> if a > b then "yes" else "no"
You can't have only the "then" branch, you must specify also the "else" branch, because an expression must have a value in all cases.

Let expression

This kind of expression is used to define local variables to inner expressions.
nix-repl> let a = "foo"; in a
The syntax is: first assign variables, then "in" expression. The overall result will be the final expression after "in".
nix-repl> let a = 3; b = 4; in a + b
Let's write two let expressions, one inside the other:
nix-repl> let a = 3; in let b = 4; in a + b
With let you cannot assign twice to the same variable. You can however shadow outer variables:
nix-repl> let a = 3; a = 8; in a
error: attribute `a' at (string):1:12 already defined at (string):1:5
nix-repl> let a = 3; in let a = 8; in a
You cannot refer to variables in a let expression outside of it:
nix-repl> let a = (let b = 3; in b); in b
error: undefined variable `b' at (string):1:31
You can refer to variables in the let expression when assigning variables like with recursive sets:
nix-repl> let a = 4; b = a + 5; in b
So beware when you want to refer to a variable from the outer scope, but it's being defined in the current let expression. Same applies to recursive sets.

With expression

This kind of expression is something you hardly see in other languages. Think of it like a more granular "using" of C++, or "from module import *" from Python. You decide per-expression when to include symbols into the scope.
nix-repl> longName = { a = 3; b = 4; }
nix-repl> longName.a + longName.b
nix-repl> with longName; a + b
That's it, it takes a set and includes symbols in the scope of the inner expression. Of course, only valid identifiers from the set keys will be included.
If a symbol exists in the outer scope and also in the "with" scope, it will not be shadowed. You can however still refer to the set:
nix-repl> let a = 10; in with longName; a + b
nix-repl> let a = 10; in with longName; longName.a + b


Nix evaluates expression only when needed. This is a great feature when working with packages.
nix-repl> let a = builtins.div 4 0; b = 6; in b
Since "a" is not needed, there's no error about division by zero, because the expression is not in need to be evaluated.
That's why we can have all the packages defined here, yet access to specific packages very fast.

Next pill

...we will talk about functions and imports. In this pill I've tried to avoid function calls as much as possible, otherwise the post would have been too long.

Nix pill 5 is available for reading here.

To be notified about the new pill, stay tuned on #NixPills, follow @lethalman or subscribe to the nixpills rss.


Carlo Nucera said...

Hey, (at least from) this post onward, this series is great!!! Keep on with the good work!!!

(as an aside: nix + haskell + Calabria is a quite rare combination!!! (I'm spending vacations near Reggio)).

Luca Bruno aka Lethalman said...

Thanks Carlo :) I hope you enjoy Calabria. My vacation will start in a week.

Alin said...

I am feeling quite interested to know about Nix language!! Great!

Shanthakumar V said...

Nice ;)

Shanthakumar V said...
This comment has been removed by the author.
Priya Kannan said...

Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
PHP Training in Chennai

Nicholas de said...

It is very interesting to learn from to easy understood. Thank you for giving information. Please let us know and more information get post to link.
mulesoft training online

gowsalya saranya said...

Existing without the answers to the difficulties you’ve sorted out through this guide is a tical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
Digital Marketing Training in Chennai

Aws Training in Chennai

Selenium Training in Chennai

johnsy sai said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
Digital Marketing Training in Bangalore

digital marketing training in tambaram

digital marketing training in annanagar

digital marketing training in marathahalli

digital marketing training in rajajinagar

Digital Marketing online training

Mouni yoga said...

I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol I know it sounds funny but its very true. . .
python training institute in chennai
python training in Bangalore
python training institute in chennai

aruna raj said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

Blueprism training in btm

Blueprism online training

AWS Training in chennai

Shanthakumar said...

How does digital marketing training related to 5hus blog post?

geetha sridhar said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in pune
Data science training in kalyan nagar

sudhagar raja said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

java training in chennai | java training in bangalore

java online training | java training in pune

Anonymous said...

Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

angularjs Training in bangalore

narayana p said...

Thanks for sharing this post. Your post is really very helpful its students. python online course

Praylin S said...

Very useful information! Thanks for sharing. Looking forward for more posts from you.
Microsoft Dynamics CRM Training in Chennai | Microsoft Dynamics Training in Chennai | Microsoft Dynamics CRM Training | Microsoft Dynamics CRM Training institutes in Chennai | Microsoft Dynamics Training | Microsoft CRM Training | Microsoft Dynamics CRM Training Courses | CRM Training in Chennai

Robotic Process Automation Tutorial said...

Thank you so much for your information,its very useful and helful to me.Keep updating and sharing. Thank you.
RPA training in chennai | UiPath training in chennai

kavinilavu G said...

Such a Great Article!! I learned something new from your blog. Amazing stuff. I would like to follow your blog frequently. Keep Rocking!!
Blue Prism training in chennai | Best Blue Prism Training Institute in Chennai

Rithi Rawat said...

It's really a nice experience to read your post. Thank you for sharing this useful information. If you are looking for more about machine learning with python course in Chennai | machine learning course in Chennai | Android training in chennai

Rithi Rawat said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.AngularJS Training in Chennai | Best AngularJS Training Institute in Chennai

DJ PRASATH said...

Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.
best selenium training in chennai | best selenium training institute in chennai selenium training in chennai | best selenium training in chennai | selenium training in Velachery | selenium training in chennai omr | quora selenium training in chennai | selenium testing course fees | java and selenium training in chennai | best selenium training institute in chennai | best selenium training center in chennai

Roja Priya said...

Given so much info in it, The list of your blogs are very helpful for those who want to learn more interesting facts. Keeps the users interest in the website, and keep on sharing more, To know more about our service:
Please free to call us @ +91 9884412301 / 9600112302

Openstack course training in Chennai | best Openstack course in Chennai | best Openstack certification training in Chennai | Openstack certification course in Chennai | openstack training in chennai omr | openstack training in chennai velachery | openstack training in Chennai

amsa leka said...

Wow!! Really a nice Article. Thank you so much for your efforts. Definitely, it will be helpful for others. I would like to follow your blog. Share more like this. Thanks Again.
iot training in Chennai | Best iot Training Institute in Chennai

Praylin S said...

Very informative post! I'm glad that I came across your post. Looking forward for more posts from you.
Tally Course in Chennai
Tally Classes in Chennai
Tally Training in Chennai
Training and Placement institutes in Chennai
Oracle Training in Chennai
LINUX Training in Chennai

Sadhana Rathore said...

I have to thank for sharing this blog, really helpful.
AWS Training in Chennai
AWS course in Chennai
DevOps Training in Chennai
Angularjs Training in Chennai
RPA Training in Chennai
R Programming Training in Chennai
Data Science Course in Chennai
Machine Learning Training in Chennai
Python Training in Chennai

kavinilavu G said...

Great post! I am actually getting ready to across this information, It's very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.DevOps Training in Chennai | Best DevOps Training Institute in Chennai

amsa leka said...

Nice blog..! I really loved reading through this article. Thanks for sharing such a
amazing post with us and keep blogging... Best React js training near me | React js training online

Anbarasan14 said...

Nice post. Thanks for sharing.

Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
Best Spoken English Classes in Chennai
Best Spoken English Institute in Chennai
English Coaching Class in Chennai
Best English Coaching Center in Chennai

Lithiksha said...

Thanks Admin For sharing this massive info with us. it seems you have put more effort to write this blog , I gained more knowledge from your blog. Keep Doing..
DevOps Training in Chennai
DevOps Certification in Chennai
AWS Training in Chennai
Data Science Course in Chennai
Digital Marketing Course in Chennai
DevOps Training in Adyar
DevOps Training in Tambaram
DevOps Training in OMR

Priyadarshini Yadav said...

I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Artificial Intelligence Training in Bangalore. Keep sharing your information regularly for my future reference.

Digital marketing company chennai said...

Awesomwly shared post!
Internet marketing company in chennai

jefrin adams said...

Wow good to read thanks
R programming training in chennai

harish sharma said...

whatsapp group links list

Kovalan J said...

Looking to add extra style to your content, make use of our strikethrough Text Generator to add amazing line through text in desired platform. Strikethrough in Google Docs.

amsa leka said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.angularjs best training center in chennai | angularjs training in velachery | angularjs training in chennai | best angularjs training institute in chennai |

janu said...

You are doing a great job. I would like to appreciate your work for good accuracy
r programming training in chennai | r training in chennai
r language training in chennai | r programming training institute in chennai
Best r training in chennai

bsnlll said...

BSNL Speed Test:- Today the high-speed internet is considered as the most important requirement of an internet connection. It ensure comfort Bsnl speedtest.
bsnl speed test

service care said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
lg mobile service center in porur
lg mobile service center in vadapalani

dhanam said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Microsoft Azure online training
Selenium online training
Java online training
Java Script online training
Share Point online training

Ram Niwas said...

Wonderful Blog, It is really helpful for us. thanks for sharing

Website Designing Company in Delhi

Ram Niwas said...

Really informative post! Thanks for sharing your thoughts with us. Keep blogging.

Digital Marketing Training online

sasireka said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

devops online training

aws online training

data science with python online training

data science online training

rpa online training

Raji said...

I found your web site by the use of Google at the same time as searching for a comparable subject, your site came up. It seems great. I have bookmarked it in my google bookmarks to come back then.
R Training Institute in Chennai | R Programming Training in Chennai

Chiến SEOCAM said...

Mam nadzieję, że będziesz mieć więcej ciekawych i interesujących artykułów. Dziękuję bardzo

lưới chống chuột

cửa lưới dạng xếp

cửa lưới tự cuốn

cửa lưới chống muỗi hà nội

ppsspppsp said...

ppsspp emulator for mobile
ppsspp emulator apk mirror
ppsspp gold psp emulator
ppsspp emulator gold apk mania
ppsspp emulator games for mobile
ppsspp emulator new version download

Pugazhendhi Dhamodaran said...

Thanks for your informative article, Your post helped me to know the future and career prospects updating your blog with such amazing article.
Angularjs training in Bangalore
Angularjs training institute in Bangalore
Angularjs training course in Bangalore

prakash gohel said...

Thank you for this great article i learn a lot from your article keep it up.
attitude status in hindi
Life status in hindi
Love Status in hindi

Anonymous said...

download lucky patcher & download gbwhatsapp For Android!

Teena bhabhi said...

many peoples want to join random whatsapp groups . as per your demand we are ready to serve you whatsapp group links . On this website you can join unlimited groups . click and get unlimited whatsapp group links

Chiến SEOCAM said...

ኦህ አምላክ! ይህ ታላቅ ጽሑፍ ነው. ስላጋሩ እናመሰግናለን!

Cửa tiệm bán cửa lưới chống muỗi TP Cần Thơ

Cô gái hé lộ điểm bán cửa lưới chống muỗi tại Hà Đông tốt

Cô gái trẻ hé lộ cửa hàng cửa lưới chống muỗi tại TP Đà Nẵng

Cô gái tiết lộ điểm bán cửa lưới chống muỗi thị xã Sơn Tây tốt nhất

Avi said...

Nice Article…
Really appreciate your work
Birthday Wishes for Girlfriend

ajay said...


thanks for sharing us


institute of marketing said...

Wonderful read and would Bookmark it. Thanks
Digital marketing courses in Bangalore


Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.

And also those who are looking for
Web Designing Training Institute in Chennai
SEO Training Institute in Chennai
Photoshop Training Institute in Chennai
PHP & Mysql Training Institute in Chennai
Android Training Institute in Chennai

raja yadav said...

google adsense account ko disable hone se kaise bachaye

Just Digital Web said...

Just Digital Web is Best Digital Marketing Company in Jamnagar

Soft Bazzar said...

GB WhatsApp APK Download

Sandeep Maheshwari Quotes

You can Eat Mouth Watering Surti Food

South Indian Actress Kriti Kharbanda Wiki

2019 Earth Day Quotes – In 2019 Earth Day Come to 22 April.

CamsLens is Best Camera Lens Review Blog

If you find Best Earphones Under 500 Budget

Best Mesothilum Tips

I love Beautiful Mehndi Designs for Boys

Basudev said...

Nice post
Download Modded Apps said...

Cams said...

Learn more about Wide angle lens is helpful for beginners

Chiến SEOCAM said...

Váš príspevok je pre mňa veľmi dôležitý. Ďakujem a prajem vám šťastie.

Lều xông hơi khô

Túi xông hơi cá nhân

Lều xông hơi hồng ngoại

Mua lều xông hơi

Don Browning said...

Much gratefulness to you such an astonishing total for displaying this dazzling article to us.Will remain related with your online diaries for the future posts.manifestation magic review
yeast infection no more review
Combat Fighter System Review
my shed plans pro review
Ez Battery Reconditioning
The Nomad Power System
heal kidney disease review

Designer said...

Có lẽ cần phải trải qua tuổi thanh xuân( Phương pháp học toán tư duy ) mới có thể hiểu được tuổi xuân là khoảng thời gian ta( dạy trẻ học toán tư duy ) sống ích kỷ biết chừng nào. Có lúc nghĩ, sở dĩ tình yêu cần phải đi một vòng tròn lớn như vậy, phải trả một cái giá quá đắt như thế,( Học toán tư duy có tốt không ) là bởi vì nó đến không đúng thời điểm. Khi có được( Toán mầm non ) tình yêu, chúng ta thiếu đi trí tuệ. Đợi đến khi( Bé học đếm số ) có đủ trí tuệ, chúng ta đã không còn sức lực để yêu một tình yêu thuần khiết nữa.

Anonymous said...

static gk quiz

Anonymous said...

know Deepak Rawat IAS

Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

Service Center Blackberry said...


Chiến SEOCAM said...

Makalenizi okumak için çok mutlu ve mutlu. Paylaştığın için teşekkür ederim.

Lều xông hơi khô

Túi xông hơi cá nhân

Lều xông hơi hồng ngoại

Mua lều xông hơi

Lottery Winning Tips said...

Thanks for sharing valuable information.It will help everyone.keep Post.
Kerala Lottery guessing

Admin said...



Priyanka said...

Attend The Digital Marketing courses in bangalore From ExcelR. Practical Digital Marketing courses in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Digital Marketing courses in bangalore.
Digital Marketing Courses in Bangalore