NixOS is really nice for self hosting. Anything that has a NixOS module can be hosted in a few lines of nix code. But what if the service we want to host doesn’t come with a NixOS module written for us already in Nixpkgs? This is where NixOS can be a little hard, as a guide on setting up a service in Debian or Arch will rarely work on NixOS. Of course, the ’nix way’ would be to write your own package and module for it, but that can be a daunting task. Here are some ’escape hatches’ to host some of the simpler services without having to write your own Nix package or module.
Nginx
If the application is a simple static website, containing just HTML and JS,
the nginx
module on NixOS provides us with a way to manage virtual hosts complete with https.
Shown is how I host my Hugo generated blog.
|
|
The complete list of options for virtual hosts can be found here
Docker
If the service publishes a Docker image, one can just run that on NixOS. Here’s how I host a game server using a premade docker container. Things get a bit more complicated with docker-compose, but one can use compose2nix to translate a docker-compose.yaml file into a nix file much like the one shown.
|
|
There are, of course, more options for the oci-containers module, found here
Systemd
Finally, if the service is composed of a single static binary, NixOS makes it really easy to write Systemd services.
(I’ve used a package in Nixpkgs here,
but you could just as easily point the Systemd service to a binary you threw in /opt/
or somewhere.)
|
|
And like the last 2 times, the complete list of options for Systemd service can be found here
(This article was originally published in issue #6 of the Paged Out! magazine.)