nixpkgs/nixos/modules/services/web-apps/kavita.nix
Silvan Mosberger 374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00

120 lines
3.2 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.kavita;
settingsFormat = pkgs.formats.json { };
appsettings = settingsFormat.generate "appsettings.json" (
{ TokenKey = "@TOKEN@"; } // cfg.settings
);
in
{
imports = [
(lib.mkChangedOptionModule
[ "services" "kavita" "ipAdresses" ]
[ "services" "kavita" "settings" "IpAddresses" ]
(
config:
let
value = lib.getAttrFromPath [ "services" "kavita" "ipAdresses" ] config;
in
lib.concatStringsSep "," value
)
)
(lib.mkRenamedOptionModule [ "services" "kavita" "port" ] [ "services" "kavita" "settings" "Port" ])
];
options.services.kavita = {
enable = lib.mkEnableOption "Kavita reading server";
user = lib.mkOption {
type = lib.types.str;
default = "kavita";
description = "User account under which Kavita runs.";
};
package = lib.mkPackageOption pkgs "kavita" { };
dataDir = lib.mkOption {
default = "/var/lib/kavita";
type = lib.types.str;
description = "The directory where Kavita stores its state.";
};
tokenKeyFile = lib.mkOption {
type = lib.types.path;
description = ''
A file containing the TokenKey, a secret with at 512+ bits.
It can be generated with `head -c 64 /dev/urandom | base64 --wrap=0`.
'';
};
settings = lib.mkOption {
default = { };
description = ''
Kavita configuration options, as configured in {file}`appsettings.json`.
'';
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
Port = lib.mkOption {
default = 5000;
type = lib.types.port;
description = "Port to bind to.";
};
IpAddresses = lib.mkOption {
default = "0.0.0.0,::";
type = lib.types.commas;
description = ''
IP Addresses to bind to. The default is to bind to all IPv4 and IPv6 addresses.
'';
};
};
};
};
};
config = lib.mkIf cfg.enable {
systemd.services.kavita = {
description = "Kavita";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = ''
install -m600 ${appsettings} ${lib.escapeShellArg cfg.dataDir}/config/appsettings.json
${pkgs.replace-secret}/bin/replace-secret '@TOKEN@' \
''${CREDENTIALS_DIRECTORY}/token \
'${cfg.dataDir}/config/appsettings.json'
'';
serviceConfig = {
WorkingDirectory = cfg.dataDir;
LoadCredential = [ "token:${cfg.tokenKeyFile}" ];
ExecStart = lib.getExe cfg.package;
Restart = "always";
User = cfg.user;
};
};
systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' 0750 ${cfg.user} ${cfg.user} - -"
"d '${cfg.dataDir}/config' 0750 ${cfg.user} ${cfg.user} - -"
];
users = {
users.${cfg.user} = {
description = "kavita service user";
isSystemUser = true;
group = cfg.user;
home = cfg.dataDir;
};
groups.${cfg.user} = { };
};
};
meta.maintainers = with lib.maintainers; [ misterio77 ];
}