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 @@ + + +
+ + + + + + +Selecting a server...
+