start	end	text
0	400	Okay.
400	1640	Can you guys hear me okay?
2120	2780	All right.
2780	3120	Great.
3120	3740	Thanks.
4980	7920	So this talk is also meant to be, if you want, interactive.
7980	10500	So if you have a question, I'm happy to stop.
10880	13320	It's definitely an introductory talk.
13320	15660	So if something's confusing, put your hands up.
15660	20160	And if I feel like I can't answer it now, I'll be like, let's answer it later.
20400	23620	So welcome to the talk on learning Nix the fun way.
24380	26760	First off, this talk can be found online.
26920	28680	There's the URI.
29580	32680	And this is a reprieve of a talk I gave at Planet Nix.
32680	35920	So if you heard that talk, nothing's really changed.
36300	40060	You know, my goal is to just spread Nix and some of the fun parts of it.
40060	44020	A lot of it can feel overwhelming and academic.
45080	48500	I welcome improvements, edits, and corrections.
49100	50180	And yeah.
52080	52760	Okay.
52760	55560	So just really quick, let me introduce myself.
55560	56340	I'm Fareed.
56340	58100	I currently work at Confluent.
58160	60600	They're like known for Apache Kafka.
60700	62420	That's some software project.
62420	63820	I don't work on that.
63820	66800	I work on build systems and automation and developer productivity.
67060	69820	I've been using Nix in a variety of capacities.
70140	75480	For a while now, I've presented at a bunch of NixCons, North America, and Europe.
75880	83040	And I had the pleasure of bringing Nix into Google through an acquisition at Looker.
83140	85240	I'm very bullish on Nix.
85240	88400	It's been a source of mostly joy.
88400	93020	And I had the fortunate experience of leveraging it to complete a PhD.
93020	95040	So this thing says nearly complete.
95040	97920	I completed my PhD as of June.
97980	99780	So yeah, I'm very happy about that.
104700	110080	Okay, so that was like the alternate working title for this talk.
110080	113200	And I want to share effectively why I love Nix.
113200	114740	Love is a strong word.
114800	123420	But Nix has brought me a lot of joy, simplification of my software life, and ultimately love.
123720	128040	You know, sometimes though, it's felt like learning it can be challenging.
128440	132540	And I find it's really important to find like the purpose of what I'm learning and why.
133720	136840	So ultimately, like why I love Nix, and you should too.
140860	143140	I've given a lot of talks about Nix.
143180	149300	I've given talks to meetup groups, friends, plenty internally at the companies I've been a part of.
149380	157460	And this is kind of a photo of in my mind, when I'm thinking about wanting to talk about Nix, talking to others.
157460	168120	It's like a noble pursuit where I'm imparting knowledge to my apostles about like what they're missing out on, and they're gaining some infinite wisdom.
168120	171740	And it's somewhat academic, the whole thing.
173820	180540	I tend to always begin these talks, covering that, like Nix as an idea started from a PhD thesis.
180660	184540	And for some reason, I can't not mention the PhD thesis.
184540	189860	I think it's like a law when you do a Nix con talk, or Nix talk, sorry, rather.
190780	194480	And the PhD was the purely functional software deployment model.
194540	196780	And it was in 2006.
198000	205560	In retrospect, when you start off with a thesis for a talk, this slide better depicts how the talks have gone.
205560	210920	People in attendance are totally bored by the nuance and complexity of what I'm talking about.
213140	225880	At worst, I've immediately turned off potential Nix, new Nix enthusiasts by immediately jumping in to like an academic and incredibly verbose vocabulary about the ideas and technologies about Nix.
225940	228220	And I've just lost a willing convert.
232020	235180	Okay, so the motivation of this talk is like, I want to do it different.
238520	240540	Let's learn Nix the fun way.
240580	243360	You should be sold why Nix, what's fun about it?
243360	244880	Why do you want to use it?
245320	250100	You know , it's going to be a little bit of a crash course, but you should come away with like, wow, there's some benefits here.
250100	251700	And it's fun to use.
251860	257020	I want to impart the benefits of using Nix by what I believe to be learning Nix the fun way.
261300	262800	Okay, let's begin.
262800	266640	So there's gonna be some code samples, I apologize, can't get around it.
266640	267780	It's a code thing.
268160	270340	Let's begin with something seemingly innocuous.
270340	273460	This is a script to determine the IP of my current machine.
273680	275320	Looks pretty standard.
275360	281060	I'm hoping most of the people here are probably reminiscent of a lot of scripts that might have looked like this or written themselves.
281300	283360	And it stitches a few programs together.
283360	290180	So we have bash, curl, jq is this tool, if you're not familiar with it, it processes JSON, and you can pull things out of it.
291700	298240	So I run it, and I get the IP of my machine that I'm running from, or the gateway or whatever.
301820	306820	Here's what rewriting that same script in the most raw form of Nix would look like.
306820	309680	Oh, God, you're scared, you wouldn't write this.
309840	311900	I know I just showed you a new language.
311900	314160	But just ignore that for a second.
314880	320720	The only takeaway is it looks like okay, if you squint, it's new, it's scary.
320720	324720	But if you're programming, it should look like JSON.
324900	329440	You know, it looks like other languages on the face of it.
329440	332600	So there, it's not like totally out there.
334980	337180	That's the important part of this recipe.
337800	341200	Okay, there's like, it looks to like the script we wrote before.
341200	344340	Okay, so there's like a direct mapping, I'm pretty happy here.
348050	350890	But of course, that's not what you'd actually write in Nix.
351050	355330	The following is a little bit more idiomatic, and a lot more terse.
355730	360190	And right now we're getting a lot closer to the original script we'd written.
362170	366950	So this is how you'd write that first script in Nix.
367170	371070	And I'll show you like why you'd want to do that anyways.
371130	380650	So for those that might want to follow along now or later, it's important to keep note of this commit value and that I'm building this on an AMD 64 Linux machine.
386090	392570	Okay, so Nix, I wrote this like thing that I'm telling you you should do for this script.
392870	393490	Why?
393490	395010	Well, let's see, let's build it.
396070	398510	Okay, you get some output.
399110	401110	It has the name, what is my IP?
401110	402430	Okay, that's cool.
403930	409630	Inside of this path is a script, and it's that script we ended up creating.
411790	415610	We can execute that script, and it works as we expect.
415610	417050	That's the original IP.
417050	419070	We're functionally identical so far.
419070	419990	So far so good.
419990	421830	I didn't lose functionality.
422170	423410	This is great.
426310	430310	Okay, but why did we go through this whole thing?
430310	434150	Why did I make you learn a superset of JSON basically?
434150	435750	Why do we even need Nix?
435770	437070	What's this file?
437070	438090	What does it look like?
439950	451030	Okay, so the output of the script actually looks really similar to what we might have written offhand at first, except everything seems to be prefixed with this really long string.
451870	457650	So you can see like everything has a Nix store or something in front of it, but for the most part everything's identical.
460640	466960	Okay, although I promised not to get too academic into Nix, this part is important.
468300	477980	What is that garbled string at the front, like ahead of our build script, and similarly found within the script before each of the tools?
481340	488240	Well, Nix has a handy command, and it could show us what does that script, like what does that prefix mean?
488260	491460	So you could write Nix derivation show and give it the path.
494980	499860	Okay, so the whole purpose of Nix is to kind of build this file.
499860	505100	Why did we go through all that setup of writing that recipe?
505180	510860	It's ultimately to produce this, and it's the recipe of what it takes to build our IP script.
513080	514900	It has the build command.
516740	521420	It has the text of the script, which may be also the source code.
523280	531880	It has the input tools we needed, so our script needed bash, jq, curl, for some reason this thing called standard end.
534300	541100	It also has the system we're running on, and as well as the architecture and operating system.
541420	548360	So all of this is hashed together to give you that garbled string at the absolute top.
550520	570260	So that's actually a really mind-bending property, because when you see that garbled string, that means we actually have incredibly strong guarantees about our build recipe, because the build recipe includes other garbled strings that represent other garbled strings,
570260	575800	that represent other garbled strings, and so forth and so forth, down to the source code.
577860	584600	There's a level of recursion here that means repeatedly seeing this hash is an incredibly strong property of reproducibility.
584600	601380	You can train your eye, if I see the same string, I have this incredibly strong property about the whole tree of software, whatever the root of what you're building is, is identical down to the architecture, source code, tools, and then recursively at each of those levels.
605240	611400	In Nix, you spend a lot of time wiring things up to basically model things as a graph.
611400	618400	Turns out, once you have a graph, everything you might want to do in software is a permutation or transformation of graphs.
618400	619680	That's all we're doing.
620540	629000	So I built the thing in Nix, I can now have it visualized as this graph, and Nix gives me a handy command to do so.
629000	630540	And here is that graph.
631420	636300	This is the full dependency graph of that seemingly innocuous script.
636300	646920	So in software today, we are taking on so much implicit dependencies we don't know about, and Nix makes those extremely evident and explicit.
648380	656140	So, I don't know, we see jq and for some reason there's krb5 and then that's Kerberos.
656140	662000	I don't know why, you know, to build libcurl, I'm sure, because it has a bunch of dependencies and on and on.
665100	667960	Okay, so here's another Nix snippet.
668060	673100	Again, ignore the actual syntax, but know that we're defining here a shell environment.
673100	679280	So you're working on a project, and you want to define a set of tools to build your project file, to build your project.
681440	685020	And remember that garbled string at the bottom.
685880	691080	We import our same script as I did before and we add it into our shell.
691080	694820	So I'm saying I have a shell and I want to use this tool.
695900	697600	Then you can enter the shell.
703020	705820	Okay, so you can Nix shell and you enter the shell.
708660	712060	I see the little shell hook I did just to show you we're entering a shell.
714900	722760	So I can say my path now contains what is my IP, so I can say which on it.
723780	728580	And it'll find the same path of the binary.
728740	735460	And more importantly, it finds it at the exact same prefix that I had built earlier.
735460	741500	So now I'm taking that exact same binary and moving it into an environment for a developer.
741980	752060	And I know that that developer has the exact same binary and all its dependencies with an extremely strong invariant.
753960	759660	And it's also good it runs and works as we expect, because if it didn't, that would be a big waste of time.
762670	763630	Boom.
763730	765210	To me, this was huge.
765210	767810	I was like, did we realize what we just did?
768630	773970	We just created a simple way to bring reproducible developer environments for projects.
773970	785570	You don't need to give a docker file, have people enter namespaces, network namespaces, volume mounts, you know, doing non-reproducible steps.
785570	791730	This is a way to be natively giving reproducible packages across individual developer projects.
793810	796790	And you could easily validate it thanks to that prefix hash.
800860	802620	But this goes even further.
802620	804860	We actually know everything about our code now.
805940	808320	We can do binary or source deployments.
808320	822960	Again, when we have that graph, we can take that graph and move all the pieces of that graph, which represent files on your machine, onto another machine, and we like to colloquially call that a deployment.
825560	828440	So, Nix makes this really simple.
828720	832280	We simply copy those paths from one machine to another.
833160	838620	And they don't even need Nix installed, because you're just moving it from a folder to a folder.
840160	841100	Boom.
841200	842200	Deployments.
842280	847380	I don't need some crazy fancy machinery or whatever to get perfect reproductive deployments.
847380	848320	Yeah, question.
852150	857050	Yeah, the question was, will this only work if you're on the same architecture and operating system?
857050	857630	That is correct.
857630	860910	Unless you have bin format and you're doing emulation.
860970	868090	But you can build on one machine for the target host, because you can cross-compile relatively easily in Nix.
868490	878590	So, the idea, though, ultimately I want to get across is when you have a graph of things, you just move the graph of things to the machine and you know it's perfectly going to work.
878590	884790	I don't know how many of us have ever been like, I built something here locally and I installed it there.
884790	891470	Oh wait, it doesn't have libssl or it has libssl at 1.0 and not 1.1.
891610	901210	You've captured everything down to libc, like the fundamental library in Linux, and you move that over and you know everything's going to work perfectly.
905880	907360	So, here's one such command.
907360	911440	There's a couple ways to do it in Nix to copy the graph we saw earlier to another machine.
911440	917100	In this example, we're leveraging Nix copy, but you could do something as dumb as scp or rsync.
917480	920800	I didn't mean to call you dumb if that's what you use, I just meant simpler.
922840	927560	So, the items are copied over and let's log into this machine.
931040	938380	And I ran the path and notice it's the exact same path that is present there and I run it.
938380	940260	So, it's that same prefix string.
940660	949960	So, that means for it to have run, because it points to other very specific paths, all those paths were there on that other machine and it all works and wires and runs.
949960	953020	So, I moved that whole graph over.
955910	966930	Wow, we just did a deployment of not only our code, which was the script, but all of its necessary dependencies, such as curl, jq, and bash.
967570	969290	This blows my mind.
969450	979370	Like I said, I can't tell you the number of times in practice I had to coordinate the runtime needed on the host for some base library not being present or whatever.
980290	982490	It simplifies it a lot.
985140	990480	Okay, well maybe that's a bit aggressive for deployments and you've already drank the Kool-Aid of Kubernetes.
993180	1003340	No problem, since we know the graph and again everything is merely a transformation of a graph, we can turn that into an image.
1007100	1013740	So, here's another snippet, and again ignore the syntax, but know this is creating an OCI-compliant image.
1018410	1022390	We set the commands of our image to be our whatismyip package.
1025580	1030940	And just like that... next slide... so let's go build that image.
1033580	1038300	So, the output of it is a tarball, because an OCI image is a tarball.
1040500	1049340	We can load that tarball into docker daemon as normal, or you could do this through whatever the you know a registry or whatnot.
1051460	1057120	Okay, I could run the tarball, or run the image effectively, and it works as we'd expect.
1057120	1061100	I get the IP, because the the command was the IP.
1061100	1072540	I can inspect the image, and we yet again see that the same prefix hash on our package for the command to execute is there.
1072540	1080820	Again, this continues to give me those warm fuzzy feelings in the inside that I've now produced an image that I'm deploying, and it has the same prefix hash.
1086390	1087470	Cool.
1087650	1090770	So, we have Nix and docker integration perfectly.
1090830	1094550	The image produced only has the files exactly necessary to run the tool.
1094690	1098450	So, I've also done effectively distroless, which is all the rage right right now.
1098450	1101910	So, you don't have any other base libraries running around.
1101910	1110630	You've built an image and a file system exactly with what's needed to run the command and or the application therein.
1112590	1114390	And honestly, you could do the same.
1114390	1115930	There's plenty of other slides here.
1115930	1119090	I'm not going to show you every variant, but everything's just a permutation.
1119090	1120730	We did OCI images.
1120910	1123930	You could do a random assembly tarball.
1123990	1125710	You could do a self-extracting zip.
1125710	1126670	You could do RPM.
1126670	1127610	You could do a Debian.
1127610	1128610	You could do a flatback.
1128610	1130110	You could do an app image.
1130270	1134250	Again, you've spent all the time setting up your graph.
1137130	1141130	Well, Linux distribution itself is actually just a collection of packages.
1141250	1145750	It's a permutation and a collection of software files.
1145750	1156820	It turns out, you could also build an OS out of Nix itself, and that's what NixOS is.
1158780	1165380	Again, oh, so again, ignore the... this is like a very basic config setup for a NixOS image.
1165440	1172700	You don't have to care about the syntax, but what's important is we're setting one up, and I'm importing that same package.
1173300	1178740	And I'm telling the OS image, include this as a default package within the image.
1180840	1184480	And I'm starting it as a VM, just so I could demo it.
1185280	1189240	But you can build an ISO or whatever and then boot into it.
1192350	1195230	Okay, so like everything else, we build it.
1195230	1196930	So I'm building my VM.
1197570	1201590	Again, keeping in mind, we want to look for this prefix hash.
1202890	1206010	So we get an output directory.
1207290	1211430	Okay, and within that output directory is a run script to run KMU.
1211430	1217830	I'm going to run my OS with some fun parameters, just so I can see stuff in the TTY.
1221100	1223300	It starts to log in into NixOS.
1223400	1224680	I log in.
1226760	1231860	And again, I execute which is which, what is my IP.
1231860	1233420	Looks like I found it.
1233420	1237180	It's on some other symlinks path, but then I say real path.
1237180	1242500	I get to the underlying symlink, and I see that warm, fuzzy feeling again.
1242500	1244320	I see that same prefix hash.
1244320	1255920	So now I've built an OS distribution that has my software package, perfectly reproducible with all its dependencies and that same OS.
1256000	1257360	I feel happy inside.
1257360	1258360	I feel joy.
1259460	1264240	And yeah, and it works, because we ran it.
1265280	1266280	Oh, that was it.
1266280	1267300	Yeah, thank you.
1272190	1272790	Questions?
1272790	1274270	Yeah, and I'll try to repeat them.
1283600	1287740	I think the question was, can you do a NixOS module?
1293290	1300210	Yeah, I mean, the question is, can you do a NixOS module to configure a Docker image?
1300830	1302350	Sure, I think you could.
1302350	1306430	A module is just another level of abstraction to set up code.
1306430	1323430	And I could see, if you have a very consistent pattern for building your OCI images, you might not want them to have to write something closer to a Docker file and write some other higher level thing that you end up emitting the OCI tarball for.
1323430	1324290	Totally.
1332530	1335410	Okay, the question is, where is jq and curl coming from?
1335410	1336630	That's a good question.
1336690	1341050	So it would be incredibly tedious to have to rewrite the world yourself.
1341270	1344210	Thankfully, there's a large community that has already done that for you.
1344390	1347350	And that is called Nix Packages.
1347350	1355770	So there is already a huge package set where authors have already taken the painstaking time to do all those dependency edges.
1355770	1357930	So you don't have to always start from scratch.
1360710	1367290	And it's also the world's largest package set, by at least one order of magnitude, I think.
1375710	1378950	Yeah, the question is, there's Nix Package Manager and NixOS.
1379690	1381950	Naming in Nix is hard.
1382290	1383750	They don't do a very good job.
1383750	1391930	I think my takeaway for the talk is Nix just says, tell me what your graph is.
1392030	1394850	And once I have your graph, I can do whatever I want with it.
1394850	1398490	So there's Nix Package Manager that says, download the graph.
1398670	1403890	Something like, you know, apt-get or yum or whatever.
1403890	1409150	So it says, you have a name, download the graph for me and let me install this package locally.
1409510	1417150	NixOS says, here's a bunch of packages, turn them into an OS distribution, and let me run that.
1417150	1424370	So they're all just different variants of, here are different graphs, put them together and make them a thing.
1431760	1433440	Yeah, what is Nix Shell?
1433860	1444060	So Nix Shell, just to get a bit more pedantic, will download the packages, but it puts them in this separate Nix store thing .
1444360	1446300	And then it just sets your path.
1446400	1451160	So in Linux, how things are found is they do a search along your path.
1451320	1463900	So the moment you do a Nix Shell, it drops you into a new bash environment with your path set with some new... your path environment variable set with some new directories.
1463900	1465020	So they're found.
1465040	1469560	So it's like, ephemeral to that single session.
1469560	1474820	And what's great is there's no container, no namespaces, no cgroups, nothing.
1474820	1478620	So you're like fully native within the host.
1491290	1494510	Sorry, the question is, does this work for alternate architectures?
1496390	1497650	Things work.
1498290	1500970	Ultimately, you're building third-party software.
1501110	1509250	So the asterisk here is, do things work for alternate architectures, is going to be, does that software build anyways reliably for those alternate architectures?
1510190	1511770	If you're on your own.
1511770	1524850	The other benefit is, if using this large package set, authors have taken the time, and many of them are here, they're running RISC-V or cross-compiling, and they've submitted patches to make them work for alternate architectures.
1524850	1528170	But fundamentally, Nix is architecture agnostic.
1560300	1574700	Because Nix is this recursive hash calculation, a cryptographic hash of your stuff, if I changed even a comment in my source code, I went back to that script, what is my IP, and I put a comment.
1574700	1577960	I functionally didn't change the thing.
1579820	1590920	Asterisk will get a different hash, because when it goes to build this big recipe file, so this is the recipe file that says, Nix, this is how you build this thing.
1591540	1601700	It had here the text, that's the contents of the script, like the contents of the script to change, and then therefore the hash changed.
1601940	1603380	Again, this is recursive.
1603380	1616260	That meant if I wanted to use a different curl, and it only changed because of a comment, its hash would change, and now my hash would change, because I have a different hash in my hash.
1617640	1622440	So I get a totally different output, and that's okay, they live side by side.
1623000	1629460	The beauty of having hashes in the name is now they're not colliding spaces in the directory.
1629460	1632800	They're just like sitting side by side in a big directory tree.
1633620	1636780	If I want to go back, I delete that comment.
1636860	1643280	If I perfectly delete it, and the hash calculates back to the same thing, Nix says, you already got it there, buddy.
1643280	1646400	I'm not going to do it again, and it skips some work.
1649910	1652890	Again, really clear, that's not bit reproducible.
1653290	1655010	Nix can do bit reproduction.
1655390	1656530	This is a bit different.
1656530	1667290	This is pessimistic reproduction, because the source changes always affect the hash.
1671590	1672510	Okay, thank you.
1672510	1673390	I'll be around.
1673390	1676190	If you have more introductory questions, I'm happy to answer.
1676270	1681370	If you have high-level questions, I'm happy to answer, and I hope you learn to love Nix as much as I do.
1681370	1682110	Thank you.
