2023-09-27 14:12:26 +10:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2023-09-27 17:06:33 +10:00
|
|
|
inherit (builtins) hashString;
|
|
|
|
inherit (lib) mkIf mkOption optionalAttrs types;
|
|
|
|
|
2023-09-27 14:12:26 +10:00
|
|
|
cfg = config.services.betanin;
|
|
|
|
|
|
|
|
defaultUser = "betanin";
|
|
|
|
defaultGroup = "betanin";
|
2023-09-27 15:00:25 +10:00
|
|
|
|
|
|
|
settingsFormat = pkgs.formats.toml { };
|
|
|
|
beetsFormat = pkgs.formats.yaml { };
|
2023-09-27 14:12:26 +10:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.betanin = {
|
|
|
|
enable = lib.mkEnableOption "betanin";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
description = "Package containing betanin program.";
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.betanin or (import ../../.. { inherit pkgs; }).betanin;
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
description = "Open ports in the firewall for the server.";
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
description = "Port to access betanin on.";
|
|
|
|
type = types.port;
|
|
|
|
default = 9393;
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
description = "User that the betanin program should run under.";
|
|
|
|
type = types.str;
|
|
|
|
default = defaultUser;
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
description = "Group that the betanin program should run under.";
|
|
|
|
type = types.str;
|
|
|
|
default = defaultGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
description = "Directory to store application data.";
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/betanin";
|
|
|
|
};
|
|
|
|
|
2023-09-27 15:00:25 +10:00
|
|
|
settings = mkOption {
|
2023-09-27 17:06:33 +10:00
|
|
|
type = settingsFormat.type;
|
|
|
|
default = { };
|
2023-09-27 15:00:25 +10:00
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
frontend = {
|
|
|
|
username = "foo";
|
2023-09-27 17:06:33 +10:00
|
|
|
password { _secret = "/run/secrets/betaninPasswordFile"; };
|
2023-09-27 15:00:25 +10:00
|
|
|
};
|
|
|
|
clients = {
|
2023-09-27 17:06:33 +10:00
|
|
|
api_key = { _secret = "/run/secrets/betaninApiKeyFile"; };
|
2023-09-27 15:00:25 +10:00
|
|
|
};
|
|
|
|
server = {
|
|
|
|
num_parallel_jobs = 1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2023-09-27 17:06:33 +10:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Configuration for betanin.
|
|
|
|
|
|
|
|
Options containing secret data should be set to an attribute set
|
|
|
|
containing the attribute `_secret` - a string pointing to a file
|
|
|
|
containing the value the option should be set to.
|
|
|
|
'';
|
2023-09-27 15:00:25 +10:00
|
|
|
};
|
|
|
|
|
2023-09-27 17:06:33 +10:00
|
|
|
beets.settings = mkOption {
|
2023-09-27 15:00:25 +10:00
|
|
|
type = beetsFormat.type;
|
2023-09-27 14:12:26 +10:00
|
|
|
default = { };
|
2023-09-27 17:06:33 +10:00
|
|
|
description = lib.mdDoc "Configuration for beets used by betanin.";
|
2023-09-27 14:12:26 +10:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-09-27 17:06:33 +10:00
|
|
|
services.betanin.settings = {
|
|
|
|
notifications = {
|
|
|
|
# Required to exist.
|
|
|
|
services = { };
|
|
|
|
strings = {
|
|
|
|
title = lib.mkDefault "[betanin] torrent `$name` $status";
|
|
|
|
body = lib.mkDefault "@ $time. view/use the console at http://127.0.0.1:${toString cfg.port}/$console_path";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-09-27 14:12:26 +10:00
|
|
|
|
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.port ];
|
|
|
|
};
|
|
|
|
|
2023-09-27 15:00:25 +10:00
|
|
|
systemd.services.betanin =
|
|
|
|
let
|
2023-09-27 17:06:33 +10:00
|
|
|
isSecret = v: lib.isAttrs v && v ? _secret && lib.isString v._secret;
|
|
|
|
sanitisedConfig = lib.mapAttrsRecursiveCond
|
|
|
|
(as: !isSecret as)
|
|
|
|
(_: v: if isSecret v then hashString "sha256" v._secret else v)
|
|
|
|
cfg.settings;
|
|
|
|
settingsFile = settingsFormat.generate "betanin.toml" sanitisedConfig;
|
|
|
|
|
|
|
|
secretPaths = lib.catAttrs "_secret" (lib.collect isSecret cfg.settings);
|
|
|
|
mkSecretReplacement = file: ''
|
|
|
|
replace-secret ${hashString "sha256" file} ${file} "${cfg.dataDir}/.config/betanin/config.toml"
|
|
|
|
'';
|
|
|
|
secretReplacements = lib.concatMapStrings mkSecretReplacement secretPaths;
|
|
|
|
|
|
|
|
beetsFile = beetsFormat.generate "betanin-beets.yaml" cfg.beets.settings;
|
2023-09-27 15:00:25 +10:00
|
|
|
in
|
|
|
|
{
|
|
|
|
description = "Betanin service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "networking.target" ];
|
|
|
|
environment = {
|
|
|
|
HOME = cfg.dataDir;
|
|
|
|
};
|
|
|
|
path = [ pkgs.replace-secret ];
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
mkdir -p ${cfg.dataDir}/.config/betanin \
|
2023-09-27 17:06:33 +10:00
|
|
|
${cfg.dataDir}/.local/share/betanin \
|
|
|
|
${cfg.dataDir}/.config/beets
|
2023-09-27 15:00:25 +10:00
|
|
|
|
2023-09-27 17:33:36 +10:00
|
|
|
ln -sf ${beetsFile} ${cfg.dataDir}/.config/beets/config.toml
|
2023-09-27 17:06:33 +10:00
|
|
|
cat ${settingsFile} > ${cfg.dataDir}/.config/betanin/config.toml
|
|
|
|
${secretReplacements}
|
2023-09-27 15:00:25 +10:00
|
|
|
|
|
|
|
${cfg.package}/bin/betanin --port ${toString cfg.port}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = lib.mkMerge [
|
|
|
|
{
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
PrivateTmp = true;
|
|
|
|
Restart = "always";
|
|
|
|
WorkingDirectory = cfg.dataDir;
|
|
|
|
}
|
|
|
|
(mkIf (cfg.dataDir == "/var/lib/betanin") {
|
|
|
|
StateDirectory = "betanin";
|
|
|
|
})
|
|
|
|
];
|
2023-09-27 14:12:26 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
users.users = optionalAttrs (cfg.user == defaultUser) {
|
|
|
|
${cfg.user} = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = cfg.group;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == defaultGroup) {
|
|
|
|
${cfg.group} = { };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|