1 00:00:00,750 --> 00:00:01,440 All right. 2 00:00:01,440 --> 00:00:03,420 Hello, Nix Vegas. 3 00:00:05,500 --> 00:00:06,300 All right. 4 00:00:06,300 --> 00:00:07,740 So, my name is Wes Payne. 5 00:00:07,740 --> 00:00:10,440 Thank you for hanging out, learning more about Nix. 6 00:00:10,440 --> 00:00:15,500 And this talk is Mesh Network Sidecars for NixOS Services. 7 00:00:16,580 --> 00:00:17,320 All right. 8 00:00:17,320 --> 00:00:20,420 So, first up, just a little bit about me. 9 00:00:21,180 --> 00:00:31,640 I'm a Seattle-based software engineer interested in functional programming and distributed systems, specifically Clojure, and more recently, definitely Nix. 10 00:00:31,920 --> 00:00:49,620 And then you can also find me rambling about Linux, about open source, about the lovely communities that build and shape open source, and nerdy kernel details like IOUring, eBPF, and, of course, my favorite, KExec, just about every week on the Linux Unplugged podcast, 11 00:00:49,620 --> 00:00:51,960 we like to say the largest Linux podcast. 12 00:00:52,520 --> 00:00:55,820 I'm a co-host there on the Jupyter Broadcasting Network. 13 00:00:56,180 --> 00:01:01,060 And then, perhaps most importantly, pictured to the right here is my dog, Charlie, little old lady. 14 00:01:01,060 --> 00:01:08,820 She's 12 years old and still keeps me busy when I'm not hacking, going on hikes in the Cascade Mountains. 15 00:01:09,700 --> 00:01:12,340 But we're here for Nix. 16 00:01:12,340 --> 00:01:12,700 Okay. 17 00:01:12,700 --> 00:01:15,420 So, Mesh Networks, Sidecars, NixOS. 18 00:01:15,420 --> 00:01:17,060 Let's define some terms. 19 00:01:17,140 --> 00:01:20,300 So, first up is Mesh Network. 20 00:01:20,300 --> 00:01:28,780 These go by many names, and the exact concept is not particularly important in terms of, you know, bike-shedding about the exact definition. 21 00:01:28,780 --> 00:01:32,640 But they're called Overlay Networks, they're called Peer-to-Peer VPNs. 22 00:01:32,640 --> 00:01:38,300 The basic idea is one flat network for all your devices and services. 23 00:01:38,300 --> 00:01:45,200 And in particular, it's probably good to contrast this with more hub-and-spoke style traditional VPNs. 24 00:01:45,200 --> 00:01:46,660 I'm sure many of us are familiar, right? 25 00:01:46,660 --> 00:01:59,680 You open up OpenVPN on your laptop, and it connects to some endpoint that corporate IT set up, and you go through a VPN concentrator and a router, and then you can access all the internal services on some corporate LAN. 26 00:02:00,160 --> 00:02:04,740 Well, with a Mesh Network or an Overlay Network, the idea is everything lives on the Mesh. 27 00:02:04,740 --> 00:02:16,260 So, each one of your servers, maybe your laptop, maybe your mobile device, they all have first-class entries on your Mesh Network, and they can all talk to each other without having to route anywhere else. 28 00:02:16,320 --> 00:02:18,660 Or at least that's obscured and handled below the scene. 29 00:02:18,660 --> 00:02:30,740 So, instead of, like, me trying to talk from my laptop to another laptop that all has to go route through some central server, they just set up, they do NAT busting, it's all very clever, so they can get actual Peer-to-Peer connections to each other. 30 00:02:31,600 --> 00:02:36,180 I've been interested in this stuff for a while, and it's actually been around longer than you might think. 31 00:02:36,280 --> 00:02:39,760 In particular, Tink came out in 1998. 32 00:02:40,160 --> 00:02:44,600 It kind of has that classic, old, first-era internet vibe to it. 33 00:02:44,600 --> 00:02:49,560 It also feels very much like a traditional Unix daemon, so it's a little bit fun to work with. 34 00:02:49,700 --> 00:02:54,640 I saw a great talk at Linux Fest Northwest, where people had set up basically their own private LAN. 35 00:02:54,640 --> 00:02:57,740 This was probably 10 years ago, I don't know, 2015 or so. 36 00:02:57,800 --> 00:03:04,980 But they'd gone so far as to, like, set up a proper domain, they had roaming user profiles, they had printers shared all between their houses. 37 00:03:05,320 --> 00:03:06,960 So, I got interested. 38 00:03:06,960 --> 00:03:13,260 I did basic stuff, like setting up backups from my house to my folks' place and vice versa. 39 00:03:14,380 --> 00:03:20,040 But these days, by modern standards, Tink just... I mean, it doesn't have great performance. 40 00:03:20,120 --> 00:03:25,360 But then also, it just was never really built with modern security principles in mind. 41 00:03:25,400 --> 00:03:33,840 But thankfully, we don't have to stop at Tink, because I think we're in a bit of a mesh network renaissance, especially starting in 2019. 42 00:03:33,880 --> 00:03:40,560 That saw Slack Open Source Nebula, which was a mesh overlay network they developed in-house. 43 00:03:40,560 --> 00:03:49,400 In particular, they were doing stuff where they wanted to have multiple clouds, they had multiple VPCs, they tried to tie this together, and they added ACLs. 44 00:03:49,400 --> 00:03:52,320 I mean, it's a cloud permissions first world. 45 00:03:52,320 --> 00:03:58,400 And so, you do things like, oh, well, only users in these groups can talk to the database group and vice versa. 46 00:03:58,400 --> 00:04:06,560 So, suddenly, it becomes easy to connect CI to the right staging places to do tests, as well as connect developers to the resources that they might need. 47 00:04:06,900 --> 00:04:10,140 And then, of course, Tailscale also came out in 2019. 48 00:04:10,300 --> 00:04:11,840 Netbird is another competitor. 49 00:04:11,840 --> 00:04:13,320 There's, like, a whole bunch more now. 50 00:04:13,320 --> 00:04:14,840 So, we're spoiled for choice. 51 00:04:14,840 --> 00:04:16,480 Tailscale is probably the most popular. 52 00:04:18,120 --> 00:04:18,900 There we go. 53 00:04:18,900 --> 00:04:19,160 Yes. 54 00:04:19,160 --> 00:04:23,560 And a great reminder, this talk is being streamed over Nebula right now. 55 00:04:24,060 --> 00:04:27,380 So, it's increasingly popular across all kinds of different scales. 56 00:04:27,380 --> 00:04:32,980 Simplifying Homelabs is something we see a lot, but also flattening, annoying multi-cloud and VPC networks. 57 00:04:34,200 --> 00:04:34,580 Okay. 58 00:04:34,580 --> 00:04:37,660 So, next up, then, for terms is Sidecar. 59 00:04:37,660 --> 00:04:39,600 And here, I thought we'd start with an example. 60 00:04:39,700 --> 00:04:41,240 I know this is the next talk, so I'm sorry. 61 00:04:41,240 --> 00:04:42,260 There is some YAML. 62 00:04:42,260 --> 00:04:45,540 I promise it's the only YAML sample that we'll see in this talk today. 63 00:04:45,820 --> 00:04:52,780 So, this is just an example I stole from Tailscale Docs, where we're running a VaultWarden server, which is an open source BitWarden password manager. 64 00:04:52,780 --> 00:04:54,760 That part's not too important. 65 00:04:55,300 --> 00:05:00,040 Normal Docker Compose file, except we've got network mode. 66 00:05:00,040 --> 00:05:03,680 And that's set to service Tailscale Sidecar. 67 00:05:03,680 --> 00:05:07,020 And then we have a second container to find, and that's Tailscale Sidecar. 68 00:05:07,020 --> 00:05:08,800 And we're running Tailscale. 69 00:05:08,820 --> 00:05:11,540 In particular, here, we can customize the host name. 70 00:05:11,540 --> 00:05:13,920 We feed it some environment variables to configure it. 71 00:05:14,080 --> 00:05:15,740 And we poke some holes. 72 00:05:15,740 --> 00:05:19,840 We give it some permanent state here with volumes that it can store anything it needs to. 73 00:05:19,840 --> 00:05:29,520 And we give it additional capabilities, like NetAdmin, because it needs to go tell the kernel and do some other networking-related things that could have security compromises. 74 00:05:29,520 --> 00:05:33,640 You don't want to give this to everything, but to be an effective VPN, it needs them. 75 00:05:34,680 --> 00:05:37,720 So, this pattern got used a lot . 76 00:05:37,800 --> 00:05:39,180 It's used in the cloud. 77 00:05:39,180 --> 00:05:40,540 It's used in Kubernetes. 78 00:05:40,540 --> 00:05:44,800 And it's used a ton in the home lab, just with Docker Compose like that. 79 00:05:45,220 --> 00:05:47,020 And there are some reasons for this. 80 00:05:47,020 --> 00:05:50,860 So, right out of the gate, Docker, of course, gives you network isolation. 81 00:05:51,280 --> 00:05:57,620 You have to go the other way and go poke holes if you want to have host-mode networking or you want to share some ports or forward things. 82 00:05:58,600 --> 00:06:03,000 And that means anything you run in this style is only exposed to the host if you want. 83 00:06:03,140 --> 00:06:06,900 It also gives you application-level networking. 84 00:06:06,900 --> 00:06:18,600 So, instead of having to think, like, oh, right, this service runs on host A, I have to kind of multiplex myself through that host to go get to maybe the 10 other services running on that particular box. 85 00:06:18,600 --> 00:06:25,900 Now I can just sort of pick up this Docker Compose file, move it to another server, and nothing else on the mesh network has to care. 86 00:06:25,900 --> 00:06:32,200 Because if they all use internal mesh addresses or names, from their perspective, nothing changed. 87 00:06:32,200 --> 00:06:41,280 So, you essentially decouple the services you're running from the host infrastructure that it's on, but in, you know, a very home lab-friendly way. 88 00:06:43,020 --> 00:06:51,560 It also is really enabled, I think, because we have this confluence of modern mesh networks that are super-configurable, made to run kind of wherever they can. 89 00:06:51,560 --> 00:06:55,480 Many of them have both kernel and user-space implementations for their networking. 90 00:06:56,140 --> 00:06:58,220 And we have containers. 91 00:06:58,680 --> 00:07:02,880 But this is Nix, and I want to stop talking about containers, right? 92 00:07:02,880 --> 00:07:05,260 So, let's talk about NixOS. 93 00:07:05,260 --> 00:07:06,540 This is NixVegas. 94 00:07:06,540 --> 00:07:21,400 Probably many of you, I hope, are familiar with Nix and NixOS, but if not, NixOS is a declarative operating system Linux distribution built on top of Nix, the glorious functional programming language and amazing software build tool. 95 00:07:21,760 --> 00:07:32,860 And, I mean, it does a lot of things very well, but one of those in particular is it solves a lot of the sort of conflicting dependency problems that we're all too familiar with in traditional Linux distributions. 96 00:07:32,900 --> 00:07:36,400 And this is also kind of what, you know, containers let us do in many cases. 97 00:07:36,600 --> 00:07:44,000 I don't have to care what kind of language or tool chain or runtime VaultWarden runs, you know, or Redis runs. 98 00:07:44,000 --> 00:07:47,960 They can run right together, even if one's based on Debian and another one's based on Alpine. 99 00:07:49,160 --> 00:08:03,660 So, Nix can do this, but without having to stuff each of your apps into a container that might also contain, like, a whole half a dozen other OS or Python libraries or a bunch of tools that you don't actually need. 100 00:08:03,820 --> 00:08:08,540 You know, we kind of create these containers and then end up having to run tools to scan them and slim them down. 101 00:08:08,540 --> 00:08:11,360 And maybe we could just... we could just not. 102 00:08:11,880 --> 00:08:21,780 It's also made very easy and just a lot of fun because it's powered by Nix packages, which is a giant cookbook of 120,000 plus different packages. 103 00:08:21,780 --> 00:08:25,720 So, it pretty much has all the software you might need to run, and it's pretty easy to add your own. 104 00:08:25,840 --> 00:08:30,780 And then you also get NixOS modules, which set up and run services for you. 105 00:08:30,780 --> 00:08:32,060 So, it's not just a package. 106 00:08:32,060 --> 00:08:34,500 It's also all of the configuration on your host. 107 00:08:34,500 --> 00:08:35,920 It might poke holes in the firewall. 108 00:08:35,920 --> 00:08:37,340 It's going to set up storage locations. 109 00:08:37,340 --> 00:08:39,500 It's going to run systemd services for you. 110 00:08:39,540 --> 00:08:41,600 And that's pre-configured, ready to go. 111 00:08:41,620 --> 00:08:45,900 So, the one that really clicked for me the first time around, I mean, the Nginx module, great. 112 00:08:45,940 --> 00:08:47,260 Postgres, super easy. 113 00:08:47,320 --> 00:08:48,280 But Nix Cloud. 114 00:08:48,500 --> 00:08:58,480 For the podcast, we run a Nix Cloud, and it's kind of how we exchange stuff between editors and the hosts, where we keep a lot of the assets we use for our soundboard and live streams. 115 00:08:58,480 --> 00:09:02,820 So, we wanted to run it, and I was quite familiar with running it in a Docker style. 116 00:09:02,820 --> 00:09:04,920 But sometimes you need to mess with stuff, right? 117 00:09:04,920 --> 00:09:08,920 So, in the example on the right, we have max upload size. 118 00:09:09,200 --> 00:09:14,180 We've also got some things in there where we're setting PHP options around the opcache. 119 00:09:15,060 --> 00:09:28,660 And if you've ever done this with like a regular sort of Docker compose setup, oftentimes what ends up happening, because it's all just adding and appending layers, if you want to go modify something in an earlier layer, I mean, you can do it, but it's on you not to break anything. 120 00:09:28,660 --> 00:09:29,800 There's no easy way to do it. 121 00:09:29,800 --> 00:09:31,020 It's not declarative. 122 00:09:31,380 --> 00:09:38,940 Versus here, I can just say, change the max upload size, and it doesn't matter if that needs to set like both a PHP option and an Nginx conf. 123 00:09:38,940 --> 00:09:41,320 Like, all of that will just get updated for me. 124 00:09:42,360 --> 00:09:50,260 And last but not least, Nix and NixOS have great support for Nebula, Tailscale, Netbird, like pretty much all these modern VPNs. 125 00:09:50,260 --> 00:09:52,860 So, we're in good territory already. 126 00:09:55,100 --> 00:10:05,540 But, as great as NixOS is for running Homelab services, running your desktop, your laptop, running servers, there is no native sidecar-like option. 127 00:10:05,540 --> 00:10:13,060 With Docker, you can kind of copy and paste, make one little tweak to your network mode like we saw at the beginning, and you can do this sidecar style. 128 00:10:13,420 --> 00:10:16,120 Unfortunately, with NixOS, it's not quite that easy. 129 00:10:16,120 --> 00:10:18,160 And you may ask, you know, there's other ways to do it. 130 00:10:18,160 --> 00:10:22,760 You can't just run regular old Tailscale on your host and get by with that. 131 00:10:22,760 --> 00:10:36,420 But I find, especially once we started talking more about Nix in the show, that, you know, you can debate when you learn a new paradigm how much of your old workflow you should bring over versus how much you should embrace the new world. 132 00:10:36,420 --> 00:10:44,980 But regardless, if you can bring over, like if you don't have to change your entire network architecture as you change, like your OS and build tooling, that's a little bit easier. 133 00:10:44,980 --> 00:10:59,460 So, the solution we're seeing today, this mesh sidecar module, is kind of my attempt at providing a similar experience and a soft landing for folks who might be considering NixOS but are already very used to doing things in a containerized or Docker-first workflow. 134 00:10:59,940 --> 00:11:04,040 And also, various people just were sort of on the internet asserting that you couldn't do this. 135 00:11:04,040 --> 00:11:06,340 So, it's a bit of spike-driven development as well. 136 00:11:08,060 --> 00:11:08,560 Okay. 137 00:11:08,560 --> 00:11:10,920 So, part one, enter systemd. 138 00:11:10,920 --> 00:11:13,740 We're all systemd fans here, right? 139 00:11:13,800 --> 00:11:14,520 Right? 140 00:11:14,860 --> 00:11:18,980 In any case, NixOS, I think, loves systemd. 141 00:11:18,980 --> 00:11:20,760 I mean, you can use other things. 142 00:11:20,760 --> 00:11:21,900 NixOS is very flexible. 143 00:11:21,900 --> 00:11:27,240 But with Nix, you have this very powerful declarative build tool. 144 00:11:27,300 --> 00:11:30,660 But that really only exists at build time. 145 00:11:30,660 --> 00:11:34,740 And if you're running the system, runtime kind of matters. 146 00:11:35,360 --> 00:11:40,040 And systemd has grown to be a very flexible tool that can do... I mean, it can manage mount points. 147 00:11:40,040 --> 00:11:41,420 It can handle DNS queries. 148 00:11:41,420 --> 00:11:42,480 It can do networking. 149 00:11:42,480 --> 00:11:46,940 And, you know, you can configure just about everything on a modern Linux distribution with systemd. 150 00:11:47,000 --> 00:11:59,020 And if you can then do complicated, sophisticated, arbitrary build time algorithms to determine what those systemd services should look like, you have a lot of availability to customize your system. 151 00:11:59,720 --> 00:12:05,300 Also, as systemd has advanced, it's gotten a lot of new security and confinement options. 152 00:12:05,300 --> 00:12:06,780 I mean, these days, it's not even that new. 153 00:12:06,780 --> 00:12:10,620 But if you don't go spelunking in the docs, you might not know how many options exist. 154 00:12:10,620 --> 00:12:12,760 Like, you can have private devices. 155 00:12:12,760 --> 00:12:14,540 You can protect the home dir files. 156 00:12:14,540 --> 00:12:16,160 You can protect system files. 157 00:12:16,160 --> 00:12:20,540 You can make sure that no, you know, certain types of address families are allowed for connections or not. 158 00:12:20,540 --> 00:12:24,740 And all of that's just in the regular old ini file for configuring systemd. 159 00:12:25,600 --> 00:12:35,140 And in particular for today, it also now exposes many of the same cgroup and namespace controls as your favorite container runtimes. 160 00:12:37,250 --> 00:12:37,890 Okay. 161 00:12:37,890 --> 00:12:47,970 So, to make this a little more practical and not just a lecture on container runtimes, if you have a home lab, one thing you might want to start doing is monitoring your home lab. 162 00:12:47,970 --> 00:12:52,170 I started my career off working in a NOC doing monitoring and observability. 163 00:12:52,170 --> 00:12:53,910 So, those have always been important to me. 164 00:12:54,130 --> 00:13:02,190 Thankfully, to make this a little easier, NixOS has modules for Grafana and Prometheus, which are both popular common choices for this. 165 00:13:02,190 --> 00:13:03,510 So, we'll start there. 166 00:13:03,510 --> 00:13:17,410 First things up, we'll get a basic config going that's just enough to run those things, a Grafana dashboard to look at, Prometheus to store our metrics, and a node exporter to export things, which we'll just do on my laptop here. 167 00:13:17,990 --> 00:13:19,090 This is all great. 168 00:13:19,090 --> 00:13:24,730 But, of course, without any help, it's all kind of trapped on one box or on one network. 169 00:13:26,130 --> 00:13:28,290 So, here is that config. 170 00:13:28,290 --> 00:13:31,210 Up top, we have services.grafana. 171 00:13:31,210 --> 00:13:33,170 Pretty standard for NixOS modules. 172 00:13:33,170 --> 00:13:37,130 Turn it on with enable, and then you can pass it some settings. 173 00:13:37,130 --> 00:13:42,710 In this case, you can tweak the listening address, the port, enable optimizations like gzip, whatever. 174 00:13:42,710 --> 00:13:45,130 This is a pretty basic stock config you might use. 175 00:13:45,370 --> 00:13:48,570 And then, same thing down below, we have Prometheus. 176 00:13:48,830 --> 00:13:51,890 Making a listen, you can tweak stuff like scrape interval. 177 00:13:52,210 --> 00:13:54,810 And then in this case, we're setting up a node exporter. 178 00:13:54,830 --> 00:14:00,950 So, you can also customize the collectors you want to run, add your own collectors that you're building from source using Nix if you want. 179 00:14:00,950 --> 00:14:02,170 All totally possible. 180 00:14:02,170 --> 00:14:05,190 But for this, just a sort of basic collector to start. 181 00:14:08,130 --> 00:14:10,210 And what does that get us? 182 00:14:10,210 --> 00:14:15,870 Well, up top, you'll see a snapshot of my personal tailscale dashboard. 183 00:14:15,870 --> 00:14:23,470 And I'm really just using tailscale here because I have a lot more stuff on Nebula, so it was easier to delete things off tailscale to get a clean snapshot. 184 00:14:24,290 --> 00:14:26,850 So, all we have is node one. 185 00:14:26,850 --> 00:14:29,450 And that is this old ThinkPad running right here. 186 00:14:29,450 --> 00:14:33,730 It just has a regular tailscale running on the host, exposing the whole host. 187 00:14:34,590 --> 00:14:39,810 And then if we take a look below, we can look at what is listening on this host. 188 00:14:40,170 --> 00:14:41,490 And we've got three things. 189 00:14:41,490 --> 00:14:43,730 We've got Prometheus on 9090. 190 00:14:43,730 --> 00:14:46,030 We have node exporter on 9000. 191 00:14:46,030 --> 00:14:48,230 And Grafana on 3000. 192 00:14:48,350 --> 00:14:48,630 Okay. 193 00:14:48,630 --> 00:14:48,970 Great. 194 00:14:48,970 --> 00:14:49,650 Kind of expected. 195 00:14:49,650 --> 00:14:51,510 They're all exposed locally on the host. 196 00:14:51,530 --> 00:14:52,890 Nothing too fancy is going on. 197 00:14:52,890 --> 00:14:56,430 And all tailscale knows about is the actual host itself. 198 00:14:59,160 --> 00:15:02,260 And this is where my module comes in. 199 00:15:02,260 --> 00:15:03,820 The mesh sidecar module. 200 00:15:03,820 --> 00:15:05,760 So, you've got to add this to your configuration somehow. 201 00:15:05,760 --> 00:15:06,620 Not pictured. 202 00:15:06,660 --> 00:15:08,380 Come talk to me after if you want help with that. 203 00:15:08,380 --> 00:15:09,500 But Flakes make it easy. 204 00:15:09,500 --> 00:15:11,940 You can build it into your NixOS config, pull it in. 205 00:15:12,140 --> 00:15:14,000 Lots of options there. 206 00:15:14,580 --> 00:15:17,400 And so far, we've got just a simple interface. 207 00:15:18,160 --> 00:15:19,400 You start by turning it on. 208 00:15:19,400 --> 00:15:20,100 Say enable. 209 00:15:20,100 --> 00:15:20,680 Yep. 210 00:15:20,780 --> 00:15:22,480 And then you select a provider. 211 00:15:22,520 --> 00:15:27,400 Right now, the module supports tailscale and netbird, but open to others in the future. 212 00:15:27,460 --> 00:15:32,520 You do have to tell that maybe considering perhaps letting you do both at once down the line. 213 00:15:32,520 --> 00:15:34,100 But so far, one only. 214 00:15:34,340 --> 00:15:36,580 And then you're going to need an auth key. 215 00:15:36,800 --> 00:15:41,660 And both tailscale and netbird let you have auth keys to sort of dynamically add hosts. 216 00:15:41,660 --> 00:15:43,440 So, that's what this module relies on. 217 00:15:43,520 --> 00:15:44,780 Store it somewhere secure. 218 00:15:44,820 --> 00:15:46,780 We'll pick it up with systemd credentials. 219 00:15:46,780 --> 00:15:48,440 So, it never touches the Nix store. 220 00:15:48,440 --> 00:15:51,140 So, as long as you secure it, we won't mess it up. 221 00:15:51,720 --> 00:15:54,440 And then a little bit more manual config here. 222 00:15:54,440 --> 00:15:55,820 The bridge name, totally optional. 223 00:15:55,820 --> 00:15:57,140 You can customize that. 224 00:15:57,140 --> 00:15:59,080 We are going to make ourselves a network bridge. 225 00:15:59,080 --> 00:16:00,420 We'll get more into that. 226 00:16:00,440 --> 00:16:03,220 But right now, you do need to set the outbound interface. 227 00:16:03,280 --> 00:16:09,200 The services we run, at least the tailscale or netbird part, are going to go need to talk to the control plane. 228 00:16:09,200 --> 00:16:10,900 So, they're going to need a connection. 229 00:16:10,900 --> 00:16:16,220 So, we'll give them a regular NATed outbound connection, just kind of like Docker Compose does by default. 230 00:16:16,620 --> 00:16:19,720 So, here I'm choosing a wireless network WLP. 231 00:16:20,380 --> 00:16:25,960 And then the main meaty bit of this is the services section down there at the end. 232 00:16:26,540 --> 00:16:29,500 So, to start, we're just going to wrap Grafana and Prometheus. 233 00:16:29,600 --> 00:16:30,860 But I did include an example. 234 00:16:30,860 --> 00:16:34,080 If you wanted to wrap the node exporter itself, totally possible. 235 00:16:34,660 --> 00:16:43,640 And we now have the ability, if you want, if you don't want the service name to match the name that shows up on your mesh network, you can customize that now, too. 236 00:16:46,240 --> 00:16:46,860 Okay. 237 00:16:46,860 --> 00:16:49,600 So, we've added ourselves a new module. 238 00:16:49,620 --> 00:16:55,860 We've told it, go back, we've told it specifically that we want to wrap Grafana and Prometheus. 239 00:16:56,080 --> 00:16:59,000 There we've just got empty dictionaries. 240 00:16:59,040 --> 00:17:02,940 We're not doing any per-service config, but that's there if you want to. 241 00:17:04,340 --> 00:17:12,060 Now we're going to make a couple little updates just to take better advantage now that we know we're transitioning to a mesh-first model. 242 00:17:12,200 --> 00:17:14,580 So, first things up, we're going to update Prometheus. 243 00:17:15,080 --> 00:17:21,920 In particular, what we're going to add here, because NixOS is great, you can do a lot of this configuration statically. 244 00:17:21,920 --> 00:17:29,500 Instead of having to configure it after you get the service running, we can add static scrape configs at build time, at configuration time. 245 00:17:29,500 --> 00:17:41,660 And so, in this case, we're going to add a single target, which is going to be node 1, and then you'll see that's a tailnet internal domain, local to tailscale, but basically that's the name for my mesh network. 246 00:17:41,920 --> 00:17:50,040 So, it's going to find node 1 on the mesh, and then the second part is me just grabbing the port out of the config to be concise, but you totally don't need to do that. 247 00:17:50,040 --> 00:17:52,580 You could hard-code it or pull it dynamically or whatever. 248 00:17:52,980 --> 00:18:01,520 So, this says Prometheus, scrape over the mesh network to talk to node 1, and then we'll continue on with our Grafana. 249 00:18:02,140 --> 00:18:06,280 Here, we will update Grafana also to add some static configuration. 250 00:18:06,400 --> 00:18:11,380 In this case, we're going to tell it we want to do some provisioning and we want to configure a data source. 251 00:18:11,420 --> 00:18:12,480 What data source is that? 252 00:18:12,480 --> 00:18:14,580 Well, it's the Prometheus we've been setting up. 253 00:18:14,740 --> 00:18:19,840 So, we can just go in there, data sources, name Prometheus, customize whatever you need. 254 00:18:19,840 --> 00:18:25,780 Same thing, the main important point here is way at the bottom, we are going to talk to Prometheus. 255 00:18:25,780 --> 00:18:29,980 Grafana is going to talk to Prometheus over the tail net, over the mesh network. 256 00:18:29,980 --> 00:18:32,040 It doesn't have to care anything else about it. 257 00:18:34,900 --> 00:18:45,740 Okay, and then this is the part where we have to wait to make it... I mean, I'm not doing a live demo because, you know, conference Wi-Fi, though it sounds like they've done incredible work to get that going pretty well. 258 00:18:45,800 --> 00:18:48,580 So, I'm just going to do a NixOS rebuild switch here. 259 00:18:48,580 --> 00:18:51,640 This is going to apply those changes that we were just talking about. 260 00:18:51,720 --> 00:18:54,220 It also gives me a chance to just take a minute . 261 00:18:54,220 --> 00:18:55,140 We can wait for that to happen. 262 00:18:55,140 --> 00:18:57,460 I'll take a drink. 263 00:19:03,680 --> 00:19:05,060 We're almost there. 264 00:19:05,060 --> 00:19:09,160 Probably should have used a smaller system config, but, you know, I like to have a lot of packages. 265 00:19:13,060 --> 00:19:17,000 And this is the part where we figure out, okay, so we're done. 266 00:19:17,200 --> 00:19:26,700 We've stopped grafana.service and prometheus.service and then we'll see the combination of NixOS and the activation scripts and systemd. 267 00:19:27,260 --> 00:19:33,680 NixOS figured out we changed grafana and prometheus, so it knows it needs to stop and restart those with the new configurations. 268 00:19:34,080 --> 00:19:46,020 And then, because we've set systemd up to know about all sort of the dependencies to make the networking work, we'll see that it then spits out the following new units were started with a whole bunch of crap. 269 00:19:46,020 --> 00:19:50,160 So, that's kind of the new stuff that the module has added to make it work. 270 00:19:53,060 --> 00:19:57,620 And then, I think, if we continue here, we'll take a quick peek. 271 00:19:58,380 --> 00:19:58,740 Okay. 272 00:19:58,740 --> 00:20:05,220 So, that is me rerunning the modern netstat to take a look at what is listening on the host. 273 00:20:05,940 --> 00:20:08,920 And all we see now is the node exporter. 274 00:20:08,920 --> 00:20:17,600 So, we did that update, we did a NixOS rebuild to apply the configuration changes, and suddenly, grafana and prometheus have vanished from our system. 275 00:20:21,280 --> 00:20:22,080 Okay. 276 00:20:22,080 --> 00:20:26,500 So, surveying our work, we head over to the TailScale dashboard. 277 00:20:26,500 --> 00:20:27,500 What do we see? 278 00:20:27,660 --> 00:20:33,760 Well, now, we see same node one we had before, but we also see grafana and prometheus. 279 00:20:33,760 --> 00:20:40,740 And they get automatically assigned an internal IP address by TailScale, and they get DNS entries added in TailScale's internal DNS. 280 00:20:40,740 --> 00:20:46,080 So, now, anything connected to that network can just ask to talk to grafana, and it's going to get the right thing. 281 00:20:48,440 --> 00:20:52,480 And then, locally, we can just pop into our browser and type in prometheus. 282 00:20:52,480 --> 00:21:02,800 In this case, I went with the full domain, but if you set up search domain, which TailScale does by default, you can just go on a, you know, just type prometheus or grafana into the browser, and the rest will just work. 283 00:21:02,900 --> 00:21:07,720 So, we pop over, we pull up a browser, we type in prometheus, and what do we get? 284 00:21:07,720 --> 00:21:10,900 Well, for one, we actually connect to it. 285 00:21:10,900 --> 00:21:14,820 And then, after that, we see that it did, in fact, take that configuration. 286 00:21:14,900 --> 00:21:22,120 It now has a single endpoint to scrape, which is our node one, and it is up and online and was able to complete a scrape. 287 00:21:22,720 --> 00:21:24,740 So, did it work with grafana? 288 00:21:26,100 --> 00:21:35,480 Oh, actually, before we get to grafana, my bad, just to confirm here, we do also see that that connection to... oh, no, this is... oh, yeah, right. 289 00:21:36,400 --> 00:21:38,780 Go back, prometheus. 290 00:21:38,780 --> 00:21:39,920 Here we go. 291 00:21:39,920 --> 00:21:41,060 We are onto grafana. 292 00:21:41,060 --> 00:21:47,520 So, when we reconfigured grafana, we had told it to talk to prometheus, because we needed data for our dashboards. 293 00:21:47,520 --> 00:21:54,480 So, this is just a quick first glance to confirm, yes, indeed, prometheus server URL is pointing to our mesh network name. 294 00:21:55,040 --> 00:21:59,640 And then, we can continue on in grafana, pull up a dashboard, and what do we get? 295 00:21:59,640 --> 00:22:01,700 We get node one's metrics. 296 00:22:02,000 --> 00:22:04,680 Now, doesn't seem like much has happened. 297 00:22:04,860 --> 00:22:13,080 On the surface, it all kind of just works like it was before, except now, we're no longer trapped to a single box or a single layer. 298 00:22:13,800 --> 00:22:21,620 We have node exporter running on the laptop just totally as normal, exposed by a tail scale out to the wider network. 299 00:22:21,900 --> 00:22:36,100 And then, prometheus and grafana running wherever they want, it just happens to be on the same laptop, wrapped by the mesh sidecar module, isolated from the host, that's why they vanished from our port scan, and they're exposed only via the tail scale sidecar. 300 00:22:36,100 --> 00:22:40,180 So, we've actually managed to escape the LAN with mesh network sidecars. 301 00:22:41,560 --> 00:22:42,980 Okay, how does it work? 302 00:22:42,980 --> 00:22:43,960 What actually happened? 303 00:22:43,960 --> 00:22:51,440 Well, most of it is network namespaces, which is a delightful kernel feature that gives you a totally separate networks deck, essentially. 304 00:22:51,640 --> 00:22:57,420 You can think of it as just the networking isolation of container engines and docker and podman and the like. 305 00:22:57,420 --> 00:23:08,560 So, you can still see the root file system, you can still use all your files, but it's a great way to run things isolated, either just with no network connectivity, say you're doing, you know, a software build, you want to make sure it doesn't reach out anywhere, 306 00:23:09,420 --> 00:23:11,560 or just for VPNs, like we'll do today. 307 00:23:11,900 --> 00:23:14,560 But to make that work, first things first, we're going to need to switch. 308 00:23:14,560 --> 00:23:18,900 So, in this case, we build a virtual bridge, and that's just on the normal host networking stack. 309 00:23:19,420 --> 00:23:26,100 And then, to connect everything together, and this is the part where we're kind of just aping Docker Compose's style and building ourselves a virtual local network. 310 00:23:27,020 --> 00:23:39,900 To connect things together, we need to use network namespaces per service for isolation, and then virtual Ethernet pairs, or VEs, which you can think of as basically just a virtual Ethernet cable. 311 00:23:39,900 --> 00:23:46,980 It has two ends, you know, and they'll let you connect networks or hosts or services or whatever you want between your different network namespaces. 312 00:23:46,980 --> 00:23:49,360 Just a little bit of network plumbing for the kernel. 313 00:23:49,760 --> 00:23:55,300 So, we have one end on the host in this case, and the other one goes in our per service namespace. 314 00:23:55,300 --> 00:24:03,220 So, each service has its own namespace, and each namespace gets a link back to the main host namespace, which actually has access to the outside world. 315 00:24:04,180 --> 00:24:06,320 From that, we add each of those pairs. 316 00:24:06,320 --> 00:24:22,880 We essentially plug in the host side into our virtual switch so that they can all talk outward, and then we do some basic routing, some NAT, DHCP, that kind of thing, just like Docker Compose does, so that anything on the gets a NAT-ed connection out by that outbound interface that we configured. 317 00:24:23,440 --> 00:24:27,140 And then, after that, you get a whole lot of system D2. 318 00:24:28,100 --> 00:24:33,420 But, just because that was a lot of text, here's a simplified version in a mermaid diagram. 319 00:24:34,000 --> 00:24:38,020 This is just, let's say we weren't running Prometheus, this is just a Grafana example. 320 00:24:38,460 --> 00:24:48,600 Go to your host OS, the browser talks via the host tail scale over the mesh, over to the Grafana namespace, where it comes in over the Grafana sidecar tail scale. 321 00:24:48,660 --> 00:24:54,580 That lets it talk to the Grafana service, and then that can go all the way back and actually render Grafana in your browser. 322 00:24:55,080 --> 00:25:12,160 And then, at the same time, the Grafana service can use the Grafana sidecar, well, really, the Grafana sidecar uses the VEF to talk to bridge 0 so that it can actually go talk to the tail scale control plane, connect things, and ultimately forward all of your mesh VPN traffic out to the rest of the network. 323 00:25:13,820 --> 00:25:22,020 Same deal here, just the expanded example that was the demo we actually did today, where we have Grafana, Prometheus, and our node exporter. 324 00:25:22,080 --> 00:25:29,280 And the main takeaway really is just that the node exporter lives regularly on the laptop. 325 00:25:29,280 --> 00:25:36,120 Prometheus and Grafana do too, but they don't care because as long as they just talk over the mesh, they don't have to care. 326 00:25:36,120 --> 00:25:39,580 Tail scale, Netbird, Nebula, it'll figure the rest out. 327 00:25:39,580 --> 00:25:43,540 And especially it just works nicely because you can just move them wherever you'd like. 328 00:25:45,220 --> 00:25:55,560 And you can maybe see hopefully in the diagram that each one has its own namespace, there's the host one, and then probably the node exporter should be in the host namespace too, but main point is they talk over the mesh. 329 00:25:57,400 --> 00:25:59,580 Okay, here's a bit of systemd. 330 00:26:00,180 --> 00:26:01,340 What did we actually do? 331 00:26:01,340 --> 00:26:03,720 We're wrapping services, but what does that mean? 332 00:26:03,720 --> 00:26:04,960 How did it change? 333 00:26:04,960 --> 00:26:08,440 It didn't really look like we touched Grafana or Prometheus. 334 00:26:08,720 --> 00:26:17,740 Under the hood, we modified the systemd config that NixOS modules gave us to start, and then we added this little block highlighted in red. 335 00:26:17,740 --> 00:26:21,240 Well, not the description, the sorting just isn't great, so ignore that part. 336 00:26:21,240 --> 00:26:27,620 In particular, we added binds to, and that tells systemd basically that tail scale is a hard dependency. 337 00:26:27,620 --> 00:26:32,220 So if we stop Grafana or we stop tail scale, they'll both stop. 338 00:26:32,220 --> 00:26:34,020 If we start one, they'll both start. 339 00:26:34,080 --> 00:26:35,260 Things like that. 340 00:26:35,340 --> 00:26:38,620 And that's where the nice graph dependency aspect of systemd comes in. 341 00:26:38,920 --> 00:26:41,140 And then joins namespace out. 342 00:26:41,140 --> 00:26:52,260 That's the killer feature here, where if we start a service with its own namespace, in this case tail scale, we run tail scale, it gets its own network namespace with joins namespace of. 343 00:26:52,260 --> 00:26:58,940 Whatever we add that to will automatically join that namespace and be network isolated, won't get the default behavior. 344 00:27:01,200 --> 00:27:07,040 And if we go take a look at what the networking looks like on the host, you can ignore probably the first four. 345 00:27:07,040 --> 00:27:09,120 Those are normal standard connections. 346 00:27:09,120 --> 00:27:11,940 But starting at number five, we've got tail scale zero. 347 00:27:11,940 --> 00:27:15,900 And that's the normal host tail scale, like if you just run tail scale anywhere. 348 00:27:16,300 --> 00:27:20,420 Br zero, that is our virtual switch or bridge on the host. 349 00:27:20,420 --> 00:27:24,160 And then we see we have Grafana and Prometheus interfaces. 350 00:27:24,920 --> 00:27:28,800 Those are the host side of those virtual ethernet pairs I was talking about. 351 00:27:28,800 --> 00:27:31,820 And we'll see their other side as we go take a look. 352 00:27:31,820 --> 00:27:40,680 Next, we can do sudo ip net ns exec Grafana to run commands in our Grafana service network namespace. 353 00:27:40,680 --> 00:27:45,180 And so here we just did ip adder, sorry if config, we did ip adder again. 354 00:27:45,180 --> 00:27:48,960 And here we have a separate loopback, each namespace gets its own loopback. 355 00:27:48,960 --> 00:27:50,680 And then we have eth zero. 356 00:27:51,720 --> 00:27:54,780 Eth zero is the other side of that virtual ethernet pair. 357 00:27:54,780 --> 00:28:00,480 So you have Grafana on the host, and you have eth zero, each service will have its own eth zero living in its namespace. 358 00:28:00,480 --> 00:28:04,820 And then of course, because we have a sidecar, you get tail scale zero in here as well. 359 00:28:04,820 --> 00:28:08,600 And that's the per service tail scale running just for Grafana. 360 00:28:11,280 --> 00:28:14,020 Okay, so that's how it works. 361 00:28:14,020 --> 00:28:15,460 But it's all new. 362 00:28:15,660 --> 00:28:18,260 I think it's safe to call it alpha. 363 00:28:18,480 --> 00:28:20,540 It still could have breaking changes. 364 00:28:20,540 --> 00:28:24,680 In particular, I would like to add Nebula support as a sort of first thing at the gate. 365 00:28:24,680 --> 00:28:27,060 So if that takes breaking changes, it'll happen. 366 00:28:27,060 --> 00:28:29,060 After that, I'm going to try not to. 367 00:28:29,700 --> 00:28:31,360 Yeah, like I said, more VPNs. 368 00:28:31,360 --> 00:28:32,860 I don't know, zero tier. 369 00:28:33,220 --> 00:28:34,900 There's a whole bunch of options. 370 00:28:34,900 --> 00:28:36,960 So if we can support it, I'm totally open. 371 00:28:37,240 --> 00:28:44,000 Also, one thing you can do with network namespaces, let's say you just have like a regular open VPN connection, you spin that up. 372 00:28:44,000 --> 00:28:56,200 If you make a network namespace, and then you move that VPN interface into that namespace as the only interface in there, then anything that runs in that namespace, that's all it gets. 373 00:28:56,200 --> 00:29:01,720 So if tail scale or open VPN goes down, it loses connectivity, it doesn't get a regular NAT and connection. 374 00:29:01,780 --> 00:29:09,620 And this is possible, because the kernel remembers, it essentially knows that like, when you started that open VPN interface, it lived in network namespace zero. 375 00:29:09,700 --> 00:29:13,400 And so it's still allowed that link, but nothing else in the new namespace gets it. 376 00:29:13,400 --> 00:29:16,540 So we could have more isolation than we have by default here. 377 00:29:16,540 --> 00:29:20,300 I was kind of copying the style Docker is familiar with, but we could take it to the next level. 378 00:29:20,460 --> 00:29:26,300 And then of course, whatever anyone else wants, you guys, the community, PR is welcome. 379 00:29:27,180 --> 00:29:29,460 And then just a final review at the end. 380 00:29:29,460 --> 00:29:32,520 This has been Mesh Network Sidecars for NixOS Services. 381 00:29:32,540 --> 00:29:42,780 We embrace the flat mesh network with per-application network entities, of course, powered by tail scale, network namespace, and systemd, and very much Nix. 382 00:29:42,780 --> 00:29:45,160 And if you'd like to try it, it's out now. 383 00:29:45,160 --> 00:29:49,200 Head on over to github.com slash NoblePane slash Mesh Sidecar. 384 00:29:49,200 --> 00:29:53,500 Thank you very much, and feel free to come find me for questions afterwards. 385 00:29:53,700 --> 00:29:54,340 Take care.