1 00:00:00,540 --> 00:00:01,540 All right, cool. 2 00:00:02,080 --> 00:00:02,740 All right. 3 00:00:03,180 --> 00:00:03,980 Hello, guys. 4 00:00:03,980 --> 00:00:04,700 I'm Alex. 5 00:00:04,700 --> 00:00:08,220 This is my first year at DEF CON and also my first time talking about Nix. 6 00:00:08,220 --> 00:00:09,200 So I'm a little nervous. 7 00:00:09,200 --> 00:00:11,280 If I stumble a little bit, bear with me. 8 00:00:11,640 --> 00:00:15,500 And my talk is Zero to Infra in 100 Days, a Nix Speedrun. 9 00:00:15,720 --> 00:00:20,040 And I'm going to start with a little story about how I got into it and then kind of go through this crash course. 10 00:00:20,040 --> 00:00:22,820 And it's funny, because Farid is actually the one that got me into it. 11 00:00:22,820 --> 00:00:25,400 I met him at his younger brother's wedding. 12 00:00:25,400 --> 00:00:27,040 Omar, he's one of my best friends. 13 00:00:27,480 --> 00:00:31,760 And he said, you should come with me to play Nix at scale 22. 14 00:00:31,980 --> 00:00:33,840 And I was like, I don't know what that is. 15 00:00:33,840 --> 00:00:35,240 But I like Linux, and I'm a nerd. 16 00:00:35,240 --> 00:00:36,880 And I have all my home lab stuff. 17 00:00:36,880 --> 00:00:38,640 So I'll just go and try it. 18 00:00:38,900 --> 00:00:40,680 And I was there. 19 00:00:40,680 --> 00:00:43,840 I set up a laptop in four days trying to learn Nix. 20 00:00:43,840 --> 00:00:46,080 Looking back, I didn't really have it. 21 00:00:46,120 --> 00:00:49,280 But I met a bunch of really smart people, and I learned a ton of stuff. 22 00:00:49,280 --> 00:00:50,380 And it was really awesome. 23 00:00:50,380 --> 00:00:52,240 And I'm just super Nix-billed now. 24 00:00:52,240 --> 00:00:54,560 So I'm going to try to get you to be the same way, maybe. 25 00:00:56,720 --> 00:00:59,460 So I'm going to get into it. 26 00:00:59,480 --> 00:01:05,240 My talk is pretty similar to Farid's in terms of like the beginner level. 27 00:01:05,240 --> 00:01:07,260 Mine's going to also kind of be a crash course. 28 00:01:07,260 --> 00:01:10,000 But I'm going to say mine's going to be even less academic. 29 00:01:10,000 --> 00:01:18,200 I'm going to try to give you what you need to know to get started as soon as possible without knowing all the theory behind it. 30 00:01:18,800 --> 00:01:23,040 And you can kind of pick that up as you go once you see the power and you start using it. 31 00:01:24,440 --> 00:01:29,760 So in most of these talks, people are always going to open with like what is Nix? 32 00:01:29,760 --> 00:01:32,400 And I think that that is not the right question. 33 00:01:32,400 --> 00:01:37,800 I think why Nix and showing what it is and how it works for you is more powerful. 34 00:01:37,800 --> 00:01:44,220 Because when people open with these talks, they'll say Nix is a package manager, a language, and an operating system. 35 00:01:44,220 --> 00:01:46,640 And when you say that, it just turns everyone off immediately. 36 00:01:47,540 --> 00:01:49,160 That's three different things. 37 00:01:49,160 --> 00:01:54,580 That's like saying C, Ubuntu, and APT. 38 00:01:54,780 --> 00:01:56,580 That's like what the hell? 39 00:01:57,400 --> 00:02:00,280 But with Nix, why Nix? 40 00:02:00,280 --> 00:02:04,640 It simplifies what you can get out of everything. 41 00:02:04,640 --> 00:02:09,960 So in a traditional sense, I'm going to use like kitchen recipes analogies a lot. 42 00:02:09,960 --> 00:02:12,060 At Planet Nix, somebody made that analogy and I really liked it. 43 00:02:12,060 --> 00:02:13,280 It clicked for me a lot. 44 00:02:13,280 --> 00:02:20,340 So the traditional is like if I'm making a tomato sauce, the traditional way would be like I got to add tomatoes and that's all I'm going to say. 45 00:02:20,360 --> 00:02:26,320 But the Nix sauce is going to say I'm going to add tomatoes from this farm at this acidity, stored at this ripeness level. 46 00:02:26,320 --> 00:02:32,080 And it's going to give you the precise same outputs every time because you are going to give it the same inputs every time. 47 00:02:33,940 --> 00:02:40,300 And actually, I guess, so I also made myself like a little cheat sheet here so I don't mess up, run any commands and stuff. 48 00:02:40,300 --> 00:02:46,380 And my talk is just going to, I'm going to run a lot of stuff live and just show you rather than talking about it. 49 00:02:46,560 --> 00:02:51,340 So just to show you some of the really cool stuff that Nix can do to kind of get you hooked, I'm going to talk about like Nix shell. 50 00:02:51,540 --> 00:03:01,780 So what I'm going to do is something that probably isn't normally allowed where I'm going to make an environment with Python 2 and 3 at the same time. 51 00:03:01,780 --> 00:03:04,020 And when I run this, it's actually going to throw a fit. 52 00:03:04,040 --> 00:03:06,180 And that's good because it's actually Nix protecting us. 53 00:03:06,180 --> 00:03:11,780 It's saying, you know, Python 2 isn't actually marked as like secure anymore. 54 00:03:11,920 --> 00:03:15,080 And we can just get by this by allowing to run insecure. 55 00:03:17,880 --> 00:03:19,880 But what it's going to do is it's going to put me into a shell. 56 00:03:19,880 --> 00:03:21,760 So this is like my own little environment. 57 00:03:21,920 --> 00:03:27,000 And Free talked about this a little bit where it's just referencing store paths. 58 00:03:27,000 --> 00:03:28,300 And I'll talk more about that in a second. 59 00:03:28,300 --> 00:03:32,920 But it's just making path variables for your shell. 60 00:03:32,920 --> 00:03:39,890 So I can run and you can see like I have Python 2.7. 61 00:03:39,890 --> 00:03:45,090 And I can also do the same command for Python 3. 62 00:03:45,090 --> 00:03:46,430 I have 2 and 3 in the same shell. 63 00:03:46,430 --> 00:03:47,790 I can use either one if I want. 64 00:03:49,330 --> 00:03:50,890 So that's pretty cool. 65 00:03:54,780 --> 00:03:56,840 And then a couple more. 66 00:03:56,840 --> 00:04:02,060 You can run things that are packaged by others and put up without actually having it on your system. 67 00:04:02,060 --> 00:04:02,840 And this is super useful. 68 00:04:02,840 --> 00:04:06,200 I use this all the time through a different command like comma. 69 00:04:06,420 --> 00:04:11,200 So just as an example of that, I don't have this. 70 00:04:14,540 --> 00:04:15,260 Yeah. 71 00:04:15,400 --> 00:04:17,860 It's like a little like 2E terminal game. 72 00:04:17,980 --> 00:04:19,140 2048 in terminal. 73 00:04:19,140 --> 00:04:19,840 I don't have it. 74 00:04:19,840 --> 00:04:21,620 But I can just run it. 75 00:04:21,620 --> 00:04:22,500 I can nix run. 76 00:04:22,500 --> 00:04:25,820 And what it's going to do is it's going to grab it from the magic world. 77 00:04:25,820 --> 00:04:28,140 And it's going to just run it on my machine. 78 00:04:28,140 --> 00:04:29,160 I don't have it on my machine. 79 00:04:29,160 --> 00:04:30,840 But now I have it on my machine. 80 00:04:30,840 --> 00:04:32,900 And now I'm playing this game. 81 00:04:34,000 --> 00:04:37,700 So that's kind of like why use Nix. 82 00:04:37,700 --> 00:04:42,680 So those are a couple examples of what it is, how you can use it, why you should care a little bit. 83 00:04:42,860 --> 00:04:45,300 And now this is the whole funny thing. 84 00:04:45,300 --> 00:04:48,440 Why does Nix seem to have such an infamous learning curve? 85 00:04:48,440 --> 00:04:54,560 This is the graph that I found on one of the most popular discourse pages for this is one of the best lectures about Nix. 86 00:04:54,560 --> 00:04:58,480 And it was a 30-minute dense lecture about using the language. 87 00:04:58,860 --> 00:05:01,840 And that one of the people commented this on it. 88 00:05:01,840 --> 00:05:02,720 And I think that makes sense. 89 00:05:02,720 --> 00:05:07,260 It's like the learning curve is infamously super steep and treacherous. 90 00:05:07,260 --> 00:05:09,040 And I don't think it has to be that way. 91 00:05:09,040 --> 00:05:13,460 I don't think you actually need to learn all the theory right now. 92 00:05:13,460 --> 00:05:18,260 I think all you need to know is just a couple commands I'll show you and the environment. 93 00:05:18,260 --> 00:05:21,420 So Nix run, Nix develop, and how do you encode the people shit. 94 00:05:21,420 --> 00:05:22,440 That's all you need to know. 95 00:05:22,640 --> 00:05:25,100 You can be productive today using it. 96 00:05:25,100 --> 00:05:27,620 You don't need to know like you can say that's a car. 97 00:05:27,620 --> 00:05:30,340 You don't need to know about how to put the engine together inside of it yet. 98 00:05:30,340 --> 00:05:32,980 You'll probably be interested and want to learn about that later. 99 00:05:33,080 --> 00:05:39,560 I will get into some of the inner workings because I think it's important for you to have a baseline understanding of how it works. 100 00:05:39,560 --> 00:05:44,280 So you're not just looking in this magical wizard orb and not knowing how the magic works. 101 00:05:44,280 --> 00:05:48,980 But as a general guideline for all of it, don't fight immutability. 102 00:05:48,980 --> 00:05:56,360 That this was the biggest rule for me to like start using Nix without fighting myself on it. 103 00:05:56,360 --> 00:06:03,280 So one of the first mistakes I made was I needed to make an alias in my shell. 104 00:06:03,280 --> 00:06:05,660 And I set my shell up with Nix. 105 00:06:05,660 --> 00:06:09,140 So I build it and it gets put on my machine in the state that it is. 106 00:06:09,140 --> 00:06:14,680 And I went into my ZSHRC and I added the alias and it worked for a while. 107 00:06:14,680 --> 00:06:16,180 But the next time I rebuilt, it was gone. 108 00:06:16,180 --> 00:06:18,320 Because it rebuilds it every time. 109 00:06:18,320 --> 00:06:24,720 So you're not thinking of, you're not opening settings menus and trying to find the setting and setting it there. 110 00:06:24,720 --> 00:06:27,180 You're gonna go into the configuration file for it. 111 00:06:27,180 --> 00:06:32,340 You're gonna set the option there and just rebuild to a state where that is the state of things. 112 00:06:32,480 --> 00:06:38,940 You'll avoid most of the roadblocks, I think, to just getting started immediately with a Nix system just by following this rule. 113 00:06:38,940 --> 00:06:40,020 Just don't fight immutability. 114 00:06:40,020 --> 00:06:43,320 Keep in mind, like, I'm not following steps. 115 00:06:43,320 --> 00:06:50,460 And this gets into the whole notion of imperative versus declarative, which I'm gonna get into like what the heck does that even mean? 116 00:06:50,640 --> 00:07:00,200 But like manually going and doing a setting would be something that's imperative and just doing it in your configuration and then rebuilding, that's gonna be declarative. 117 00:07:00,640 --> 00:07:03,920 So declarative versus imperative, like, what the heck does this even mean? 118 00:07:03,920 --> 00:07:07,700 So this is like the Wikipedia definition for it. 119 00:07:08,200 --> 00:07:11,500 I'm not gonna read it because this is like my crash course talk. 120 00:07:11,500 --> 00:07:12,680 Like, who cares? 121 00:07:12,740 --> 00:07:14,120 This is my analogy. 122 00:07:14,720 --> 00:07:16,860 For imperative, I want a sandwich. 123 00:07:17,140 --> 00:07:18,320 Do I order it? 124 00:07:18,320 --> 00:07:19,060 Do I make it? 125 00:07:19,060 --> 00:07:21,540 I need to do XYZ to get my sandwich somehow. 126 00:07:21,540 --> 00:07:22,400 Okay, I'm gonna make it. 127 00:07:22,400 --> 00:07:25,900 I'm gonna, like, I'm gonna get the bread, I'm gonna put the meat on it, mustard, mayo, blah, blah, blah. 128 00:07:25,900 --> 00:07:28,440 I did all these things and now I've got my sandwich. 129 00:07:29,020 --> 00:07:34,440 So in a declarative approach, all I do is I want a sandwich, I declare I have a sandwich. 130 00:07:34,440 --> 00:07:36,160 And now I just have a sandwich. 131 00:07:36,380 --> 00:07:40,300 All the stuff to get to the sandwich, I don't care about. 132 00:07:40,380 --> 00:07:42,180 That can be, Nix can do that for me. 133 00:07:42,180 --> 00:07:43,980 Nix can be my sandwich guy. 134 00:07:44,240 --> 00:07:45,780 But me, I have the sandwich. 135 00:07:45,780 --> 00:07:47,120 I declare I have it, so now I have it. 136 00:07:47,120 --> 00:07:48,420 That's my analogy. 137 00:07:48,420 --> 00:07:51,440 And this is like the whole food thing I'm gonna keep going into more. 138 00:07:53,780 --> 00:07:54,420 Yeah. 139 00:07:54,900 --> 00:07:58,320 So this is like an example of setting up a web server imperatively. 140 00:07:58,320 --> 00:08:00,380 You gotta run a crap load of commands. 141 00:08:00,840 --> 00:08:10,140 You're running curl commands, you're adding PPAs, you gotta add systemd services, you gotta add a user for it, you're running all these commands one at a time, one after the other. 142 00:08:10,180 --> 00:08:16,340 If one of those links is broken, like, stuff's busted, and then by the end of it, like, what did you even install? 143 00:08:17,140 --> 00:08:18,740 Like, can you do this again? 144 00:08:18,740 --> 00:08:22,260 Like, you can make a script, but what if something else is broken? 145 00:08:22,380 --> 00:08:24,980 And this is like my, this is the point of it. 146 00:08:25,460 --> 00:08:28,160 Do you even know all the stuff on your system, on your computer? 147 00:08:28,160 --> 00:08:30,780 Do you actually know all the stuff installed on your computer right now? 148 00:08:30,780 --> 00:08:31,740 I bet you don't. 149 00:08:31,740 --> 00:08:34,460 Like, unless you use Nix, then you do. 150 00:08:34,460 --> 00:08:39,960 But, like, you just, like, it's following, like, a medium article to set something up. 151 00:08:39,960 --> 00:08:43,640 You copy a link, and you run some curl command, and it puts something on your computer. 152 00:08:43,640 --> 00:08:45,180 Two months later, you don't even remember doing that. 153 00:08:45,180 --> 00:08:46,280 It's on there somewhere. 154 00:08:46,280 --> 00:08:49,440 Maybe you're, like, refreshing your repository, but you have no idea. 155 00:08:49,540 --> 00:09:02,620 The declarative approach to this, this is the same thing set up, but with Nix, and I'm going to break this down a little bit in a second, but you're declaring each service, and this is going to give you the same results every time, no matter what. 156 00:09:02,720 --> 00:09:11,760 And we're doing everything from setting up the services, like Nginx, PostgreSQL, and the systemd service, and even setting up the user to run this. 157 00:09:11,760 --> 00:09:15,440 And so, when I look at this, this can be kind of, like, intimidating to look at at first. 158 00:09:15,440 --> 00:09:19,700 It kind of, there's a lot of brackets, and this is how I look at it. 159 00:09:19,700 --> 00:09:20,540 I break it down. 160 00:09:20,540 --> 00:09:21,480 I do my scribble. 161 00:09:21,600 --> 00:09:33,020 Just look for the, like, top-level attributes, the curlies, and when you actually look inside each of them as, like, a chunk, it actually makes it a lot easier to understand what's even going on. 162 00:09:33,020 --> 00:09:34,780 So, like, just look at the first one. 163 00:09:34,980 --> 00:09:36,520 It's a service, Nginx. 164 00:09:36,520 --> 00:09:39,920 We're enabling it, and then we're just doing settings for that specifically. 165 00:09:39,920 --> 00:09:40,840 Same thing for the other ones. 166 00:09:40,840 --> 00:09:45,980 The systemd one, that's the same syntax you're writing to, like, write your own systemd service. 167 00:09:45,980 --> 00:09:48,880 You're just doing it through Nginx, and it's going to end up on your system after you rebuild it. 168 00:09:48,880 --> 00:09:50,080 Even the same thing with the user. 169 00:09:50,080 --> 00:09:52,600 I'm making a user called MyApp. 170 00:09:55,460 --> 00:10:08,380 So, DevShells and Durand are your gateway drug into getting into Nginx, and if you take nothing else away from this talk, it's just start using DevShells to do development of whatever you're working on. 171 00:10:08,380 --> 00:10:14,140 It makes stuff super easy, and it makes collaborating with other people really, really easy, too. 172 00:10:14,420 --> 00:10:19,160 Your directory becomes your full custom environment. 173 00:10:20,380 --> 00:10:28,060 So, just to show this, I, like, and this is all the stuff you need to do a Durand and DevShell. 174 00:10:28,060 --> 00:10:39,820 You need the envrc, which is for Durand, and flakes, which I'll get into a little bit later, but just as a top level, that's just the thing packaged reproducibly monolith. 175 00:10:39,820 --> 00:10:55,920 So, just to show this, I did a web server in Rust, super simple, and I'm going to show, like, this is the flake, the whole thing, and, like, this kind of ties into yoinking other people's stuff. 176 00:10:55,920 --> 00:11:10,220 You can yoink this and replace it with the stuff you need for your project, but all I'm doing is I need Rusty, I need cargo, I need Rust analyzer, and I'm spitting this stuff out when I enter it, and I have the inputs, and you'll see, like, once I enter the shell, 177 00:11:10,220 --> 00:11:11,100 you see all this stuff. 178 00:11:11,100 --> 00:11:15,420 Like, I loaded it, I loaded the flake, and then it spits that stuff out that was inside the flake. 179 00:11:16,240 --> 00:11:20,580 So, if I exit now, if I go out of this, you see it says unloading. 180 00:11:20,580 --> 00:11:22,600 If I run Rust C, I don't have it. 181 00:11:22,600 --> 00:11:23,680 It's not on my system. 182 00:11:23,680 --> 00:11:29,600 But as soon as I just enter that DevShell, I have Rust C now, magically. 183 00:11:29,880 --> 00:11:32,740 I'm inside of this new environment. 184 00:11:32,780 --> 00:11:36,440 And the same thing, I can just do cargo run. 185 00:11:36,440 --> 00:11:41,300 All my stuff was together, and now I just, like, I have this little private web page below from Nix. 186 00:11:41,300 --> 00:11:42,220 It just works. 187 00:11:42,220 --> 00:11:43,940 I have it on the DevShell. 188 00:11:43,940 --> 00:11:50,780 And for a lot of the projects I've been working on, I've repackaged them to use DevShells, and it makes sharing the setup super easy. 189 00:11:50,780 --> 00:12:03,040 Like, if you're trying to collaborate on something with someone, they can clone your project, and then they just enter the DevShell, as long as they have Nix on their system, and they have the exact same environment that you have for working on something. 190 00:12:03,040 --> 00:12:10,080 It makes it really easy to, like, have the same stuff on all the machines for your whole team, all working on the same stuff. 191 00:12:15,760 --> 00:12:16,600 Okay. 192 00:12:16,600 --> 00:12:21,700 So now is my slightly dense part of this, like, lecture part. 193 00:12:21,700 --> 00:12:23,300 This is my speedrun lecture. 194 00:12:24,000 --> 00:12:25,580 How does the stuff actually work? 195 00:12:25,580 --> 00:12:34,200 I think it's important for you to have some base knowledge of what's going on under the hood, so it doesn't all seem like some mystical orb that you're pondering. 196 00:12:34,200 --> 00:12:35,180 You're like a wizard. 197 00:12:35,360 --> 00:12:47,540 So I think all you really need to know to get jump-started and feel not confused by what you're doing every day is derivations, what's the store, what is Nix packages, and what are flakes. 198 00:12:47,540 --> 00:12:53,160 And I'm going to go through, like, a blitz explanation of all these, and I'm going to give some examples as I go. 199 00:12:53,400 --> 00:12:58,620 So basically, and this goes back to my food thing, derivations, how stuff gets built. 200 00:12:58,620 --> 00:13:01,080 A derivation is a recipe to do something. 201 00:13:01,080 --> 00:13:03,940 For example, let's do the GNU hello program. 202 00:13:03,940 --> 00:13:05,720 All it does is just print out hello world. 203 00:13:05,980 --> 00:13:10,960 And this is basically, this is the stuff that's inside of that derivation. 204 00:13:10,960 --> 00:13:16,840 It's going to say, I need this hello tar.gz source code stuff, I need GCC, and I need make. 205 00:13:16,840 --> 00:13:18,460 And these are the recipe inputs. 206 00:13:18,500 --> 00:13:24,120 And if I do this, if I put all these together, I'm going to get this same output every time. 207 00:13:25,020 --> 00:13:28,020 And just to show this, 208 00:13:36,880 --> 00:13:40,020 this is what a derivation actually looks like. 209 00:13:40,100 --> 00:13:45,940 We showed this a little bit earlier, but so you don't actually need to read too far into it. 210 00:13:45,940 --> 00:13:47,280 It looks like JSON. 211 00:13:47,280 --> 00:13:48,120 It is JSON. 212 00:13:48,120 --> 00:13:57,580 And you can see it's spitting out that hash stuff and the output where it's going to end up on the machine. 213 00:13:57,940 --> 00:13:59,480 But this is what's inside of it. 214 00:13:59,480 --> 00:14:00,080 It's not scary. 215 00:14:00,080 --> 00:14:00,960 It's just JSON. 216 00:14:00,960 --> 00:14:03,180 It's showing all the inputs. 217 00:14:03,180 --> 00:14:06,320 You can see this is the, these are the inputs. 218 00:14:06,320 --> 00:14:07,640 This is how it's being built. 219 00:14:07,640 --> 00:14:08,920 These are all the flags. 220 00:14:08,920 --> 00:14:11,620 You see CMake flags for make stuff. 221 00:14:11,660 --> 00:14:12,660 That's how it is. 222 00:14:22,820 --> 00:14:35,720 And just to show a little bit more about this hash thing at the bottom, just so you know what this is, that's basically the result of all the inputs and the derivation getting spit out somewhere. 223 00:14:35,720 --> 00:14:38,820 And that will tell you that it's the same thing every time based on the inputs. 224 00:14:38,820 --> 00:14:45,840 That's how we make sure that all the stuff that we're putting in is the exact same every time so that we get that reproducibility. 225 00:14:45,860 --> 00:14:52,100 And so just to show this, if we do that same thing, make note of the hash thing. 226 00:14:52,100 --> 00:15:02,220 If I do this command, all I'm doing here is I'm going to show this derivation again, but I'm just going to add a random variable to it just called my var equals demo. 227 00:15:02,220 --> 00:15:03,180 It doesn't do anything. 228 00:15:03,180 --> 00:15:04,120 It doesn't change anything. 229 00:15:04,120 --> 00:15:09,960 But the fact that I added it and the fact that it's there in the inputs, it changes the output. 230 00:15:09,960 --> 00:15:11,180 It changes the hash. 231 00:15:11,180 --> 00:15:13,380 We can see it in the inputs here. 232 00:15:13,820 --> 00:15:14,540 It's here. 233 00:15:14,540 --> 00:15:15,520 It doesn't do anything. 234 00:15:15,620 --> 00:15:21,420 But the end result, this GJI, whatever, and then down here, it's different. 235 00:15:21,420 --> 00:15:25,400 The end result is different because of what we put in, even though it's not actually doing anything. 236 00:15:25,400 --> 00:15:29,300 And Nix is very picky about everything that you put in. 237 00:15:29,300 --> 00:15:34,680 And that guarantees that even if you change a comment, the entire thing is going to be different in the end. 238 00:15:35,760 --> 00:15:48,720 And just to show what is this hash, where does it come from, I have this file, file.txt. 239 00:15:49,120 --> 00:15:50,360 This is a test file. 240 00:15:50,360 --> 00:15:53,120 Capital T exclamation point, just make note of that. 241 00:15:53,600 --> 00:15:59,820 If I do Nix hash file, file.txt, it's going to give me the hash for the file of the stuff in it. 242 00:15:59,820 --> 00:16:08,960 If I go back into that file and I change something, I'm just going to like, there, just one thing. 243 00:16:09,040 --> 00:16:15,300 If I run that same thing, my hash is different because I changed the contents of the file. 244 00:16:15,300 --> 00:16:16,640 So that's all that a hash is. 245 00:16:16,640 --> 00:16:19,460 It's just making sure that your inputs are the same. 246 00:16:21,140 --> 00:16:23,340 So next is the Nix store. 247 00:16:23,340 --> 00:16:25,580 This is where everything lives. 248 00:16:25,580 --> 00:16:28,420 This is like the pantry in your house for all the stuff. 249 00:16:28,800 --> 00:16:31,420 This is like an excerpt of what all the stuff looks like in the Nix store. 250 00:16:31,420 --> 00:16:36,860 I highlighted all the hashes so you can kind of separate out what the thing is and then the hash. 251 00:16:37,520 --> 00:16:42,860 And the binary cache, I think that's something important to know about as well. 252 00:16:42,900 --> 00:16:44,920 You can think of that as precooked meals. 253 00:16:45,720 --> 00:16:53,280 And when I go to, I think I have this, yeah, I'm going to get into it here as well. 254 00:16:53,280 --> 00:17:03,000 But when I go to grab something, I'm going to grab, I'm going to get the derivation from somewhere, which I'll talk about in a second. 255 00:17:03,000 --> 00:17:04,200 And I'm storing that. 256 00:17:04,200 --> 00:17:06,660 And final binary, all that's being put into the store. 257 00:17:06,840 --> 00:17:14,680 And when I go to actually decide whether or not I need to build something from the derivation, you can actually skip that whole step. 258 00:17:14,680 --> 00:17:19,120 Because if we know the hash is the exact same, we know all the inputs are the same. 259 00:17:19,120 --> 00:17:21,620 So we know the result is going to be what we want. 260 00:17:21,620 --> 00:17:30,280 So if somebody else already built that for us and they put it somewhere that we can grab easily, why would we do the whole process of rebuilding it all ourselves? 261 00:17:30,380 --> 00:17:31,980 So that's all a binary cache. 262 00:17:31,980 --> 00:17:33,340 It's just precooked meals for you to fetch. 263 00:17:33,340 --> 00:17:35,260 No need to manually rebuild stuff. 264 00:17:35,540 --> 00:17:39,020 So this slide is like my densest one. 265 00:17:39,020 --> 00:17:41,260 I was thinking about what the best way to explain this is. 266 00:17:41,260 --> 00:17:46,320 But Nix packages, you can think of as where all these recipes come from. 267 00:17:46,320 --> 00:17:48,800 It's the biggest, fattest cookbook. 268 00:17:48,920 --> 00:17:57,080 And this search.nixos.org, you can go here, you can do a search for any Nix package you could probably possibly think of, and it's probably there. 269 00:17:57,080 --> 00:17:58,620 Somebody's probably packaged it and put it there. 270 00:17:58,620 --> 00:18:00,340 It is absolutely huge. 271 00:18:00,420 --> 00:18:02,240 And it's like all the secret sauce. 272 00:18:02,240 --> 00:18:05,740 It has so many recipe generators. 273 00:18:06,540 --> 00:18:10,080 Before, I had this slide saying recipes themselves. 274 00:18:10,080 --> 00:18:14,300 But I think saying that is a little bit too much of an oversimplification. 275 00:18:15,000 --> 00:18:23,580 So here's the flow of what happens when we're run that command to run the hello program or the one where I did 2048. 276 00:18:24,360 --> 00:18:29,220 When you run it, Nix is going to look up hello in Nix packages, which is your big cookbook. 277 00:18:29,220 --> 00:18:33,000 It's going to look in the cookbook for the hello. 278 00:18:33,260 --> 00:18:35,640 And the cookbook has a recipe generator. 279 00:18:35,640 --> 00:18:38,160 You can think of this as like a template for the recipe. 280 00:18:38,340 --> 00:18:40,320 And it's not the recipe itself. 281 00:18:40,320 --> 00:18:45,420 So your machine's going to get the recipe from the cookbook on its own. 282 00:18:45,720 --> 00:18:52,460 Nix is going to run the recipe generator to get the actual recipe, the derivation for your system. 283 00:18:52,640 --> 00:18:55,380 And then it's going to ask the binary cache, yo, do you have this thing? 284 00:18:55,380 --> 00:18:58,560 And if you have it, sweet, I don't have to build it myself. 285 00:18:58,560 --> 00:18:59,660 Yoink, I'm going to grab it. 286 00:18:59,660 --> 00:19:02,040 And if not, I'm going to build it myself. 287 00:19:02,360 --> 00:19:07,300 And this process of building it yourself, like let's say for this hello, it didn't have it. 288 00:19:07,300 --> 00:19:19,860 Let's say I needed GCC to do it, it's going to turtle all the way down recursively and go all the way down to the bootstrap layer if it has to, if it can't find stuff in the binary cache. 289 00:19:20,000 --> 00:19:28,220 Which is really cool, because that means you can build up anything from the lowest level possible, which would take forever, but you could. 290 00:19:28,220 --> 00:19:30,780 You don't need external sources to build. 291 00:19:30,780 --> 00:19:39,480 All you need is the very bare minimum, like POSIX shell and bash and C compiler, stuff like that, super minimal. 292 00:19:40,080 --> 00:19:43,220 And at the end, it puts all this stuff in the store. 293 00:19:43,260 --> 00:19:51,060 And it puts both the recipe, the derivation, and the final binary in the store. 294 00:19:51,060 --> 00:19:52,760 It puts both of those in the store together. 295 00:19:52,960 --> 00:19:55,460 Not under the same hash, they're separate. 296 00:19:56,620 --> 00:19:58,520 So yeah. 297 00:19:58,520 --> 00:19:59,940 So that's Nix packages. 298 00:19:59,940 --> 00:20:04,820 So just to recap derivations or recipes, the Nix store is your pantry. 299 00:20:05,040 --> 00:20:06,820 Nix packages is the biggest, fattest cookbook. 300 00:20:06,820 --> 00:20:13,660 And I went on a side tangent about these recipe generator things, just to not confuse you later if you look into it more. 301 00:20:14,020 --> 00:20:16,780 And this is just a graph that shows all the... 302 00:20:16,780 --> 00:20:21,420 it might be kind of hard to see, but it shows all the repositories. 303 00:20:21,620 --> 00:20:26,420 And you can see that Nix packages unstable is just completely in a league of its own, all the way up at the top. 304 00:20:26,420 --> 00:20:29,560 The AUR is a little bit underneath it. 305 00:20:29,760 --> 00:20:34,120 But those different versions get into what we're going to talk about next for Nix packages. 306 00:20:34,320 --> 00:20:35,480 So Flakes. 307 00:20:35,480 --> 00:20:37,740 Flakes is the reproducibility solution. 308 00:20:37,980 --> 00:20:41,760 Explaining Flakes can be pretty difficult, but just use Flakes, bro. 309 00:20:41,760 --> 00:20:42,180 Trust me. 310 00:20:42,180 --> 00:20:42,800 Just trust me, bro. 311 00:20:42,800 --> 00:20:44,080 All the cool kids use Flakes. 312 00:20:45,560 --> 00:20:50,360 In order to explain Flakes, I'm going to talk about channels really quickly. 313 00:20:50,360 --> 00:20:54,720 And I think you need to know about that to understand what Flakes is doing and why you should care. 314 00:20:54,720 --> 00:21:02,880 So earlier when I gave you this example for building the hello binary, I showed you these are the inputs, and I lied to you. 315 00:21:02,880 --> 00:21:03,700 I'm sorry. 316 00:21:03,700 --> 00:21:09,460 There's actually a hidden input, and it's the Nix packages version that you're grabbing the stuff from. 317 00:21:09,460 --> 00:21:15,160 And that actually changes the output because the inputs are different. 318 00:21:15,560 --> 00:21:24,740 So just to show this, I'm going to run evaluate, which is just going to do the... it's going to tell me what I'm going to get. 319 00:21:24,740 --> 00:21:25,860 It's not going to do the build for me. 320 00:21:25,860 --> 00:21:27,300 It's going to tell me what the result would be. 321 00:21:27,940 --> 00:21:31,020 And you're going to see I have this hash here. 322 00:21:31,100 --> 00:21:34,240 Really only look at the last couple of characters just to pay attention. 323 00:21:34,520 --> 00:21:41,800 And I'm going to do the same thing, but this time I'm going to specify I want it from Nix packages version 2505. 324 00:21:42,300 --> 00:21:45,520 And I think my system right now is on 2511 or unstable. 325 00:21:45,640 --> 00:21:49,400 But you'll see if I run it, my hash is different. 326 00:21:49,400 --> 00:21:50,020 And why? 327 00:21:50,020 --> 00:21:54,820 It's actually a different version of this hello program. 328 00:21:54,820 --> 00:21:58,800 So if I run this, just to show, this one's 2.12.2. 329 00:21:58,840 --> 00:22:01,540 If I run the same thing, 330 00:22:05,400 --> 00:22:07,240 this one's 2.12.1. 331 00:22:08,240 --> 00:22:10,840 I actually think I have a different version in that example. 332 00:22:11,240 --> 00:22:17,660 I can change this to 2505. 333 00:22:22,080 --> 00:22:23,720 2.12.1, same thing. 334 00:22:23,880 --> 00:22:25,660 But the versions are different. 335 00:22:25,660 --> 00:22:34,700 So on my system, the one that I have currently when I ran that evaluate, I got 2.12.2 and the hash was different versus what it is if I use some different version. 336 00:22:34,700 --> 00:22:37,920 So that gets back to the whole it's different. 337 00:22:37,920 --> 00:22:42,840 Running different versions of stuff for the same build will give you a different result. 338 00:22:42,840 --> 00:22:45,680 So this is kind of where Flakes comes in. 339 00:22:45,680 --> 00:22:50,300 And what it does is it's pinning of Nix packages. 340 00:22:50,540 --> 00:22:54,640 And this is like the reproducibility solution monolith all in one. 341 00:22:55,160 --> 00:23:00,340 So it goes back to the highest level of the same inputs are always going to give you the same outputs. 342 00:23:00,340 --> 00:23:02,520 And now it's the same inputs for real. 343 00:23:02,520 --> 00:23:07,420 It's using all the same Nix package source. 344 00:23:07,900 --> 00:23:10,820 And you can pin versions for specific packages. 345 00:23:10,940 --> 00:23:15,640 Once I run this, like this is just a super quick example of that hello in there. 346 00:23:15,640 --> 00:23:16,260 Super minimal. 347 00:23:16,260 --> 00:23:20,280 I'm just pinning it to version 2311 for Nix packages in this example. 348 00:23:20,680 --> 00:23:20,760 And 349 00:23:24,000 --> 00:23:28,100 it's going to generate me a lock file, which is how they version everything. 350 00:23:28,100 --> 00:23:30,800 And it's going to keep track of the version of all the packages. 351 00:23:30,800 --> 00:23:38,920 So that if I give you my repo or whatever, and you run it with the Flake in it, you're going to get the exact same output every time. 352 00:23:39,480 --> 00:23:44,360 Because the version, the Nix packages version for every package you're using is going to also be the same. 353 00:23:44,360 --> 00:23:46,520 There's no more invariability between our systems. 354 00:23:46,520 --> 00:23:50,440 What if what if your system is running 2511 and mine's 2505? 355 00:23:50,440 --> 00:23:51,480 Now we get a different result. 356 00:23:51,480 --> 00:23:53,480 This is not the reproducibility that we actually want. 357 00:23:54,540 --> 00:23:56,820 So this is like the highest level. 358 00:23:56,820 --> 00:23:58,500 Let's take this to the next level. 359 00:23:58,500 --> 00:24:01,620 This is all the previous stuff we talked about. 360 00:24:01,620 --> 00:24:06,680 But instead of configuring applications, packages, you're doing it for your system now. 361 00:24:07,000 --> 00:24:12,760 And this, you can do this for your bootloader, login manager, blah, blah, blah, all the stuff for your system, which is, it's pretty crazy. 362 00:24:12,760 --> 00:24:16,280 When you think about it, you can do your entire system configured declaratively. 363 00:24:16,280 --> 00:24:20,820 And you can just share your entire computer configuration with somebody else. 364 00:24:21,080 --> 00:24:23,900 And they can just have your whole setup, which is what I did earlier. 365 00:24:23,900 --> 00:24:28,580 I put, I put my laptop right here, my whole configuration onto my brother's laptop. 366 00:24:28,580 --> 00:24:29,960 I did that two days ago. 367 00:24:31,000 --> 00:24:34,500 And just to show this, I'm going to give a quick example. 368 00:24:36,880 --> 00:24:39,740 And all that I have here is just a flake. 369 00:24:39,980 --> 00:24:46,820 And it is using Astro's micro VMs, which is just a, I'm just going to boot up a micro VM to show you a NixOS system. 370 00:24:47,120 --> 00:24:51,520 And I'm sharing my Nix store with it so that the boot up time is really fast. 371 00:24:51,520 --> 00:24:52,180 But this is the whole flake. 372 00:24:52,180 --> 00:24:52,720 It's really simple. 373 00:24:52,720 --> 00:24:55,400 All I'm doing is I'm pulling in Nix packages. 374 00:24:55,400 --> 00:24:56,440 I'm using unstable. 375 00:24:56,720 --> 00:24:59,540 And I'm using micro VM, which is the thing I just told you about. 376 00:24:59,540 --> 00:25:01,500 And this is the configuration for it. 377 00:25:01,500 --> 00:25:06,120 The stuff that you care about is this, this configuration.nix file that I made to make it easier to follow. 378 00:25:06,120 --> 00:25:07,160 This is what it looks like. 379 00:25:07,160 --> 00:25:09,560 I'm setting up my host name for my system. 380 00:25:09,560 --> 00:25:11,480 I'm making a user, demo user. 381 00:25:11,500 --> 00:25:14,260 I'm giving him a password, a description. 382 00:25:14,420 --> 00:25:17,100 He's a normal user and I'm giving him wheel group so he can run sudo. 383 00:25:17,340 --> 00:25:21,600 And he's got these packages for the system. 384 00:25:21,600 --> 00:25:24,420 He's got the whole system that has Vim on it. 385 00:25:24,420 --> 00:25:29,120 And it's going to auto log in for my demo user and I'm hitting the system state version. 386 00:25:29,640 --> 00:25:37,660 So if I just run this, you're going to see it's going to boot up a micro VM. 387 00:25:41,040 --> 00:25:45,060 And it's going to put that stuff from the configuration on this machine. 388 00:25:45,060 --> 00:25:48,140 And you'll see we're going to be logged in as demo user in a second. 389 00:25:48,140 --> 00:25:52,100 And we'll see we have the Vim package on here. 390 00:25:52,620 --> 00:25:53,600 So we're logged in. 391 00:25:53,600 --> 00:25:54,240 Cool. 392 00:25:54,760 --> 00:25:55,740 Who am I? 393 00:25:56,960 --> 00:25:57,860 User's wheel. 394 00:25:57,860 --> 00:25:58,440 Cool. 395 00:25:58,660 --> 00:26:00,000 Vim, do I have it? 396 00:26:00,000 --> 00:26:01,380 I do have it. 397 00:26:01,400 --> 00:26:04,800 And then can I run sudo? 398 00:26:04,800 --> 00:26:06,020 Power off. 399 00:26:06,880 --> 00:26:08,100 Password was demo. 400 00:26:08,240 --> 00:26:09,700 Yeah, I can shut it down. 401 00:26:09,840 --> 00:26:19,550 Just to show this like super quick, what I'm just going to do is I'm going to add htop to the packages that are going to go on the system. 402 00:26:19,710 --> 00:26:20,970 And I'm going to build it again. 403 00:26:20,970 --> 00:26:26,710 And you're going to see that this time I'm going to have, I probably should have shown that I didn't have htop on it before, but trust me, I didn't. 404 00:26:29,650 --> 00:26:33,390 I'm running again and you'll see that we have that package on the machine now. 405 00:26:36,150 --> 00:26:44,010 And this little micro VM is just a super simplification of a bigger NixOS system that has all the stuff on it. 406 00:26:44,010 --> 00:26:47,730 So like this system that I'm showing you this on right now has that. 407 00:26:50,470 --> 00:26:51,830 Cool, we have it. 408 00:26:51,830 --> 00:26:59,110 So yeah, that's, it's really powerful being able to use Nix for a whole system instead of just packages and configurations. 409 00:26:59,110 --> 00:27:04,910 And it lets you be really particular about everything that you have on that given system. 410 00:27:04,910 --> 00:27:10,390 And it lets you track it all super easily because like you can just go to that configuration file and see what's on the system. 411 00:27:10,390 --> 00:27:13,650 There's no ambiguity about what's installed on there or how things are configured. 412 00:27:15,050 --> 00:27:17,870 And just as a quick aside, generations. 413 00:27:17,950 --> 00:27:22,710 So I noticed somebody said a question about like versioning systems before. 414 00:27:23,070 --> 00:27:25,070 And I added that while I was listening. 415 00:27:25,250 --> 00:27:28,050 Generations are really cool for the system. 416 00:27:28,050 --> 00:27:40,810 If I were, if I had this on a traditional system and I like rebooted my right now, during my boot menu, I have the option to roll back or use a different generation or version of my NixOS configuration. 417 00:27:41,030 --> 00:27:50,570 So every time I make a change to my config and I rebuilt my system to it, it's going to add it as a new generation list. 418 00:27:50,670 --> 00:28:00,170 And it keeps a backlog of however many you can specify how many you want to keep, how many you want to like delete after a bad config. 419 00:28:00,170 --> 00:28:07,570 Like I do something dangerous, like I change variables for my boot loader, like I'm configuring it with Nix and I do something bad and I bork it. 420 00:28:07,650 --> 00:28:08,950 And now my system is broken. 421 00:28:08,950 --> 00:28:09,890 I don't even boot. 422 00:28:10,350 --> 00:28:10,970 It's fine. 423 00:28:10,970 --> 00:28:13,410 I'll just roll back the configuration and I'm saved. 424 00:28:13,410 --> 00:28:15,610 You don't have to worry about breaking your system anymore. 425 00:28:15,610 --> 00:28:23,330 You can go ham and do all the stupid things you want to do and not have to worry about doing the full fresh install all the way from scratch. 426 00:28:26,250 --> 00:28:27,850 So, yeah. 427 00:28:27,850 --> 00:28:28,350 Thanks. 428 00:28:28,350 --> 00:28:41,390 That was kind of like my super crash course about how I went from not really knowing anything about Nix just back in February to now I have all my machines configured declaratively. 429 00:28:41,390 --> 00:28:43,570 I get to hack on Nix for work. 430 00:28:44,990 --> 00:28:46,510 And, yeah. 431 00:28:46,510 --> 00:28:58,410 And I guess at this point, if I have it, I'm just going to kind of flex my repository that I have for my machines and I share it with Britton over there, so I'll show you. 432 00:29:02,920 --> 00:29:03,460 So, yeah. 433 00:29:03,460 --> 00:29:04,620 This is ours. 434 00:29:05,540 --> 00:29:15,500 And the way, without getting too much into it, just to show not everything for a system configuration has to be all condensed into one file, because most people don't actually do that, because it would end up being really huge. 435 00:29:15,500 --> 00:29:17,180 They split it out into multiple things. 436 00:29:17,180 --> 00:29:20,340 And just at the end, it all gets merged together back into the same place. 437 00:29:20,600 --> 00:29:35,320 But the way that I have things set up that I think is really cool for organizations is, and I'm only going to show this part, I think, the way that I have machines, users, and user environments configured across all my machines, all in one place. 438 00:29:35,320 --> 00:29:41,420 So, I have all these machines set up on my on our infrastructure. 439 00:29:41,420 --> 00:29:42,700 So, like, AlexDev is a machine. 440 00:29:42,980 --> 00:29:45,920 AlexFramework, that's this machine, this laptop that I'm using right now. 441 00:29:45,920 --> 00:29:50,480 And I put the name for it, the host, the target host for it. 442 00:29:50,480 --> 00:29:51,900 And this is using plan, by the way. 443 00:29:51,900 --> 00:29:53,200 I should have mentioned that. 444 00:29:54,260 --> 00:30:01,760 And I have this tag system set up so that I don't actually have all the configurations all in one big place, like I mentioned. 445 00:30:01,760 --> 00:30:09,700 It's actually kind of like a buffet example, where you go to this inventory and you pick out the things that you want for that machine. 446 00:30:09,700 --> 00:30:11,500 So, this is a laptop. 447 00:30:11,500 --> 00:30:12,980 I want to be on tail net. 448 00:30:13,080 --> 00:30:14,680 And this laptop has Hyperlend on it. 449 00:30:14,680 --> 00:30:15,600 That's what I want. 450 00:30:15,760 --> 00:30:22,760 And if I go and actually look at those tags, like the Hyperlend one, you can see, like, for this, this is the base stuff that I need to do Hyperlend. 451 00:30:22,760 --> 00:30:31,840 So, this, because I added this tag to that machine, this is getting pulled into and essentially added to, as far as that machine's concerned, this is part of its configuration. 452 00:30:32,260 --> 00:30:39,500 And the next step is, like, HomeManager, which is configuring dot files for yourself. 453 00:30:40,200 --> 00:30:54,800 So, like, for instance, I have... this is my ZSH RC set up with HomeManager, which is basically, HomeManager is Nix set up, but for your dot files to manage all of that declaratively as well. 454 00:30:54,800 --> 00:31:00,900 And the end result of this, so, I have all this stuff on here, just, I'll show you, like, we have all these aliases and stuff. 455 00:31:01,020 --> 00:31:08,940 And what I'll do is, like, this is the generated ZSH RC. 456 00:31:08,940 --> 00:31:11,100 This is, like, some robot generated RC. 457 00:31:11,100 --> 00:31:12,400 You probably wouldn't want to read this. 458 00:31:12,400 --> 00:31:14,680 Like, it's not meant to be super human readable. 459 00:31:14,680 --> 00:31:19,740 But you can see, like, it's taking all the stuff that I'm configuring here and it's putting it all in here. 460 00:31:19,740 --> 00:31:22,520 Like, all the aliases are here from everywhere. 461 00:31:22,940 --> 00:31:39,620 But, yeah, so, the end game, I guess, that I'm getting at with this is, you can be really modular with system configurations and you can manage all your machines from the same place and choose what you want to be on them with these, like, pre-built little, 462 00:31:39,620 --> 00:31:42,040 like, nuggets that you can make for it. 463 00:31:42,040 --> 00:31:50,200 And it makes spinning up new machines and managing machines and knowing the exact state of all your machines super easy. 464 00:31:51,160 --> 00:31:54,500 So, yeah, I think that's about it. 465 00:31:55,340 --> 00:31:56,520 Thanks, guys.