[00:00.750 --> 00:01.440] All right. [00:01.440 --> 00:03.420] Hello, Nix Vegas. [00:05.500 --> 00:06.300] All right. [00:06.300 --> 00:07.740] So, my name is Wes Payne. [00:07.740 --> 00:10.440] Thank you for hanging out, learning more about Nix. [00:10.440 --> 00:15.500] And this talk is Mesh Network Sidecars for NixOS Services. [00:16.580 --> 00:17.320] All right. [00:17.320 --> 00:20.420] So, first up, just a little bit about me. [00:21.180 --> 00:31.640] I'm a Seattle-based software engineer interested in functional programming and distributed systems, specifically Clojure, and more recently, definitely Nix. [00:31.920 --> 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, [00:49.620 --> 00:51.960] we like to say the largest Linux podcast. [00:52.520 --> 00:55.820] I'm a co-host there on the Jupyter Broadcasting Network. [00:56.180 --> 01:01.060] And then, perhaps most importantly, pictured to the right here is my dog, Charlie, little old lady. [01:01.060 --> 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. [01:09.700 --> 01:12.340] But we're here for Nix. [01:12.340 --> 01:12.700] Okay. [01:12.700 --> 01:15.420] So, Mesh Networks, Sidecars, NixOS. [01:15.420 --> 01:17.060] Let's define some terms. [01:17.140 --> 01:20.300] So, first up is Mesh Network. [01:20.300 --> 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. [01:28.780 --> 01:32.640] But they're called Overlay Networks, they're called Peer-to-Peer VPNs. [01:32.640 --> 01:38.300] The basic idea is one flat network for all your devices and services. [01:38.300 --> 01:45.200] And in particular, it's probably good to contrast this with more hub-and-spoke style traditional VPNs. [01:45.200 --> 01:46.660] I'm sure many of us are familiar, right? [01:46.660 --> 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. [02:00.160 --> 02:04.740] Well, with a Mesh Network or an Overlay Network, the idea is everything lives on the Mesh. [02:04.740 --> 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. [02:16.320 --> 02:18.660] Or at least that's obscured and handled below the scene. [02:18.660 --> 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. [02:31.600 --> 02:36.180] I've been interested in this stuff for a while, and it's actually been around longer than you might think. [02:36.280 --> 02:39.760] In particular, Tink came out in 1998. [02:40.160 --> 02:44.600] It kind of has that classic, old, first-era internet vibe to it. [02:44.600 --> 02:49.560] It also feels very much like a traditional Unix daemon, so it's a little bit fun to work with. [02:49.700 --> 02:54.640] I saw a great talk at Linux Fest Northwest, where people had set up basically their own private LAN. [02:54.640 --> 02:57.740] This was probably 10 years ago, I don't know, 2015 or so. [02:57.800 --> 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. [03:05.320 --> 03:06.960] So, I got interested. [03:06.960 --> 03:13.260] I did basic stuff, like setting up backups from my house to my folks' place and vice versa. [03:14.380 --> 03:20.040] But these days, by modern standards, Tink just... I mean, it doesn't have great performance. [03:20.120 --> 03:25.360] But then also, it just was never really built with modern security principles in mind. [03:25.400 --> 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. [03:33.880 --> 03:40.560] That saw Slack Open Source Nebula, which was a mesh overlay network they developed in-house. [03:40.560 --> 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. [03:49.400 --> 03:52.320] I mean, it's a cloud permissions first world. [03:52.320 --> 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. [03:58.400 --> 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. [04:06.900 --> 04:10.140] And then, of course, Tailscale also came out in 2019. [04:10.300 --> 04:11.840] Netbird is another competitor. [04:11.840 --> 04:13.320] There's, like, a whole bunch more now. [04:13.320 --> 04:14.840] So, we're spoiled for choice. [04:14.840 --> 04:16.480] Tailscale is probably the most popular. [04:18.120 --> 04:18.900] There we go. [04:18.900 --> 04:19.160] Yes. [04:19.160 --> 04:23.560] And a great reminder, this talk is being streamed over Nebula right now. [04:24.060 --> 04:27.380] So, it's increasingly popular across all kinds of different scales. [04:27.380 --> 04:32.980] Simplifying Homelabs is something we see a lot, but also flattening, annoying multi-cloud and VPC networks. [04:34.200 --> 04:34.580] Okay. [04:34.580 --> 04:37.660] So, next up, then, for terms is Sidecar. [04:37.660 --> 04:39.600] And here, I thought we'd start with an example. [04:39.700 --> 04:41.240] I know this is the next talk, so I'm sorry. [04:41.240 --> 04:42.260] There is some YAML. [04:42.260 --> 04:45.540] I promise it's the only YAML sample that we'll see in this talk today. [04:45.820 --> 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. [04:52.780 --> 04:54.760] That part's not too important. [04:55.300 --> 05:00.040] Normal Docker Compose file, except we've got network mode. [05:00.040 --> 05:03.680] And that's set to service Tailscale Sidecar. [05:03.680 --> 05:07.020] And then we have a second container to find, and that's Tailscale Sidecar. [05:07.020 --> 05:08.800] And we're running Tailscale. [05:08.820 --> 05:11.540] In particular, here, we can customize the host name. [05:11.540 --> 05:13.920] We feed it some environment variables to configure it. [05:14.080 --> 05:15.740] And we poke some holes. [05:15.740 --> 05:19.840] We give it some permanent state here with volumes that it can store anything it needs to. [05:19.840 --> 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. [05:29.520 --> 05:33.640] You don't want to give this to everything, but to be an effective VPN, it needs them. [05:34.680 --> 05:37.720] So, this pattern got used a lot . [05:37.800 --> 05:39.180] It's used in the cloud. [05:39.180 --> 05:40.540] It's used in Kubernetes. [05:40.540 --> 05:44.800] And it's used a ton in the home lab, just with Docker Compose like that. [05:45.220 --> 05:47.020] And there are some reasons for this. [05:47.020 --> 05:50.860] So, right out of the gate, Docker, of course, gives you network isolation. [05:51.280 --> 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. [05:58.600 --> 06:03.000] And that means anything you run in this style is only exposed to the host if you want. [06:03.140 --> 06:06.900] It also gives you application-level networking. [06:06.900 --> 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. [06:18.600 --> 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. [06:25.900 --> 06:32.200] Because if they all use internal mesh addresses or names, from their perspective, nothing changed. [06:32.200 --> 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. [06:43.020 --> 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. [06:51.560 --> 06:55.480] Many of them have both kernel and user-space implementations for their networking. [06:56.140 --> 06:58.220] And we have containers. [06:58.680 --> 07:02.880] But this is Nix, and I want to stop talking about containers, right? [07:02.880 --> 07:05.260] So, let's talk about NixOS. [07:05.260 --> 07:06.540] This is NixVegas. [07:06.540 --> 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. [07:21.760 --> 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. [07:32.900 --> 07:36.400] And this is also kind of what, you know, containers let us do in many cases. [07:36.600 --> 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. [07:44.000 --> 07:47.960] They can run right together, even if one's based on Debian and another one's based on Alpine. [07:49.160 --> 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. [08:03.820 --> 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. [08:08.540 --> 08:11.360] And maybe we could just... we could just not. [08:11.880 --> 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. [08:21.780 --> 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. [08:25.840 --> 08:30.780] And then you also get NixOS modules, which set up and run services for you. [08:30.780 --> 08:32.060] So, it's not just a package. [08:32.060 --> 08:34.500] It's also all of the configuration on your host. [08:34.500 --> 08:35.920] It might poke holes in the firewall. [08:35.920 --> 08:37.340] It's going to set up storage locations. [08:37.340 --> 08:39.500] It's going to run systemd services for you. [08:39.540 --> 08:41.600] And that's pre-configured, ready to go. [08:41.620 --> 08:45.900] So, the one that really clicked for me the first time around, I mean, the Nginx module, great. [08:45.940 --> 08:47.260] Postgres, super easy. [08:47.320 --> 08:48.280] But Nix Cloud. [08:48.500 --> 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. [08:58.480 --> 09:02.820] So, we wanted to run it, and I was quite familiar with running it in a Docker style. [09:02.820 --> 09:04.920] But sometimes you need to mess with stuff, right? [09:04.920 --> 09:08.920] So, in the example on the right, we have max upload size. [09:09.200 --> 09:14.180] We've also got some things in there where we're setting PHP options around the opcache. [09:15.060 --> 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. [09:28.660 --> 09:29.800] There's no easy way to do it. [09:29.800 --> 09:31.020] It's not declarative. [09:31.380 --> 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. [09:38.940 --> 09:41.320] Like, all of that will just get updated for me. [09:42.360 --> 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. [09:50.260 --> 09:52.860] So, we're in good territory already. [09:55.100 --> 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. [10:05.540 --> 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. [10:13.420 --> 10:16.120] Unfortunately, with NixOS, it's not quite that easy. [10:16.120 --> 10:18.160] And you may ask, you know, there's other ways to do it. [10:18.160 --> 10:22.760] You can't just run regular old Tailscale on your host and get by with that. [10:22.760 --> 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. [10:36.420 --> 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. [10:44.980 --> 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. [10:59.940 --> 11:04.040] And also, various people just were sort of on the internet asserting that you couldn't do this. [11:04.040 --> 11:06.340] So, it's a bit of spike-driven development as well. [11:08.060 --> 11:08.560] Okay. [11:08.560 --> 11:10.920] So, part one, enter systemd. [11:10.920 --> 11:13.740] We're all systemd fans here, right? [11:13.800 --> 11:14.520] Right? [11:14.860 --> 11:18.980] In any case, NixOS, I think, loves systemd. [11:18.980 --> 11:20.760] I mean, you can use other things. [11:20.760 --> 11:21.900] NixOS is very flexible. [11:21.900 --> 11:27.240] But with Nix, you have this very powerful declarative build tool. [11:27.300 --> 11:30.660] But that really only exists at build time. [11:30.660 --> 11:34.740] And if you're running the system, runtime kind of matters. [11:35.360 --> 11:40.040] And systemd has grown to be a very flexible tool that can do... I mean, it can manage mount points. [11:40.040 --> 11:41.420] It can handle DNS queries. [11:41.420 --> 11:42.480] It can do networking. [11:42.480 --> 11:46.940] And, you know, you can configure just about everything on a modern Linux distribution with systemd. [11:47.000 --> 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. [11:59.720 --> 12:05.300] Also, as systemd has advanced, it's gotten a lot of new security and confinement options. [12:05.300 --> 12:06.780] I mean, these days, it's not even that new. [12:06.780 --> 12:10.620] But if you don't go spelunking in the docs, you might not know how many options exist. [12:10.620 --> 12:12.760] Like, you can have private devices. [12:12.760 --> 12:14.540] You can protect the home dir files. [12:14.540 --> 12:16.160] You can protect system files. [12:16.160 --> 12:20.540] You can make sure that no, you know, certain types of address families are allowed for connections or not. [12:20.540 --> 12:24.740] And all of that's just in the regular old ini file for configuring systemd. [12:25.600 --> 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. [12:37.250 --> 12:37.890] Okay. [12:37.890 --> 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. [12:47.970 --> 12:52.170] I started my career off working in a NOC doing monitoring and observability. [12:52.170 --> 12:53.910] So, those have always been important to me. [12:54.130 --> 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. [13:02.190 --> 13:03.510] So, we'll start there. [13:03.510 --> 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. [13:17.990 --> 13:19.090] This is all great. [13:19.090 --> 13:24.730] But, of course, without any help, it's all kind of trapped on one box or on one network. [13:26.130 --> 13:28.290] So, here is that config. [13:28.290 --> 13:31.210] Up top, we have services.grafana. [13:31.210 --> 13:33.170] Pretty standard for NixOS modules. [13:33.170 --> 13:37.130] Turn it on with enable, and then you can pass it some settings. [13:37.130 --> 13:42.710] In this case, you can tweak the listening address, the port, enable optimizations like gzip, whatever. [13:42.710 --> 13:45.130] This is a pretty basic stock config you might use. [13:45.370 --> 13:48.570] And then, same thing down below, we have Prometheus. [13:48.830 --> 13:51.890] Making a listen, you can tweak stuff like scrape interval. [13:52.210 --> 13:54.810] And then in this case, we're setting up a node exporter. [13:54.830 --> 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. [14:00.950 --> 14:02.170] All totally possible. [14:02.170 --> 14:05.190] But for this, just a sort of basic collector to start. [14:08.130 --> 14:10.210] And what does that get us? [14:10.210 --> 14:15.870] Well, up top, you'll see a snapshot of my personal tailscale dashboard. [14:15.870 --> 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. [14:24.290 --> 14:26.850] So, all we have is node one. [14:26.850 --> 14:29.450] And that is this old ThinkPad running right here. [14:29.450 --> 14:33.730] It just has a regular tailscale running on the host, exposing the whole host. [14:34.590 --> 14:39.810] And then if we take a look below, we can look at what is listening on this host. [14:40.170 --> 14:41.490] And we've got three things. [14:41.490 --> 14:43.730] We've got Prometheus on 9090. [14:43.730 --> 14:46.030] We have node exporter on 9000. [14:46.030 --> 14:48.230] And Grafana on 3000. [14:48.350 --> 14:48.630] Okay. [14:48.630 --> 14:48.970] Great. [14:48.970 --> 14:49.650] Kind of expected. [14:49.650 --> 14:51.510] They're all exposed locally on the host. [14:51.530 --> 14:52.890] Nothing too fancy is going on. [14:52.890 --> 14:56.430] And all tailscale knows about is the actual host itself. [14:59.160 --> 15:02.260] And this is where my module comes in. [15:02.260 --> 15:03.820] The mesh sidecar module. [15:03.820 --> 15:05.760] So, you've got to add this to your configuration somehow. [15:05.760 --> 15:06.620] Not pictured. [15:06.660 --> 15:08.380] Come talk to me after if you want help with that. [15:08.380 --> 15:09.500] But Flakes make it easy. [15:09.500 --> 15:11.940] You can build it into your NixOS config, pull it in. [15:12.140 --> 15:14.000] Lots of options there. [15:14.580 --> 15:17.400] And so far, we've got just a simple interface. [15:18.160 --> 15:19.400] You start by turning it on. [15:19.400 --> 15:20.100] Say enable. [15:20.100 --> 15:20.680] Yep. [15:20.780 --> 15:22.480] And then you select a provider. [15:22.520 --> 15:27.400] Right now, the module supports tailscale and netbird, but open to others in the future. [15:27.460 --> 15:32.520] You do have to tell that maybe considering perhaps letting you do both at once down the line. [15:32.520 --> 15:34.100] But so far, one only. [15:34.340 --> 15:36.580] And then you're going to need an auth key. [15:36.800 --> 15:41.660] And both tailscale and netbird let you have auth keys to sort of dynamically add hosts. [15:41.660 --> 15:43.440] So, that's what this module relies on. [15:43.520 --> 15:44.780] Store it somewhere secure. [15:44.820 --> 15:46.780] We'll pick it up with systemd credentials. [15:46.780 --> 15:48.440] So, it never touches the Nix store. [15:48.440 --> 15:51.140] So, as long as you secure it, we won't mess it up. [15:51.720 --> 15:54.440] And then a little bit more manual config here. [15:54.440 --> 15:55.820] The bridge name, totally optional. [15:55.820 --> 15:57.140] You can customize that. [15:57.140 --> 15:59.080] We are going to make ourselves a network bridge. [15:59.080 --> 16:00.420] We'll get more into that. [16:00.440 --> 16:03.220] But right now, you do need to set the outbound interface. [16:03.280 --> 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. [16:09.200 --> 16:10.900] So, they're going to need a connection. [16:10.900 --> 16:16.220] So, we'll give them a regular NATed outbound connection, just kind of like Docker Compose does by default. [16:16.620 --> 16:19.720] So, here I'm choosing a wireless network WLP. [16:20.380 --> 16:25.960] And then the main meaty bit of this is the services section down there at the end. [16:26.540 --> 16:29.500] So, to start, we're just going to wrap Grafana and Prometheus. [16:29.600 --> 16:30.860] But I did include an example. [16:30.860 --> 16:34.080] If you wanted to wrap the node exporter itself, totally possible. [16:34.660 --> 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. [16:46.240 --> 16:46.860] Okay. [16:46.860 --> 16:49.600] So, we've added ourselves a new module. [16:49.620 --> 16:55.860] We've told it, go back, we've told it specifically that we want to wrap Grafana and Prometheus. [16:56.080 --> 16:59.000] There we've just got empty dictionaries. [16:59.040 --> 17:02.940] We're not doing any per-service config, but that's there if you want to. [17:04.340 --> 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. [17:12.200 --> 17:14.580] So, first things up, we're going to update Prometheus. [17:15.080 --> 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. [17:21.920 --> 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. [17:29.500 --> 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. [17:41.920 --> 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. [17:50.040 --> 17:52.580] You could hard-code it or pull it dynamically or whatever. [17:52.980 --> 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. [18:02.140 --> 18:06.280] Here, we will update Grafana also to add some static configuration. [18:06.400 --> 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. [18:11.420 --> 18:12.480] What data source is that? [18:12.480 --> 18:14.580] Well, it's the Prometheus we've been setting up. [18:14.740 --> 18:19.840] So, we can just go in there, data sources, name Prometheus, customize whatever you need. [18:19.840 --> 18:25.780] Same thing, the main important point here is way at the bottom, we are going to talk to Prometheus. [18:25.780 --> 18:29.980] Grafana is going to talk to Prometheus over the tail net, over the mesh network. [18:29.980 --> 18:32.040] It doesn't have to care anything else about it. [18:34.900 --> 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. [18:45.800 --> 18:48.580] So, I'm just going to do a NixOS rebuild switch here. [18:48.580 --> 18:51.640] This is going to apply those changes that we were just talking about. [18:51.720 --> 18:54.220] It also gives me a chance to just take a minute . [18:54.220 --> 18:55.140] We can wait for that to happen. [18:55.140 --> 18:57.460] I'll take a drink. [19:03.680 --> 19:05.060] We're almost there. [19:05.060 --> 19:09.160] Probably should have used a smaller system config, but, you know, I like to have a lot of packages. [19:13.060 --> 19:17.000] And this is the part where we figure out, okay, so we're done. [19:17.200 --> 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. [19:27.260 --> 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. [19:34.080 --> 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. [19:46.020 --> 19:50.160] So, that's kind of the new stuff that the module has added to make it work. [19:53.060 --> 19:57.620] And then, I think, if we continue here, we'll take a quick peek. [19:58.380 --> 19:58.740] Okay. [19:58.740 --> 20:05.220] So, that is me rerunning the modern netstat to take a look at what is listening on the host. [20:05.940 --> 20:08.920] And all we see now is the node exporter. [20:08.920 --> 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. [20:21.280 --> 20:22.080] Okay. [20:22.080 --> 20:26.500] So, surveying our work, we head over to the TailScale dashboard. [20:26.500 --> 20:27.500] What do we see? [20:27.660 --> 20:33.760] Well, now, we see same node one we had before, but we also see grafana and prometheus. [20:33.760 --> 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. [20:40.740 --> 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. [20:48.440 --> 20:52.480] And then, locally, we can just pop into our browser and type in prometheus. [20:52.480 --> 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. [21:02.900 --> 21:07.720] So, we pop over, we pull up a browser, we type in prometheus, and what do we get? [21:07.720 --> 21:10.900] Well, for one, we actually connect to it. [21:10.900 --> 21:14.820] And then, after that, we see that it did, in fact, take that configuration. [21:14.900 --> 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. [21:22.720 --> 21:24.740] So, did it work with grafana? [21:26.100 --> 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. [21:36.400 --> 21:38.780] Go back, prometheus. [21:38.780 --> 21:39.920] Here we go. [21:39.920 --> 21:41.060] We are onto grafana. [21:41.060 --> 21:47.520] So, when we reconfigured grafana, we had told it to talk to prometheus, because we needed data for our dashboards. [21:47.520 --> 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. [21:55.040 --> 21:59.640] And then, we can continue on in grafana, pull up a dashboard, and what do we get? [21:59.640 --> 22:01.700] We get node one's metrics. [22:02.000 --> 22:04.680] Now, doesn't seem like much has happened. [22:04.860 --> 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. [22:13.800 --> 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. [22:21.900 --> 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. [22:36.100 --> 22:40.180] So, we've actually managed to escape the LAN with mesh network sidecars. [22:41.560 --> 22:42.980] Okay, how does it work? [22:42.980 --> 22:43.960] What actually happened? [22:43.960 --> 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. [22:51.640 --> 22:57.420] You can think of it as just the networking isolation of container engines and docker and podman and the like. [22:57.420 --> 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, [23:09.420 --> 23:11.560] or just for VPNs, like we'll do today. [23:11.900 --> 23:14.560] But to make that work, first things first, we're going to need to switch. [23:14.560 --> 23:18.900] So, in this case, we build a virtual bridge, and that's just on the normal host networking stack. [23:19.420 --> 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. [23:27.020 --> 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. [23:39.900 --> 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. [23:46.980 --> 23:49.360] Just a little bit of network plumbing for the kernel. [23:49.760 --> 23:55.300] So, we have one end on the host in this case, and the other one goes in our per service namespace. [23:55.300 --> 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. [24:04.180 --> 24:06.320] From that, we add each of those pairs. [24:06.320 --> 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. [24:23.440 --> 24:27.140] And then, after that, you get a whole lot of system D2. [24:28.100 --> 24:33.420] But, just because that was a lot of text, here's a simplified version in a mermaid diagram. [24:34.000 --> 24:38.020] This is just, let's say we weren't running Prometheus, this is just a Grafana example. [24:38.460 --> 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. [24:48.660 --> 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. [24:55.080 --> 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. [25:13.820 --> 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. [25:22.080 --> 25:29.280] And the main takeaway really is just that the node exporter lives regularly on the laptop. [25:29.280 --> 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. [25:36.120 --> 25:39.580] Tail scale, Netbird, Nebula, it'll figure the rest out. [25:39.580 --> 25:43.540] And especially it just works nicely because you can just move them wherever you'd like. [25:45.220 --> 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. [25:57.400 --> 25:59.580] Okay, here's a bit of systemd. [26:00.180 --> 26:01.340] What did we actually do? [26:01.340 --> 26:03.720] We're wrapping services, but what does that mean? [26:03.720 --> 26:04.960] How did it change? [26:04.960 --> 26:08.440] It didn't really look like we touched Grafana or Prometheus. [26:08.720 --> 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. [26:17.740 --> 26:21.240] Well, not the description, the sorting just isn't great, so ignore that part. [26:21.240 --> 26:27.620] In particular, we added binds to, and that tells systemd basically that tail scale is a hard dependency. [26:27.620 --> 26:32.220] So if we stop Grafana or we stop tail scale, they'll both stop. [26:32.220 --> 26:34.020] If we start one, they'll both start. [26:34.080 --> 26:35.260] Things like that. [26:35.340 --> 26:38.620] And that's where the nice graph dependency aspect of systemd comes in. [26:38.920 --> 26:41.140] And then joins namespace out. [26:41.140 --> 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. [26:52.260 --> 26:58.940] Whatever we add that to will automatically join that namespace and be network isolated, won't get the default behavior. [27:01.200 --> 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. [27:07.040 --> 27:09.120] Those are normal standard connections. [27:09.120 --> 27:11.940] But starting at number five, we've got tail scale zero. [27:11.940 --> 27:15.900] And that's the normal host tail scale, like if you just run tail scale anywhere. [27:16.300 --> 27:20.420] Br zero, that is our virtual switch or bridge on the host. [27:20.420 --> 27:24.160] And then we see we have Grafana and Prometheus interfaces. [27:24.920 --> 27:28.800] Those are the host side of those virtual ethernet pairs I was talking about. [27:28.800 --> 27:31.820] And we'll see their other side as we go take a look. [27:31.820 --> 27:40.680] Next, we can do sudo ip net ns exec Grafana to run commands in our Grafana service network namespace. [27:40.680 --> 27:45.180] And so here we just did ip adder, sorry if config, we did ip adder again. [27:45.180 --> 27:48.960] And here we have a separate loopback, each namespace gets its own loopback. [27:48.960 --> 27:50.680] And then we have eth zero. [27:51.720 --> 27:54.780] Eth zero is the other side of that virtual ethernet pair. [27:54.780 --> 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. [28:00.480 --> 28:04.820] And then of course, because we have a sidecar, you get tail scale zero in here as well. [28:04.820 --> 28:08.600] And that's the per service tail scale running just for Grafana. [28:11.280 --> 28:14.020] Okay, so that's how it works. [28:14.020 --> 28:15.460] But it's all new. [28:15.660 --> 28:18.260] I think it's safe to call it alpha. [28:18.480 --> 28:20.540] It still could have breaking changes. [28:20.540 --> 28:24.680] In particular, I would like to add Nebula support as a sort of first thing at the gate. [28:24.680 --> 28:27.060] So if that takes breaking changes, it'll happen. [28:27.060 --> 28:29.060] After that, I'm going to try not to. [28:29.700 --> 28:31.360] Yeah, like I said, more VPNs. [28:31.360 --> 28:32.860] I don't know, zero tier. [28:33.220 --> 28:34.900] There's a whole bunch of options. [28:34.900 --> 28:36.960] So if we can support it, I'm totally open. [28:37.240 --> 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. [28:44.000 --> 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. [28:56.200 --> 29:01.720] So if tail scale or open VPN goes down, it loses connectivity, it doesn't get a regular NAT and connection. [29:01.780 --> 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. [29:09.700 --> 29:13.400] And so it's still allowed that link, but nothing else in the new namespace gets it. [29:13.400 --> 29:16.540] So we could have more isolation than we have by default here. [29:16.540 --> 29:20.300] I was kind of copying the style Docker is familiar with, but we could take it to the next level. [29:20.460 --> 29:26.300] And then of course, whatever anyone else wants, you guys, the community, PR is welcome. [29:27.180 --> 29:29.460] And then just a final review at the end. [29:29.460 --> 29:32.520] This has been Mesh Network Sidecars for NixOS Services. [29:32.540 --> 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. [29:42.780 --> 29:45.160] And if you'd like to try it, it's out now. [29:45.160 --> 29:49.200] Head on over to github.com slash NoblePane slash Mesh Sidecar. [29:49.200 --> 29:53.500] Thank you very much, and feel free to come find me for questions afterwards. [29:53.700 --> 29:54.340] Take care.