buzzrelay/nixos-module.nix

118 lines
3.2 KiB
Nix
Raw Normal View History

2022-12-19 23:37:05 +00:00
{ self }:
{ config, lib, pkgs, ... }: {
options.services.buzzrelay = with lib; {
enable = mkEnableOption "Enable Fedi.buzz relay";
2024-07-09 13:23:07 +00:00
package = mkOption {
type = types.package;
default = self.packages.${pkgs.system}.buzzrelay;
};
2023-01-11 17:45:45 +00:00
streams = mkOption {
2023-01-15 23:04:52 +00:00
type = with types; listOf str;
2023-01-11 17:45:45 +00:00
default = [
"https://fedi.buzz/api/v1/streaming/public"
];
};
2022-12-19 23:37:05 +00:00
listenPort = mkOption {
type = types.int;
default = 8000;
};
hostName = mkOption {
type = types.str;
};
privKeyFile = mkOption {
type = types.str;
};
pubKeyFile = mkOption {
type = types.str;
};
database = mkOption {
type = types.str;
default = "buzzrelay";
};
user = mkOption {
type = types.str;
default = "buzzrelay";
2022-12-19 23:37:05 +00:00
};
group = mkOption {
type = types.str;
default = "buzzrelay";
2022-12-19 23:37:05 +00:00
};
2023-06-23 23:28:12 +00:00
logLevel = mkOption {
type = types.enum [ "ERROR" "WARN" "INFO" "DEBUG" "TRACE" ];
default = "INFO";
};
2023-10-12 20:43:46 +00:00
redis = {
connection = mkOption {
type = with types; nullOr str;
default = null;
};
passwordFile = mkOption {
type = with types; nullOr path;
default = null;
};
inTopic = mkOption {
type = types.str;
default = "relay-in";
2023-10-12 20:43:46 +00:00
};
};
2022-12-19 23:37:05 +00:00
};
config =
let
cfg = config.services.buzzrelay;
2024-05-24 00:51:26 +00:00
configFile = builtins.toFile "buzzrelay.yaml" (
2022-12-19 23:37:05 +00:00
lib.generators.toYAML {} {
2023-01-11 17:45:45 +00:00
streams = cfg.streams;
2022-12-19 23:37:05 +00:00
hostname = cfg.hostName;
listen_port = cfg.listenPort;
priv_key_file = cfg.privKeyFile;
pub_key_file = cfg.pubKeyFile;
2022-12-20 00:09:09 +00:00
db = "host=/var/run/postgresql user=${cfg.user} dbname=${cfg.database}";
2023-10-12 20:43:46 +00:00
redis = if cfg.redis.connection != null
then {
connection = cfg.redis.connection;
password_file = cfg.redis.passwordFile;
in_topic = cfg.redis.inTopic;
}
2023-10-12 20:43:46 +00:00
else null;
2022-12-19 23:37:05 +00:00
});
2024-07-09 13:23:07 +00:00
buzzrelay = cfg.package;
2022-12-19 23:37:05 +00:00
in
lib.mkIf cfg.enable {
users.users.${cfg.user} = {
inherit (cfg) group;
isSystemUser = true;
};
users.groups.${cfg.group} = {};
services.postgresql = {
enable = true;
ensureDatabases = [ cfg.database ];
ensureUsers = [ {
name = cfg.user;
ensureDBOwnership = true;
2022-12-19 23:37:05 +00:00
} ];
};
systemd.services.buzzrelay = {
wantedBy = [ "multi-user.target" ];
after = [ "postgresql.service" "network-online.target" ];
2024-05-24 00:38:25 +00:00
wants = [ "network-online.target" ];
2023-06-23 23:28:12 +00:00
environment.RUST_LOG = "buzzrelay=${cfg.logLevel}";
2022-12-19 23:37:05 +00:00
serviceConfig = {
Type = "notify";
2022-12-23 17:21:05 +00:00
WorkingDirectory = "${buzzrelay}/share/buzzrelay";
ExecStart = "${buzzrelay}/bin/buzzrelay ${lib.escapeShellArg configFile}";
2022-12-19 23:37:05 +00:00
User = cfg.user;
Group = cfg.group;
ProtectSystem = "full";
Restart = "always";
RestartSec = "1s";
WatchdogSec = "1800s";
LimitNOFile = 40000;
2022-12-19 23:37:05 +00:00
};
};
};
}