INT. CONFERENCE STAGE
 
Xe is standing at the podium with a laptop visible. The title slide is visible. The presentation is using the Tailscale slidehead, but with Xe's sigil visible on the title slide.
 
XE
Good morning everyone! Say this happens to you: you've been coding nonstop on something you want to share with your friends and it works on your MacBook. You want it to stay up when your MacBook goes to sleep or you get on a plane or something, and all you have to do is the easy task of putting it into production. It's just simple, right?
 
The slide shows the docker logo.
 
XE
Just add a Dockerfile, they say! So you do that and then you have a Docker image that you can push to your target machine and then you find out that you can't just push it from machine to machine, you have to push it to a registry.
 
The slide adds the docker registry logo.
 
XE
So you make an account on the Docker hub only to find out that their rate limits are very aggressive so you have to move to something like GHCR and aggressively cache all your images there so you don't run afoul of the comically small Docker Hub rate limits which will block your attempts to deploy it to your cloud provider of choice.
 
The slide adds a cloud emoji.
 
XE
So you do that and you pull this on a VM running on someone else's computer, and then you need to figure out the other fun part:
 
The slide adds an nginx logo.
 
XE
You need to configure nginx. Of course it uses its own bespoke configuration language that no other program on the planet uses (this is an unfortunately common pattern in our industry) so it's even more googling for that. But then you realize you need to configure the real final boss of the internet:
 
The slide adds the text "DNS" and Terraform/lego logos.
 
