nixpkgs/nixos/modules/services/web-apps/suwayomi-server.nix

227 lines
7.0 KiB
Nix
Raw Normal View History

2024-01-18 21:10:47 +00:00
{ config, pkgs, lib, ... }:
let
cfg = config.services.suwayomi-server;
inherit (lib) mkOption mkEnableOption mkIf types;
2023-11-02 03:48:06 +00:00
format = pkgs.formats.hocon { };
2024-01-18 21:10:47 +00:00
in
{
options = {
services.suwayomi-server = {
2024-06-03 16:59:45 +00:00
enable = mkEnableOption "Suwayomi, a free and open source manga reader server that runs extensions built for Tachiyomi";
2024-01-18 21:10:47 +00:00
package = lib.mkPackageOption pkgs "suwayomi-server" { };
2024-01-18 21:10:47 +00:00
dataDir = mkOption {
type = types.path;
default = "/var/lib/suwayomi-server";
example = "/var/data/mangas";
description = ''
2024-01-18 21:10:47 +00:00
The path to the data directory in which Suwayomi-Server will download scans.
'';
};
user = mkOption {
type = types.str;
default = "suwayomi";
example = "root";
description = ''
2024-01-18 21:10:47 +00:00
User account under which Suwayomi-Server runs.
'';
};
group = mkOption {
type = types.str;
default = "suwayomi";
example = "medias";
description = ''
2024-01-18 21:10:47 +00:00
Group under which Suwayomi-Server runs.
'';
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = ''
2024-01-18 21:10:47 +00:00
Whether to open the firewall for the port in {option}`services.suwayomi-server.settings.server.port`.
'';
};
settings = mkOption {
type = types.submodule {
2023-11-02 03:48:06 +00:00
freeformType = format.type;
2024-01-18 21:10:47 +00:00
options = {
server = {
ip = mkOption {
type = types.str;
default = "0.0.0.0";
example = "127.0.0.1";
description = ''
2024-01-18 21:10:47 +00:00
The ip that Suwayomi will bind to.
'';
};
port = mkOption {
type = types.port;
default = 8080;
example = 4567;
description = ''
2024-01-18 21:10:47 +00:00
The port that Suwayomi will listen to.
'';
};
basicAuthEnabled = mkEnableOption ''
2024-06-03 16:59:45 +00:00
basic access authentication for Suwayomi-Server.
2024-01-18 21:10:47 +00:00
Enabling this option is useful when hosting on a public network/the Internet
'';
2024-01-18 21:10:47 +00:00
basicAuthUsername = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
2024-01-18 21:10:47 +00:00
The username value that you have to provide when authenticating.
'';
};
# NOTE: this is not a real upstream option
basicAuthPasswordFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/var/secrets/suwayomi-server-password";
description = ''
2024-01-18 21:10:47 +00:00
The password file containing the value that you have to provide when authenticating.
'';
};
downloadAsCbz = mkOption {
type = types.bool;
default = false;
description = ''
2024-01-18 21:10:47 +00:00
Download chapters as `.cbz` files.
'';
};
extensionRepos = mkOption {
type = types.listOf types.str;
default = [];
example = [
"https://raw.githubusercontent.com/MY_ACCOUNT/MY_REPO/repo/index.min.json"
];
description = ''
URL of repositories from which the extensions can be installed.
'';
};
2024-01-18 21:10:47 +00:00
localSourcePath = mkOption {
type = types.path;
default = cfg.dataDir;
defaultText = lib.literalExpression "suwayomi-server.dataDir";
example = "/var/data/local_mangas";
description = ''
2024-01-18 21:10:47 +00:00
Path to the local source folder.
'';
};
systemTrayEnabled = mkOption {
type = types.bool;
default = false;
description = ''
2024-01-18 21:10:47 +00:00
Whether to enable a system tray icon, if possible.
'';
};
};
};
};
description = ''
2024-01-18 21:10:47 +00:00
Configuration to write to {file}`server.conf`.
See <https://github.com/Suwayomi/Suwayomi-Server/wiki/Configuring-Suwayomi-Server> for more information.
'';
default = { };
example = {
server.socksProxyEnabled = true;
server.socksProxyHost = "yourproxyhost.com";
server.socksProxyPort = "8080";
};
};
};
};
config = mkIf cfg.enable {
assertions = [{
assertion = with cfg.settings.server; basicAuthEnabled -> (basicAuthUsername != null && basicAuthPasswordFile != null);
message = ''
[suwayomi-server]: the username and the password file cannot be null when the basic auth is enabled
'';
}];
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.settings.server.port ];
users.groups = mkIf (cfg.group == "suwayomi") {
suwayomi = { };
};
users.users = mkIf (cfg.user == "suwayomi") {
suwayomi = {
group = cfg.group;
# Need to set the user home because the package writes to ~/.local/Tachidesk
home = cfg.dataDir;
description = "Suwayomi Daemon user";
isSystemUser = true;
};
};
systemd.tmpfiles.settings."10-suwayomi-server" = {
"${cfg.dataDir}/.local/share/Tachidesk".d = {
mode = "0700";
inherit (cfg) user group;
};
};
systemd.services.suwayomi-server =
let
2023-11-02 03:48:06 +00:00
configFile = format.generate "server.conf" (lib.pipe cfg.settings [
2024-01-18 21:10:47 +00:00
(settings: lib.recursiveUpdate settings {
server.basicAuthPasswordFile = null;
server.basicAuthPassword =
if settings.server.basicAuthEnabled
then "$TACHIDESK_SERVER_BASIC_AUTH_PASSWORD"
else null;
})
2023-11-02 03:48:06 +00:00
(lib.filterAttrsRecursive (_: x: x != null))
2024-01-18 21:10:47 +00:00
]);
in
{
description = "A free and open source manga reader server that runs extensions built for Tachiyomi.";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
script = ''
${lib.optionalString cfg.settings.server.basicAuthEnabled ''
export TACHIDESK_SERVER_BASIC_AUTH_PASSWORD="$(<${cfg.settings.server.basicAuthPasswordFile})"
''}
${lib.getExe pkgs.envsubst} -i ${configFile} -o ${cfg.dataDir}/.local/share/Tachidesk/server.conf
${lib.getExe cfg.package} -Dsuwayomi.tachidesk.config.server.rootDir=${cfg.dataDir}
'';
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Type = "simple";
Restart = "on-failure";
StateDirectory = mkIf (cfg.dataDir == "/var/lib/suwayomi-server") "suwayomi-server";
};
};
};
meta = {
maintainers = with lib.maintainers; [ ratcornu ];
doc = ./suwayomi-server.md;
};
}