nixfiles/modules/librespeed/default.nix

294 lines
9.9 KiB
Nix
Raw Normal View History

2024-09-24 23:56:26 +02:00
{ config, lib, pkgs, ... }:
let
cfg = config.services.librespeed;
in {
options.services.librespeed = let
inherit (lib) mkOption types;
in {
enable = lib.mkEnableOption "LibreSpeed server";
package = lib.mkPackageOption pkgs "librespeed-rust" {};
configureNginx = mkOption {
description = "Configure nginx as a reverse proxy for LibreSpeed.";
default = if (cfg.domain != null) then true else false;
type = types.bool;
};
contactEmail = mkOption {
description = "Email address listed in the privacy policy.";
default = if (cfg.domain != null) then "webmaster@${cfg.domain}" else "webmaster@${config.networking.fqdn}";
type = types.str;
};
domain = mkOption {
description = ''
If not `null`, this will add an entry to `services.librespeed.servers` and
configure an nginx reverse proxy at the specified FQDN, unless explicitly disabled.
'';
default = null;
type = with types; nullOr nonEmptyStr;
};
openFirewall = mkOption {
description = ''
Whether to open the firewall for the specified port.
This is only necessary if no reverse proxy is used.
'';
default = false;
type = types.bool;
};
pageTitle = mkOption {
description = "Title of the webpage.";
default = "LibreSpeed";
type = types.str;
};
secrets = mkOption {
description = ''
Attribute set of filesystem paths.
The contents of the specified paths will be read at service start time and merged with the attributes provided in `settings`.
'';
default = {};
type = with types; nullOr (attrsOf path);
};
servers = mkOption {
description = "LibreSpeed servers that should apper in the server list.";
type = types.listOf (types.submodule {
options = let
inherit (types) nonEmptyStr;
in {
name = mkOption {
description = "Name shown in the server list.";
type = nonEmptyStr;
};
server = mkOption {
description = "URL to the server. You may use `//` instead of `http://` or `https://`.";
type = nonEmptyStr;
};
dlURL = mkOption {
description = ''
URL path to download test on this server.
Append `.php` to the default value if the server uses the php implementation.
'';
default = "backend/garbage";
type = nonEmptyStr;
};
ulURL = mkOption {
description = ''
URL path to upload test on this server.
Append `.php` to the default value if the server uses the php implementation.
'';
default = "backend/empty";
type = nonEmptyStr;
};
pingURL = mkOption {
description = ''
URL path to latency/jitter test on this server.
Append `.php` to the default value if the server uses the php implementation.
'';
default = "backend/empty";
type = nonEmptyStr;
};
getIpURL = mkOption {
description = ''
URL path to IP lookup on this server.
Append `.php` to the default value if the server uses the php implementation.
'';
default = "backend/getIP";
type = nonEmptyStr;
};
};
});
};
settings = mkOption {
description = ''
LibreSpeed configuration written as Nix expression.
All values set to `null` will be excluded from the evaluated config.
This is useful if you want to omit certain defaults when using a different LibreSpeed implementation.
See [github.com/librespeed][librespeed] for configuration help.
[librespeed]: https://github.com/librespeed/speedtest-rust
'';
default = {};
type = with types; nullOr (attrsOf (oneOf [
2024-09-27 00:06:27 +02:00
(nullOr bool)
2024-09-24 23:56:26 +02:00
int
str
2024-09-27 00:06:27 +02:00
package
2024-09-24 23:56:26 +02:00
]));
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.configureNginx -> cfg.domain != null;
message = ''
`services.librespeed.configureNginx` requires `services.librespeed.domain` to be set.
'';
}
];
networking.firewall = lib.mkIf (cfg.openFirewall) {
allowedTCPPorts = [ cfg.settings.listen_port ];
};
services.nginx.virtualHosts = lib.mkIf cfg.configureNginx {
${cfg.domain} = {
locations."/" = {
2024-09-27 00:06:27 +02:00
proxyPass = "http://[::1]:${toString cfg.settings.listen_port}";
2024-09-24 23:56:26 +02:00
recommendedProxySettings = true;
2024-09-27 00:06:27 +02:00
extraConfig = ''
proxy_cache off;
proxy_buffering off;
proxy_request_buffering off;
'';
2024-09-24 23:56:26 +02:00
};
enableACME = true;
forceSSL = true;
2024-09-27 00:06:27 +02:00
extraConfig = ''
gzip off;
'';
2024-09-24 23:56:26 +02:00
};
};
security.acme.certs = lib.mkIf cfg.configureNginx {
${cfg.domain} = {};
};
services.librespeed.servers = lib.mkIf (cfg.domain != null) [
{
name = cfg.domain;
2024-09-27 00:06:27 +02:00
server = "//${cfg.domain}${lib.optionalString (!cfg.configureNginx) ":${toString cfg.settings.listen_port}"}";
2024-09-24 23:56:26 +02:00
}
];
services.librespeed.settings = let
inherit (lib) mkDefault mkIf;
assets = pkgs.runCommand "librespeed-assets" {
preferLocal = true;
2024-09-27 00:06:27 +02:00
serversList = ''
2024-09-24 23:56:26 +02:00
function get_servers() {
return ${builtins.toJSON cfg.servers}
}
'';
} ''
cp -r ${pkgs.librespeed-rust}/assets $out
2024-09-27 00:06:27 +02:00
chmod 666 $out/servers_list.js
cat >$out/servers_list.js <<<"$serversList"
2024-09-24 23:56:26 +02:00
substitute ${pkgs.librespeed-rust}/assets/index.html $out/index.html \
--replace-fail "LibreSpeed Example" ${lib.escapeShellArg (lib.escapeXML cfg.pageTitle)} \
--replace-fail "PUT@YOUR_EMAIL.HERE" ${lib.escapeShellArg (lib.escapeXML cfg.contactEmail)} \
--replace-fail "TO BE FILLED BY DEVELOPER" ${lib.escapeShellArg (lib.escapeXML cfg.contactEmail)}
'';
in {
2024-09-27 00:06:27 +02:00
#speed_test_dir = assets;
assets_path = assets;
bind_address = mkDefault (if cfg.configureNginx then "::1" else "::");
2024-09-24 23:56:26 +02:00
listen_port = mkDefault 8989;
2024-09-27 00:06:27 +02:00
#base_url = mkDefault "backend";
#worker_threads = mkDefault "auto";
2024-09-24 23:56:26 +02:00
2024-09-27 00:06:27 +02:00
server_lat = 0;
server_lng = 0;
proxyprotocol_port = 0;
redact_ip_addresses = false;
2024-09-24 23:56:26 +02:00
#librespeed-rust will fail to start if the following config parameters are omitted.
ipinfo_api_key = mkIf (!cfg.secrets ? "ipinfo_api_key") "";
stats_password = mkIf (!cfg.secrets ? "stats_password") "";
2024-09-27 00:06:27 +02:00
#tls_key_file = mkDefault "";
#tls_cet_file = mkDefault "";
2024-09-24 23:56:26 +02:00
enable_tls = mkDefault false;
2024-09-27 00:06:27 +02:00
} // rec {
database_type = mkDefault "none";
database_file = mkIf (database_type == "sqlite") (mkDefault "/var/lib/librespeed/speedtest.sqlite");
2024-09-24 23:56:26 +02:00
};
systemd.services = let
configFile = let
2024-09-27 00:06:27 +02:00
mapValue = arg: if (lib.isBool arg) then
2024-09-24 23:56:26 +02:00
lib.boolToString arg
else if (lib.isInt arg) then
toString arg
else "\"${lib.escape [ "\"" ] (toString arg)}\"";
in
with lib; pipe cfg.settings [
(filterAttrs (_: val: val != null))
2024-09-27 00:06:27 +02:00
(mapAttrs (name: val: "${name}=${mapValue val}"))
(attrValues)
(concatLines)
2024-09-24 23:56:26 +02:00
(pkgs.writeText "${cfg.package.name}-config.toml")
];
in {
librespeed-secrets = lib.mkIf (cfg.secrets != {}) {
description = "LibreSpeed secret helper";
ExecStart = let
script = pkgs.writeShellApplication {
name = "librespeed-secrets";
runtimeInputs = [ pkgs.coreutils ];
text = ''
cp ${configFile} ''${RUNTIME_DIRECTORY%%:*}/config.toml
'' + lib.pipe cfg.secrets [
(lib.mapAttrs (name: file: ''
cat >>''${RUNTIME_DIRECTORY%%:*}/config.toml <<EOF
${name}="$(<${lib.escapeShellArg file})"
EOF
''))
(lib.concatLines lib.attrValues)
];
};
in lib.getExe script;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
RuntimeDirectory = "librespeed";
UMask = "u=rw";
};
};
librespeed = {
description = "LibreSpeed server daemon";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
requires = lib.optionals (cfg.secrets != {}) [ "librespeed-secrets.service" ];
serviceConfig = {
Type = "simple";
Restart = "always";
DynamicUser = true;
2024-09-27 00:06:27 +02:00
#ExecStartPre = lib.mkIf (!cfg.secrets ? "ipinfo_api_key") "${lib.getExe cfg.package} --update-ipdb";
2024-09-24 23:56:26 +02:00
ExecStart = "${lib.getExe cfg.package} -c ${if (cfg.secrets == {}) then configFile else "\${RUNTIME_DIRECTORY%%:*}/config.toml"}";
WorkingDirectory = "/var/cache/librespeed";
RuntimeDirectory = "librespeed";
RuntimeDirectoryPreserve = true;
StateDirectory = "librespeed";
CacheDirectory = "librespeed";
SyslogIdentifier = "librespeed";
ReadOnlyPaths = [ cfg.package ];
RestrictSUIDSGID = true;
RestrictNamespaces = true;
PrivateTmp = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
ProtectSystem = "strict";
ProtectHome = true;
ProtectProc = "invisible";
SystemCallArchitectures = "native";
SystemCallFilter = "@system-service";
SystemCallErrorNumber = "EPERM";
LockPersonality = true;
NoNewPrivileges = true;
};
};
};
};
meta.maintainers = with lib.maintainers; [ snaki ];
}