XE
DNS. It's never DNS until it's always DNS. So you install the artist formerly known as Terraform, lego, and provision your DNS and HTTPS certificates (because of course nginx doesn't just natively have this support in anno dominium two thousand and twenty three like any sensible HTTP reverse proxy should). And then you're finally done. It's taken you an hour to hack up the service and a whole 8 hours to research and implement everything to deploy it. This is madness. Why do we have to put up with this?
 
The slide shows an AWS reference architecture diagram for tamagotchis.
 
XE
The koolaid runs deep in the cloud too, if you're not careful you'll end up accidentally making an entire event sourcing platform with an unrealistic amount of complexity to manage something as simple as a tamagochi. You're just trying to make an HTTP service show up on the internet, you don't need to know what an ALB, EKS, ECS, IAM, or PFA is.
 
The slide clears and shows a Kubernetes logo.
 
XE
Of course, complain about this online and a certain tangerine community funded by big YAML will decry that you should use Kubernetes to simplify all this down to "simple" and "easy to understand" things, conveniently ignoring that they use a string templating language for structured data.
 
The slide goes blank.
 
XE
There has got to be something simpler, right? What if you didn't have to deal with nearly any of that? What if you could just push and run your binary on a home server and then access it? No dealing with the cloud. No dealing with security groups or IAM or DNS or HTTPS or any of the slings and arrows of outrageous investment. What if you could just describe the state of the system you want instead of going three layers deep into a side of devops hell that you will never return from unscathed?
 
The slide shows the NixOS logo.
 
XE
This is the real value of NixOS. Today I'm going to show you how to turn an arbitrary Go program into a NixOS service and then I'll expose it to the world thanks to Tailscale Funnel. This means you can link it to your group chat of friends and restore balance to the force.
 
The slide shows a speaker introduction.
 
XE
All that said, let me introduce myself. I'm Xe yah-soh, I write that one blog that you keep finding when you google Nix and NixOS stuff. I'm a writer, gamer, philosopher of chaos magick, and my blog regularly gets to the front page of popular computer news aggregators you may have heard of.
 
The slide shows an agenda for the talk.
 
XE
Today I'm going to cover a few core things so you can make your own NixOS modules: I'm going to cover what a NixOS module is and why you should care, the parts of one, how to make your own, and then I'm going to tempt the demo gods by doing a live deployment to a virtual machine on my MacBook.
 
The slide shows a picture of hands.
 
XE
Before I get started though, let's get some exercise in. Raise your hand if this is your first exposure to Nix and/or NixOS.
(Pause)
Alright, thanks.
 
The slide shows Mara+hacker.
 
XE
Raise your hand if you've ever used it before.
(Pause)
That's about what I expected.
 
The slide shows Cadey+percussive-maintenance.
 
XE
How about if you have it installed on a server at home?
(Pause)
Okay, okay, I see.
 
The slide shows an AWS logo.
 
XE
How about if your employer uses it in production?
(Pause)
Oh, wow, okay. That makes sense. You can lower your hands now.
 
The slide shows the Nix logo.
 
XE
Just so we're on the same page, Nix is a package manager that lets you declare all of the inputs for a program and get the same output from the build process.
 
The slide shows the Go function signature of func Build(inputs []Package, instructions BuildInfo) (Package, error).
 
XE
One of the main ways that Nix stands out from the crowd is the idea that package builds are functions. They effectively take in inputs, use them against some instructions, and then either return a package or the build fails due to an error. Because there were no other options at the time, Nix uses its own programming language also named Nix to define packages.
Remember, Nix was the result of lamentations at the state of software and this was the result.
 
The slide shows the holy trinity of Nix.
 
XE
To help you understand, I've put up this helpful diagram. Nixpkgs the standard library uses Nix the language, but it is not NixOS the operating system. I like to think about NixOS like this:
NixOS is the natural consequence of using Nix to build Linux systems. You can think about NixOS as a bunch of prebaked snippets of configuration that you can combine info a running system that does what you want. Each of those snippets is called a module. Nixpkgs (the standard library in Nix land) ships with a bunch of them that do things from compiling systemd to configuring Tailscale for you. Here's a simple NixOS module from my homelab:
 
The slide embeds github:Xe/flake-configs/main/hardware/alrest/monitoring.nix.
 
XE
A NixOS module is a function that takes the current state of the world and returns things to change in it. The module I'm showing here is from my homelab, specifically the part that enables the prometheus node exporter so that I can report when machines suddenly go offline or their hard drives are going bad. This is a very simple example. When you import it, it always takes effect. There's no flags to enable it or disable it. This is fine for my usecase however, because I want my homelab cluster to always be monitored. Things get a lot more fun when you add options into the mix:
 
The slide embeds github:Xe/flake-configs/common/home-manager/vim/default.nix.
 
XE
Compare it to this module, this is a dot file management module that sets up my vimrc on my machines. I have the option `within.vim.enable`, and if that is set to true, the vim configuration is dropped in place. If it's not set to true, it won't put the vim configuration in the system. NixOS modules have options and configuration outputs. Options let you customize the configuration to meet your spacebar heating needs.
 
The slide embeds a contrived example of importing and using that module.
 
XE
To use this, you'd add the path to the file to an `imports` output of the module, then add a `within.vim.enable = true` statement inside your home-manager configuration.
 
The slide goes blank.
 
XE
The state of the world is the input, and any new changes are the outputs. This lets you build a Linux system exactly the way you want to.
 
The slide shows an example configuration snippet for configuring Nginx in a NixOS module.
 
XE
Above all else: you can configure programs like nginx directly in your NixOS configuration without having to learn how to write nginx config, saving you from having to configure every single program on your system in its own bespoke ways.
(Pause to let it sink in)
 
The slide shows Mara+happy.
 
XE
Of course, things become a lot more fun when you can build your own NixOS modules that have your own programs running on your own machines. Let's do that with an example program that shows quotes from the legendary British science fiction author Douglas Adams.
 
The slide shows a picture of the end result.
 
XE
This is what the end result will look like. It'll be the quotes on a screen that refreshes every time you press F5. This will let you spread the undeniable wisdom of the late and great Douglas Adams, author of the five part trilogy The Hitchhiker's Guide to the Galaxy.
 
The slide shows a Mermaid diagram of the setup.
 
XE
So overall, the infrastructure setup will look like this: my MacBook and the VM are both connected to each other with Tailscale. When I enable Tailscale Funnel, the VM is going to have its HTTPS port opened up to the public internet so that you can visit this service running on a VM, on my MacBook, on conference Wi-Fi. Let's hope the demo gods are in our favour!
 
The slide shows Tailscale, DNS, and Let's Encrypt logos.
 
XE
By the way, Tailscale's gonna take care of the DNS and Let's Encrypt problems for us. No having to figure that out in the conference!
 
The slides exit in favour of a VS Code instance. The VS Code instance has the douglas-adams-quotes repo loaded with a few variants of the flake.nix file in each tab.
 
XE
I've opened a VS Code session with an "empty" flake configuration. Nix flakes let you create a set of packages, development environments, NixOS modules, and even end to end integration tests. To start, this flake will import nixpkgs:
 
Highlight the inputs.nixpkgs.url line.
 
XE
And then it declares a devShells.default for every one of these supported systems:
 
Highlight the supported systems line.
 
XE
This basically has Nix create development environments and packages for amd64 macOS, amd64 Linux, arm64 macOS, and arm64 Linux. This should cover most of the targets that y'all in the audience use. If you really need more, you can add other systems like riscv64 Linux, but for now these should be more than sufficient.
 
Highlight the packages in the devShell.
 
XE
This adds the packages relevant to the project to the development environment. This is a Go project with some CSS managed by Tailwind, so it's got the Go compiler, some Go development tools, npm, and yarn. A pretty normal set of things really.
 
Switch to the terminal view.
 
XE
Just to make sure that the CSS is up to date, I'm going to run `yarn start:css` in another shell to rebuild when any of the template files change.
 
Type in `yarn start:css` and hit enter.
 
XE
Now that we have that, let's see how we would add a package to the flake. One of the flake output kinds is Nix packages, so we make an output named packages and paste in some boilerplate to get a Go package working.
 
Switch to the version of flake.nix that has a package defined.
 
XE
The package would look something like this. This forAllSystems / nixpkgsFor hack is something you can work around with flake-utils, but for right now I'm doing everything manually. This is basically a bunch of predefined copies of nixpkgs for all the supported architectures, much like there's a devshell for every supported architecture. Either way, we get a Go module built into a package, and we define the dependency hash as null because this is only using the standard library. It's called default in the flake because it's best practice to name your package that.
 
Switch to a terminal.
 
XE
Just to test it, you can run `nix build` to build the default package in that flake.nix file:
 
Run `nix build`, it comes up clean and happy.
 
XE
Perfect! It builds! The binary is in ./result/bin/ and we can run it wherever we want.
(Run it with --help to prove it works)
See? If it didn't work we wouldn't get this far!
 
The file changes to a version of the flake.nix with a NixOS module partially defined.
 
XE
We defined the devShell to build the program development. We defined the package to build the software, and now we'll define the module to tell NixOS how to run the software. This is a basic NixOS module. It's defined inline to the flake for now, moving it to its own file is an exercise for the reader.
 
The file moves to the top of the module.
 
XE
Like I said before, a NixOS module is a function that takes in the state of the world and returns new additions to the state of the world. This NixOS module provides some options under `xe.services.douglas-adams-quotes` and then if the module is enabled, it creates a new systemd service to run it in. We're in the future, so we can use fancy things like DynamicUser to avoid having to run this service as root.
 
The options section is expanded.
 
XE
The real fun part comes when you define options for the service. Every one of these options correlates to CLI flags so you can change various options on the fly. It's good practice to map any non-secret configuration settings to options so that users can have easy escape hatches for changing things like the HTTP bind port or log level to get debug output. Secrets are a more complicated thing due to how Nix works, so we're not going to talk about those today.
(Pause a moment)
 
The file is scrolled all the way to the top.
 
XE
So we have everything we need now. We have development environment configuration, a package build, and finally a NixOS module to get the service running. The last step is to push it into prod. I have a NixOS virtual machine set up for this on my MacBook, but you may want to run this somewhere else, such as in Hyper-V on your gaming tower. Or maybe the cloud, I won't judge!
 
The VS Code sidebar is opened and the Tailscale extension is opened.
 
XE
Now we get to the fun part, enabling the NixOS module. I'm going to use the VS Code Tailscale extension to SSH in and open up the files in my VM, so lemme do that real quick.
 
Navigate to /etc/nixos.
 
XE
Let's peer into my VM's deployment flake and see what we can do to deploy it. This is a brand new, never opened VM, the only thing I did was set up a flake in /etc/nixos/flake.nix that imports the autogenerated configuration from the installer. This allows us to import things like the Douglas Adams Quotes service into the VM.
 
The /etc/nixos/flake.nix file is opened.
 
XE
Here's what the file looks like. I import nixos unstable, and then I create a nixos configuration for a machine named douglas-adams. This "modules" block has a list of NixOS module filenames or literal expressions. This lets you import NixOS modules from other flakes and define your own NixOS modules on the fly.
 
The version of the file that imports the Douglas Adams quotes service is loaded.
 
XE
To make it import this, first we add a new input that points to the Douglas Adams quotes flake. This then gets threaded into the outputs function, we import the module, and finally enable it on the system.
(Pause)
That's it. That's how you enable the service on the VM. Now all that's left is SSHing in and running a nixos-rebuild to enable it.
 
The terminal comes back after right-clicking into the douglas-adams node.
 
XE
I just right-click on the douglas-adams node, ssh in with Tailscale handling the auth, and then run the magic nixos-rebuild command: nixos-rebuild switch --flake /etc/nixos and hit enter.
 
The command is typed in. The enter key is pressed for dramatic effect.
 
XE
And now the source code gets pulled, the package gets built, the service gets created and then:
 
A systemctl status command is written.
 
XE
We can see that the process is running. Let's prove that it's working:
 
A curl command is written against the instance.
 
XE
Et voila. But, we aren't stopping there. I also enabled serving it with Tailscale Funnel so that you can see it on your phones. Get your phones out, I'm gonna be showing a QR Code:
 
We go back to the slides and show a QR code and URL pointing to a live install of the douglas-adams-quotes service.
 
XE
Scan this QR code. You can trust me, right? It's not gonna be a Rick Roll. I'm not that mean. When you do, you'll connect to my VM on my laptop on the conference wifi, yet still exposed to the public internet.
(Pause for applause)
 
The slide shows a conclusion.
 
XE
In conclusion, NixOS modules aren't hard. It's just options to configure systemd or nginx or even Tailscale. It's a function that takes in the state of the world and returns new parts to add to the whole. This gives you a nearly infinite level of composition and logistical freedom to implement whatever you want. Every systemd option is exposed as a NixOS flag. Your programs can become services trivially. It's just that easy. I promise.
 
The title slide is shown again.
 
XE
But, now you know how to make your own NixOS modules for fun and (hopefully) profit!
 
The GReeTZ slide is shown.
 
XE
Before we get this talk wrapped up, I just want to thank everyone on this list for helping me make this talk shine. Thanks everyone!
(Pause for applause)
 
The final slide is shown with a per-talk email address.
 
XE
And with that, I've been Xe! Thank you so much for coming to this talk. I hope you've had a good conference and I'll be wandering around in case you have any questions. I've posted a summary of the code samples on my blog at xeiaso.net so you can look into my code some more.
Oh by the way, if you're looking, Tailscale is hiring. I know it's probably rare to see someone like this at a Linux conference but if you're a Windows expert please let me know. That role has been so hard to fill.
I try to answer every question I can, but if I don't get to you, please email dynamicuser at xeserv dot us and I'll reply to your questions as soon as I can.
Thanks again to the All Systems Go organizers for having me here and I hope you continue having a good day. Be well!