1 { config, lib, pkgs, ... }:
4 cfg = config.services.calibre-web;
6 inherit (lib) concatStringsSep mkEnableOption mkIf mkOption optional optionalString types;
10 services.calibre-web = {
11 enable = mkEnableOption "Calibre-Web";
13 package = lib.mkPackageOption pkgs "calibre-web" { };
20 IP address that Calibre-Web should listen on.
28 Listen port for Calibre-Web.
35 default = "calibre-web";
37 The directory below {file}`/var/lib` where Calibre-Web stores its data.
43 default = "calibre-web";
44 description = "User account under which Calibre-Web runs.";
49 default = "calibre-web";
50 description = "Group account under which Calibre-Web runs.";
53 openFirewall = mkOption {
57 Open ports in the firewall for the server.
62 calibreLibrary = mkOption {
63 type = types.nullOr types.path;
66 Path to Calibre library.
70 enableBookConversion = mkOption {
74 Configure path to the Calibre's ebook-convert in the DB.
78 enableKepubify = mkEnableOption "kebup conversion support";
80 enableBookUploading = mkOption {
84 Allow books to be uploaded via Calibre-Web UI.
93 Enable authorization using auth proxy.
101 Auth proxy header name.
109 config = mkIf cfg.enable {
110 systemd.services.calibre-web = let
111 appDb = "/var/lib/${cfg.dataDir}/app.db";
112 gdriveDb = "/var/lib/${cfg.dataDir}/gdrive.db";
113 calibreWebCmd = "${cfg.package}/bin/calibre-web -p ${appDb} -g ${gdriveDb}";
115 settings = concatStringsSep ", " (
117 "config_port = ${toString cfg.listen.port}"
118 "config_uploading = ${if cfg.options.enableBookUploading then "1" else "0"}"
119 "config_allow_reverse_proxy_header_login = ${if cfg.options.reverseProxyAuth.enable then "1" else "0"}"
120 "config_reverse_proxy_login_header_name = '${cfg.options.reverseProxyAuth.header}'"
122 ++ optional (cfg.options.calibreLibrary != null) "config_calibre_dir = '${cfg.options.calibreLibrary}'"
123 ++ optional cfg.options.enableBookConversion "config_converterpath = '${pkgs.calibre}/bin/ebook-convert'"
124 ++ optional cfg.options.enableKepubify "config_kepubifypath = '${pkgs.kepubify}/bin/kepubify'"
128 description = "Web app for browsing, reading and downloading eBooks stored in a Calibre database";
129 after = [ "network.target" ];
130 wantedBy = [ "multi-user.target" ];
137 StateDirectory = cfg.dataDir;
138 ExecStartPre = pkgs.writeShellScript "calibre-web-pre-start" (
140 __RUN_MIGRATIONS_AND_EXIT=1 ${calibreWebCmd}
142 ${pkgs.sqlite}/bin/sqlite3 ${appDb} "update settings set ${settings}"
143 '' + optionalString (cfg.options.calibreLibrary != null) ''
144 test -f "${cfg.options.calibreLibrary}/metadata.db" || { echo "Invalid Calibre library"; exit 1; }
148 ExecStart = "${calibreWebCmd} -i ${cfg.listen.ip}";
149 Restart = "on-failure";
153 networking.firewall = mkIf cfg.openFirewall {
154 allowedTCPPorts = [ cfg.listen.port ];
157 users.users = mkIf (cfg.user == "calibre-web") {
164 users.groups = mkIf (cfg.group == "calibre-web") {
169 meta.maintainers = with lib.maintainers; [ pborzenkov ];