From a2b146e8ba8144a501eee0de144ef99449cf1d72 Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Wed, 3 Jan 2024 01:07:12 +0000 Subject: [PATCH] nixos: Add librespeed --- nixos/boxes/colony/vms/estuary/dns.nix | 1 + .../shill/containers/middleman/default.nix | 14 + .../vms/shill/containers/middleman/vhosts.nix | 8 + nixos/modules/_list.nix | 1 + nixos/modules/librespeed/default.nix | 76 +++ nixos/modules/librespeed/index.html | 491 ++++++++++++++++++ nixos/modules/tmproot.nix | 3 + pkgs/default.nix | 1 + pkgs/librespeed-go.nix | 26 + secrets/librespeed.toml.age | 13 + 10 files changed, 634 insertions(+) create mode 100644 nixos/modules/librespeed/default.nix create mode 100644 nixos/modules/librespeed/index.html create mode 100644 pkgs/librespeed-go.nix create mode 100644 secrets/librespeed.toml.age diff --git a/nixos/boxes/colony/vms/estuary/dns.nix b/nixos/boxes/colony/vms/estuary/dns.nix index 7748158..7fe8268 100644 --- a/nixos/boxes/colony/vms/estuary/dns.nix +++ b/nixos/boxes/colony/vms/estuary/dns.nix @@ -145,6 +145,7 @@ in http IN A ${assignments.internal.ipv4.address} http IN AAAA ${allAssignments.middleman.internal.ipv6.address} + librespeed IN CNAME http.${config.networking.domain}. valheim IN A ${assignments.internal.ipv4.address} valheim IN AAAA ${allAssignments.valheim-oci.internal.ipv6.address} diff --git a/nixos/boxes/colony/vms/shill/containers/middleman/default.nix b/nixos/boxes/colony/vms/shill/containers/middleman/default.nix index 1b5d183..33e021e 100644 --- a/nixos/boxes/colony/vms/shill/containers/middleman/default.nix +++ b/nixos/boxes/colony/vms/shill/containers/middleman/default.nix @@ -66,6 +66,7 @@ in owner = "nginx"; group = "nginx"; }; + "librespeed.toml" = { }; }; }; @@ -122,6 +123,19 @@ in baseURL = "https://sso.${pubDomain}"; }; }; + + librespeed = { + frontend.servers = [ + { + name = "Amsterdam, Netherlands"; + server = "//librespeed.${domain}"; + } + ]; + backend = { + enable = true; + extraSettingsFile = config.age.secrets."librespeed.toml".path; + }; + }; }; users = { diff --git a/nixos/boxes/colony/vms/shill/containers/middleman/vhosts.nix b/nixos/boxes/colony/vms/shill/containers/middleman/vhosts.nix index 77ee9b6..a774142 100644 --- a/nixos/boxes/colony/vms/shill/containers/middleman/vhosts.nix +++ b/nixos/boxes/colony/vms/shill/containers/middleman/vhosts.nix @@ -357,6 +357,14 @@ in locations."/".proxyPass = "http://simpcraft-oci.${domain}:8100"; useACMEHost = pubDomain; }; + + "librespeed.${domain}" = { + locations."/".proxyPass = "http://localhost:8989"; + }; + "speed.${pubDomain}" = { + locations."/".proxyPass = "http://localhost:8989"; + useACMEHost = pubDomain; + }; }; minio = diff --git a/nixos/modules/_list.nix b/nixos/modules/_list.nix index 899bc1d..8fbf0f3 100644 --- a/nixos/modules/_list.nix +++ b/nixos/modules/_list.nix @@ -19,5 +19,6 @@ borgthin = ./borgthin.nix; nvme = ./nvme; spdk = ./spdk.nix; + librespeed = ./librespeed; }; } diff --git a/nixos/modules/librespeed/default.nix b/nixos/modules/librespeed/default.nix new file mode 100644 index 0000000..e0ae13e --- /dev/null +++ b/nixos/modules/librespeed/default.nix @@ -0,0 +1,76 @@ +{ lib, pkgs, config, ... }: +let + inherit (builtins) toJSON; + inherit (lib) mkOption mkMerge mkIf mkDefault; + inherit (lib.my) mkOpt' mkBoolOpt'; + + cfg = config.my.librespeed; + + serversConf = map (s: s // { + dlURL = "backend/garbage"; + ulURL = "backend/empty"; + pingURL = "backend/empty"; + getIpURL = "backend/getIP"; + }) cfg.frontend.servers; + frontendTree = pkgs.runCommand "librespeed-frontend" { + speedtestServers = toJSON serversConf; + } '' + mkdir "$out" + cp "${pkgs.librespeed-go}"/assets/* "$out"/ + substitute ${./index.html} "$out"/index.html --subst-var speedtestServers + ''; + + backendConf = pkgs.writers.writeTOML "librespeed.toml" cfg.backend.settings; + generateBackendSettings = base: dst: if (cfg.backend.extraSettingsFile != null) then '' + oldUmask="$(umask)" + umask 006 + cat "${base}" "${cfg.backend.extraSettingsFile}" > "${dst}" + umask "$oldUmask" + '' else '' + cp "${base}" "${dst}" + ''; +in +{ + options.my.librespeed = with lib.types; { + frontend = { + servers = mkOpt' (listOf (attrsOf unspecified)) { } "Server configs."; + webroot = mkOption { + description = "Frontend webroot."; + type = package; + readOnly = true; + }; + }; + backend = { + enable = mkBoolOpt' false "Whether to enable librespeed backend."; + settings = mkOpt' (attrsOf unspecified) { } "Backend settings."; + extraSettingsFile = mkOpt' (nullOr str) null "Extra settings file."; + }; + }; + + config = mkMerge [ + (mkIf (cfg.frontend.servers != { }) { + my.librespeed.frontend.webroot = frontendTree; + }) + (mkIf cfg.backend.enable { + my.librespeed.backend.settings = { + assets_path = frontendTree; + database_type = mkDefault "bolt"; + database_file = mkDefault "/var/lib/librespeed-go/speedtest.db"; + }; + + systemd.services.librespeed = { + description = "LibreSpeed Go backend"; + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + + preStart = generateBackendSettings backendConf "/run/librespeed-go/settings.toml"; + serviceConfig = { + ExecStart = "${pkgs.librespeed-go}/bin/speedtest -c /run/librespeed-go/settings.toml"; + RuntimeDirectory = "librespeed-go"; + StateDirectory = "librespeed-go"; + }; + wantedBy = [ "multi-user.target" ]; + }; + }) + ]; +} diff --git a/nixos/modules/librespeed/index.html b/nixos/modules/librespeed/index.html new file mode 100644 index 0000000..b10d936 --- /dev/null +++ b/nixos/modules/librespeed/index.html @@ -0,0 +1,491 @@ + + + + + + + + + +/dev/player0's speedtest + + +

/dev/player0's speedtest

+
+

Selecting a server...

+
+ + + + diff --git a/nixos/modules/tmproot.nix b/nixos/modules/tmproot.nix index 0fa2128..fcef861 100644 --- a/nixos/modules/tmproot.nix +++ b/nixos/modules/tmproot.nix @@ -480,6 +480,9 @@ in (mkIf config.services.tailscale.enable { my.tmproot.persistence.config.directories = [ "/var/lib/tailscale" ]; }) + (mkIf config.my.librespeed.backend.enable { + my.tmproot.persistence.config.directories = [ "/var/lib/librespeed-go" ]; + }) ])) ]); diff --git a/pkgs/default.nix b/pkgs/default.nix index 8af243b..eae416f 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -6,4 +6,5 @@ in # yeah turns out this is in nixpkgs now... we'll leave it as a sample i guess lol monocraft' = callPackage ./monocraft.nix { }; vfio-pci-bind = callPackage ./vfio-pci-bind.nix { }; + librespeed-go = callPackage ./librespeed-go.nix { }; } diff --git a/pkgs/librespeed-go.nix b/pkgs/librespeed-go.nix new file mode 100644 index 0000000..6588282 --- /dev/null +++ b/pkgs/librespeed-go.nix @@ -0,0 +1,26 @@ +{ lib, fetchFromGitHub, buildGoModule, ... }: +let + webSrc = fetchFromGitHub { + owner = "librespeed"; + repo = "speedtest"; + rev = "5.3.0"; + hash = "sha256-OgKGLQcfWX/sBLzaHI6TcJHxX1Wol6K7obLf0+CHrC8="; + }; +in +buildGoModule rec { + pname = "librespeed-go"; + version = "1.1.5"; + + src = fetchFromGitHub { + owner = "librespeed"; + repo = "speedtest-go"; + rev = "v${version}"; + hash = "sha256-ywGrodl/mj/WB25F0TKVvaV0PV4lgc+KEj0x/ix9HT8="; + }; + vendorHash = "sha256-ev5TEv8u+tx7xIvNaK8b5iq2XXF6I37Fnrr8mb+N2WM="; + + postInstall = '' + mkdir -p "$out"/assets + cp "${webSrc}"/{speedtest.js,speedtest_worker.js,favicon.ico} "$out"/assets/ + ''; +} diff --git a/secrets/librespeed.toml.age b/secrets/librespeed.toml.age new file mode 100644 index 0000000..b1a5bd4 --- /dev/null +++ b/secrets/librespeed.toml.age @@ -0,0 +1,13 @@ +-----BEGIN AGE ENCRYPTED FILE----- +YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IFZGY3c1ZyA4N0Qw +bFd4Q01yb3M3M05Ua1BPZjNsK2cwYlBuSUM3TXRaa2hPU2wxb1hzCnhoTzNMYVNT +bjRUTzFJdjdZVG1IVWJNa29PNjVVQ2NWbFdnNWNjcDk1bk0KLT4gWDI1NTE5IGVu +d2x5N21ZaW5MeHBzWkw5QnV0SUNiZTJBVlhXOFZacUFJVktNQXI4MTgKYmxQYmlC +YXJvYjhKbWVZWGRPWWh0eTlIZTJPSFZTZmt4UHJ1M2thNHRCMAotPiBvPls2N3ct +Z3JlYXNlIGowLGEgUFNKKWljTX4gInUKMkFwNGRiZUJCbUZhN2Z3YnZnZ3Nna0tD +UCtrODlldWZ0Rk5INmxTd0tmTGx1NS8rQUtTdVFwSVROcjVqeUJaWAplNWd3SUR5 +MjRzeXlyemtka2EwdVZDTkowTk5NMk1rdUxCdkR5ZUJOCi0tLSA4bWVKMXFTalg4 +ODFIN0hsTS9lekdWZVI2VTNKYlE3UzhXNk5nM1ZCMlowCn3ZLml2b8qDs+A2W8Nn +YGCfDV+aes39ef/2wu6EMISradXZQrzhSj8JHn7HqsarqSQmOhYP5juawEFxAuZJ +kUc= +-----END AGE ENCRYPTED FILE-----