dotfiles/modules/nixos/services/calibre.nix

84 lines
2.2 KiB
Nix
Raw Permalink Normal View History

2024-01-10 04:11:11 +00:00
# Calibre-web is an E-Book library and management tool.
# - Exposed to the public via Caddy.
# - Hostname defined with config.hostnames.books
# - File directory backed up to S3 on a cron schedule.
2023-03-04 01:04:02 +00:00
{ config, pkgs, lib, ... }:
let
libraryPath = "/data/books";
in {
2022-10-01 21:39:36 +00:00
2022-10-02 14:48:51 +00:00
options = {
2023-03-04 01:04:02 +00:00
backups.calibre = lib.mkOption {
type = lib.types.bool;
description = "Whether to backup Calibre library";
default = true;
};
2022-10-02 14:48:51 +00:00
};
2022-10-01 21:39:36 +00:00
config = lib.mkIf config.services.calibre-web.enable {
2022-10-01 21:39:36 +00:00
services.calibre-web = {
openFirewall = true;
options = {
reverseProxyAuth.enable = false;
enableBookConversion = true;
enableBookUploading = true;
2023-03-04 01:04:02 +00:00
calibreLibrary = libraryPath;
2022-10-01 21:39:36 +00:00
};
};
2024-01-10 04:11:11 +00:00
# Allow web traffic to Caddy
2022-12-21 21:18:03 +00:00
caddy.routes = [{
2023-07-07 16:16:07 +00:00
match = [{ host = [ config.hostnames.books ]; }];
handle = [{
handler = "reverse_proxy";
2023-08-05 01:20:38 +00:00
upstreams = [{
dial = "localhost:${
builtins.toString config.services.calibre-web.listen.port
}";
}];
2024-01-10 04:11:11 +00:00
# This is required when calibre-web is behind a reverse proxy
# https://github.com/janeczku/calibre-web/issues/19
headers.request.add."X-Script-Name" = [ "/calibre-web" ];
2022-10-02 15:24:25 +00:00
}];
}];
2022-10-02 14:48:51 +00:00
# Grant user access to Calibre directories
users.users.${config.user}.extraGroups = [ "calibre-web" ];
2022-10-16 03:47:21 +00:00
# Run a backup on a schedule
2023-03-04 01:04:02 +00:00
systemd.timers.calibre-backup = lib.mkIf config.backups.calibre {
2022-10-16 03:47:21 +00:00
timerConfig = {
OnCalendar = "*-*-* 00:00:00"; # Once per day
Unit = "calibre-backup.service";
};
wantedBy = [ "timers.target" ];
};
# Backup Calibre data to object storage
2023-03-04 01:04:02 +00:00
systemd.services.calibre-backup = lib.mkIf config.backups.calibre {
description = "Backup Calibre data";
environment.AWS_ACCESS_KEY_ID = config.backup.s3.accessKeyId;
serviceConfig = {
Type = "oneshot";
User = "calibre-web";
Group = "backup";
EnvironmentFile = config.secrets.backup.dest;
2022-10-16 03:47:21 +00:00
};
2023-03-04 01:04:02 +00:00
script = ''
${pkgs.awscli2}/bin/aws s3 sync \
${libraryPath}/ \
s3://${config.backup.s3.bucket}/calibre/ \
--endpoint-url=https://${config.backup.s3.endpoint}
'';
};
2022-10-16 03:47:21 +00:00
2022-10-01 21:39:36 +00:00
};
}