text
stringlengths
0
2.02k
So Nix as a language evaluates to so-called derivations. These are the instructions how to build a package, and then you can copy those to another machine, and then you can realize them - that's the term, which goes from this derivation into the actual build, which then produces the output.
On the way, you can also, instead of building substitutes - that's the technical term we use when, say, you download a package for this hash, which is then the binary you get, instead of building. That's a kind of pretty nice benefit, I think.
**Break:** \[19:06\]
**Jerod Santo:** So let's say I wanna use Nix to install Firefox, and I type "nix install firefox", or you can tell us what you would type... Or what do I do? And then tell us what Nix would do, and then we'll go from there and talk through what that provides, and why I might wanna do it that way.
**Domen Kožar:** Alright. Well, there's quite a lot going on behind the scenes, so let's go through each step. If you say "nix install firefox", Nix will first of all try to see where you're trying to install Firefox from... And by default, it will use Nix packages, which is the official, one of the sources it can inst...
Then there is a top-level file in Nix Packages called all-packages. You can imagine this -- you know Nix language is kind of like JSON with functions... So in there you'll see a key Firefox, and it will point then to a file, which will import that file.
Inside the Firefox file (firefox.nix, wherever it is), there is a description of how to build Firefox. In the Nix language there is a primitive called derivation, which is kind of like the core of the whole concept. In the Firefox case, it will say -- you know, there's a bunch of dependencies, you have to run make with...
What happens in there is the derivation function gets a bunch of inputs, which you can imagine as like key-value pairs, essentially... And it passes that to a builder, which is some kind of an executable. By default in Nix all the builders are done in Bash, but you can have an executable as a builder, essentially, and ...
This builder, once it will be executed, is run in a sandbox environment; you can imagine this as something like a Docker environment, where it will not have access to the internet, it will be completely isolated from the file system, and so on.
The idea of this sandboxing is, of course, for the build to be reproducible, and only dependent on these inputs. That is one of the core design decisions. And as I've previously mentioned, all these inputs are then calculated -- there is a hash calculate out of these inputs, and this uniquely identifies how this packag...
So the builder will take care of the building part, and this is where I previously talked about evaluation and building separation kicks in. When you will install Firefox, it will find a Firefox file, it will evaluate this first... So it will evaluate the Firefox derivation, and then everything up to the bootstrapping ...
\[24:14\] And the building phase is not that interesting. There's essentially two parts to it. One is that it will use these derivation files to call this builder, as I've mentioned... But before it does that, it will also check with this hash if there exists a binary package for it, and it will substitute it if there ...
How that works is that when the package is built, as I've mentioned, Nix will put it in the /nix/store/, and then hash and the name of the package, and then everything goes in there. And the same for all the dependencies.
So let's assume now that Nix downloaded some binaries as a dependency of Firefox, and then it builds the Firefox. Now it just has a bunch of folders in /nix/store, and now it will link those into something we would call file system hierarchy, which is the standard you're used to in Debian, for example, so /user and /op...
So that's how Nix goes from this global store into an actual file system hierarchy that we're used to. It's one big assembling farm, the way to imagine it... This is then how you go through -- if you install something, it will install all the packages you had before, and then this package on top of it. So it's kind of ...
And the same when you uninstall, it will not remove the Firefox from the /nix/store directory, but it will just create a new profile version without Firefox linked in. And this is very typical in memory management, where you essentially just allocate, and then you garbage-collect when you want to. Nix works in a simila...
So that's the garbage collection bit... But let's go back to installing. Now we have this profile where Firefox was installed in, and Nix will activate it, which means that the specific snapshot of the profile is now the activated one. And these profiles can be stacked one upon another as well, and there is like the us...
**Jerod Santo:** So how does it accomplish that? So I understand completely, because it has /nix/store/unique-hash-firefox, or whatever... I understand how that provides for multiple versions installed on the same system, right? And I can also understand how once you have this ever-adding system where you're just addin...
\[28:15\] It doesn't explain to me the multi-user support. So you said there's profiles... Is everything stored in the Nix store, and then it's just like the profiles are elsewhere, and point to which versions you are using? Or how does it know when it's garbage-collecting that Adam's profile has this Firefox, but my p...
**Domen Kožar:** The easiest way to imagine it is, Debian installation would be one profile, and then you have different profiles in your system. The way Nix stacks those together is - if I understand your question - it will just append the path by the hierarchy of the profiles you have activated. So if you have a user...
**Jerod Santo:** Yeah, that much I understand, but how does it know not to garbage-collect my profile's version of Firefox when you run garbage collect?
**Domen Kožar:** Oh, okay.
**Jerod Santo:** Is there some sort of registry of who's using what, where?
**Domen Kožar:** Yeah, so the profiles are symlinks as well. They're installed in the /nix/wire/profiles, I think... And for each profile there is a name, and then there is something called profile, and then the version of it (1, 2, and so on; you count linearly). And inside there, that's the symlink then to the file s...
So when you run garbage collect, you can do it for the user, or you can do it globally. Oh, yeah, you can pass a link to (if I remember correctly) to the profile you want to garbage-collect, otherwise it will garbage-collect everything. And the way the garbage collection works is you can say "garbage-collect everything...
**Jerod Santo:** Gotcha.
**Adam Stacoviak:** So the profiles essentially bless what's installed, and when you garbage-collect, if it's present in a profile, then it's like "Hey, I'm not gonna touch that, because that's necessary, based upon somebody's profile." That's established, whether it's Jerod's user or my user...
**Domen Kožar:** Exactly.
**Adam Stacoviak:** And then likewise, if Jerod wants to install something that I've already installed, it's not gonna redownload, recompile and rebuild that a second time. It's gonna just use what's already there, which is secure, because you can't go in and change that. It's got that hash, it's already been built... ...
**Domen Kožar:** Exactly, yeah. Nix store is mounted as a read-only, and so on. Nix is allowed to manage it, essentially. So that's the guarantee you get.
**Adam Stacoviak:** So since we compared Nix as analogous to how somebody might be familiar with apt or apt-get - does apt, or apt-get, or Homebrew, are they not in this kind of world where it's reproducible? Is that not a concern in those worlds? Or is that not a scenario? When I run apt-get install git, for example -...
**Domen Kožar:** \[32:09\] Right.
**Adam Stacoviak:** So Nix runs on reproducible builds, and the fact that it makes these builds hashes, secure by nature, because you can prove the complete dependencies, you know what's involved... All that good stuff. And that hash proves it, and that's the way it works by design. How does that compare to apt or apt-...
**Domen Kožar:** Yeah, that's a very good question. I think there are actually two kinds of reproducibility that are usually, I would say, mentioned in the package management world. So Nix does the reproducibility where it goes from source to a binary by ensuring that you always get the same binary, and this is then mi...
On Debian, they have also the reproducibility project, but that is more about the binary output, so that the actual binaries you get are identical each time you build something.
The difference is that in Debian, as far as I know - maybe the infrastructure has changed recently, but it used to be at least the case that when you build something, it will pick up libraries from your system.
Let's say you build Firefox - it will pick up OpenSSL from your system. Now, how this OpenSSL was built is not -- there is no guarantee. Something built it, and of course, if you use Debian to install OpenSSL, then you kind of have this guarantee implicitly on your system. But you could easily swap out OpenSSL with the...
The ways I reasoned about Debian is it's just some kind of a file system state where you installed OpenSSL, and then you install another library that depends on it, and so on. And then you stack these... But yeah, as I've said, there is nothing tracking what's used to install this.
So of course, Debian probably has some servers where they build this in a sandbox environment, and so on... But when you do it locally, you kind of lose that guarantee.
In Nix everything is sandboxed by default, so everyone that's building anything on Nix gets this guarantee, and it's enforced. So yeah, that's the main difference between the two.
**Break:** \[34:46\]
**Jerod Santo:** Domen, one of the things you said at the top, and also you say on Nix ecosystem is a DevOps toolkit; so there's a DevOps focus in what Nix is providing. So not just merely installing Firefox on my local Linux box so I can browse the web, but using this for getting your DevOps, getting your stuff out th...
**Domen Kožar:** That's a great question. Yeah, I don't think there's a definitely answer to it. Essentially, there's all the options.
**Jerod Santo:** The answer is yes...
**Domen Kožar:** The answer is yes, again... \[laughter\] So the way I would say it, at least first compared to Docker, Nix is really good with the configuration and build part. And once you build something, then when you run it, it's just an executable. So Docker is, I would say, complementary to that. Docker provides...
In the NixOS we use systemd since the very early days, so that one kind of manages the whole runtime bit, if you use the OS bit. But if you use containers, then -- there's people who are using Nix to build containers for Nomad, and Kubernetes as well... You can build Docker images with Nix...
I think that's a pretty nice combination as well, because one thing I forgot to mention is in Nix you have two kinds of derivations. One is called fixed output derivation, and the other one is just a derivation, or a dynamic derivation. The dynamic derivation is the one that hashes all the inputs. The fixed output deri...
In the Docker world, this is not the case. If you have a Docker image that downloads something from the internet, essentially if you run it twice and that content changes, there is no guarantee whatsoever that this will -- this will be a completely different image, with a different output.
The reason why people don't really notice that is because Docker Hub has the history of all the images, and people don't usually build them themselves. But yeah, that's where I think Nix shines better, so in this reproducibility aspect... And then Docker for runtime for sure, and all the container stuff we have built r...
**Jerod Santo:** So you're effectively running Nix alongside Docker, or inside of Docker, to do all the package stuff. Is that the way you would use it, together?