technical-writing/paged-out!/nixos-easy-host/main.typ

114 lines
3.7 KiB
Typst

#import "template.typ": article
#show: article.with(
title: [Running non Nixpkgs services on NixOS, the lazy way],
authors: "Gabe Venberg",
date: datetime(year: 2024, month: 12, day: 04),
fontsize: 12pt
)
#show heading.where(level: 2): it => text(size: 12pt, it.body + [:])
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.
#set rect(
inset: 8pt,
fill: rgb("e4e5ea"),
width: 100%,
)
#grid(columns: (1fr, 2fr), gutter: 5mm,
[
== 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.
],
rect[
```nix
{ config, ... }: {
services.nginx.virtualHosts."gabevenberg.com" = {
enableACME = true;
forceSSL = true;
root = "/var/www/gabevenberg.com";
};
security.acme = {
acceptTerms = true;
defaults.email = "myname@example.com";
};
networking.firewall.allowedTCPPorts = [443 80];
}
```
],
grid.cell(
colspan: 2,
[
The complete list of options for virtual hosts can be found here:\
#link("https://nixos.org/manual/nixos/stable/options#opt-services.nginx.virtualHosts")
],
),
[
== 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 #link("https://github.com/aksiksi/compose2nix") to translate a docker-compose.yaml file into a nix file much like the one shown.
],
rect[
```nix
{ config, ... }: {
virtualisation.oci-containers = {
backend = "docker";
containers.factorio = {
image = "factoriotools/factorio:stable";
volumes = ["/storage/factorio:/factorio"];
hostname = "factorio";
ports = ["34197:34197/tcp"];
environment = {UPDATE_MODS_ON_START = "true";};
};
};
virtualisation.docker.enable = true;
}
```
],
grid.cell(
colspan: 2,
[
There are, of course, more options for the oci-containers module, found at:\
#link("https://nixos.org/manual/nixos/stable/options#opt-virtualisation.oci-containers.containers")
],
),
[
== 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.)
],
rect[
```nix
{ config, ... }: {
systemd.services.miniserve = {
wantedBy = ["multi-user.target"];
after = ["network.target"];
description = "A directory miniserve instance";
environment = {MINISERVE_ENABLE_TAR_GZ="true";}
serviceConfig.ExecStart = "${pkgs.miniserve}/bin/miniserve -i 127.0.0.1 -- /storage/miniserve"
};
}
```
],
grid.cell(
colspan: 2,
[
And like the last 2 times, the complete list of options for Systemd service can be found here:\
#link("https://nixos.org/manual/nixos/stable/options.html#opt-systemd.services")
],
))