diff --git a/flake.lock b/flake.lock index eef414d..5cdb421 100644 --- a/flake.lock +++ b/flake.lock @@ -743,11 +743,11 @@ }, "nixos-hardware": { "locked": { - "lastModified": 1727613673, - "narHash": "sha256-qqIffTQfxMYo3MKQ9BoY2s2mdKZNnUiksdnxv81js9U=", + "lastModified": 1727665282, + "narHash": "sha256-oKtfbQB1MBypqIyzkC8QCQcVGOa1soaXaGgcBIoh14o=", "owner": "nixos", "repo": "nixos-hardware", - "rev": "f5c239fa9acb27f0a5326ba2949c00fada89ca9f", + "rev": "11c43c830e533dad1be527ecce379fcf994fbbb5", "type": "github" }, "original": { diff --git a/modules/librespeed/default.nix b/modules/librespeed/default.nix index 14e180f..011656f 100644 --- a/modules/librespeed/default.nix +++ b/modules/librespeed/default.nix @@ -71,12 +71,27 @@ in { useNginx = mkOption { description = '' Configure nginx for the LibreSpeed frontend. - This will only create a virtual host for the frontend and won't proxy all requests, - because the reported upload and download speeds are inaccurate if proxied. + This will only create a virtual host for the frontend and won't proxy all requests because, + the reported upload and download speeds are inaccurate if proxied. ''; default = cfg.domain != null; type = types.bool; }; + settings = mkOption { + description = '' + Override default test parameters. + See [speedtest_worker.js][link] for a list of possible values. + + [link]: https://github.com/librespeed/speedtest/blob/master/speedtest_worker.js#L39 + ''; + default = {}; + type = with types; nullOr (attrsOf (oneOf [ + bool + int + str + float + ])); + }; servers = mkOption { description = "LibreSpeed servers that should apper in the server list."; type = types.listOf (types.submodule { @@ -129,19 +144,36 @@ in { }; }; config = lib.mkIf cfg.enable (let - librespeedAssets = pkgs.runCommand "librespeed-assets" { + librespeedAssets = pkgs.runCommand "librespeed-assets" (let + mapValue = arg: if (lib.isBool arg) then + lib.boolToString arg + else if ((lib.isInt arg) || (lib.isFloat arg)) then + toString arg + else + "\"${lib.escape [ "\"" ] (toString arg)}\""; + + mapSettings = lib.pipe cfg.frontend.settings [ + (lib.mapAttrs (name: val: " s.setParameter(\"${lib.escape [ "\"" ] name}\",${mapValue val});")) + (lib.attrValues) + (lib.concatLines) + ]; + in { preferLocal = true; serversList = '' function get_servers() { return ${builtins.toJSON cfg.frontend.servers} } + function override_settings () { + ${mapSettings} + } ''; - } '' + }) '' cp -r ${pkgs.librespeed-rust}/assets $out chmod 666 $out/servers_list.js cat >$out/servers_list.js <<<"$serversList" substitute ${pkgs.librespeed-rust}/assets/index.html $out/index.html \ + --replace-fail "s.setParameter(\"telemetry_level\",\"basic\"); //enable telemetry" "override_settings();" \ --replace-fail "LibreSpeed Example" ${lib.escapeShellArg (lib.escapeXML cfg.frontend.pageTitle)} \ --replace-fail "PUT@YOUR_EMAIL.HERE" ${lib.escapeShellArg (lib.escapeXML cfg.frontend.contactEmail)} \ --replace-fail "TO BE FILLED BY DEVELOPER" ${lib.escapeShellArg (lib.escapeXML cfg.frontend.contactEmail)} @@ -180,15 +212,15 @@ in { server = "//${cfg.domain}:${toString cfg.settings.listen_port}"; } ]; + services.librespeed.frontend.settings = lib.mkIf cfg.frontend.enable { + telemetry_level = lib.mkDefault "basic"; + }; + services.librespeed.settings = let inherit (lib) mkDefault mkIf; in { assets_path = if (cfg.frontend.enable && !cfg.frontend.useNginx) then librespeedAssets - else pkgs.writeTextDir "index.html" '' - - - - ''; + else pkgs.writeTextDir "index.html" ""; bind_address = mkDefault "::"; listen_port = mkDefault 8989; @@ -205,7 +237,6 @@ in { tls_key_file = if (cfg.domain != null) then (mkDefault "/run/credentials/librespeed.service/key.pem") else (mkDefault ""); enable_tls = mkDefault (cfg.domain != null); - }; systemd.services = let