git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #113994 from pborzenkov/calibre-web

calibre-web: init at 0.6.11

commitf19434ad4a1c4eca0cf9b3b26b11bc0c8e4f0e09 authorGuillaume Girol <symphorien@users.noreply.github.com> dateWed 31 Mar 2021, 18:25 (6 years ago) parentf074dba58ce51e
8 files changed+325
nixos/modules/module-list.nix+1 nixos/modules/services/web-apps/calibre-web.nix+165 nixos/tests/all-tests.nix+1 nixos/tests/calibre-web.nix+53 pkgs/servers/calibre-web/db-migrations.patch+14 pkgs/servers/calibre-web/default-logger.patch+17 pkgs/servers/calibre-web/default.nix+72 pkgs/top-level/all-packages.nix+2
nixos/modules/module-list.nix+1
@@ -889,6 +889,7 @@
889889 ./services/web-apps/atlassian/crowd.nix
890890 ./services/web-apps/atlassian/jira.nix
891891 ./services/web-apps/bookstack.nix
892+ ./services/web-apps/calibre-web.nix
892893 ./services/web-apps/convos.nix
893894 ./services/web-apps/cryptpad.nix
894895 ./services/web-apps/documize.nix
nixos/modules/services/web-apps/calibre-web.nixnew file+165
@@ -0,0 +1,165 @@
1+{ config, lib, pkgs, ... }:
2+
3+let
4+ cfg = config.services.calibre-web;
5+
6+ inherit (lib) concatStringsSep mkEnableOption mkIf mkOption optional optionalString types;
7+in
8+{
9+ options = {
10+ services.calibre-web = {
11+ enable = mkEnableOption "Calibre-Web";
12+
13+ listen = {
14+ ip = mkOption {
15+ type = types.str;
16+ default = "::1";
17+ description = ''
18+ IP address that Calibre-Web should listen on.
19+ '';
20+ };
21+
22+ port = mkOption {
23+ type = types.port;
24+ default = 8083;
25+ description = ''
26+ Listen port for Calibre-Web.
27+ '';
28+ };
29+ };
30+
31+ dataDir = mkOption {
32+ type = types.str;
33+ default = "calibre-web";
34+ description = ''
35+ The directory below <filename>/var/lib</filename> where Calibre-Web stores its data.
36+ '';
37+ };
38+
39+ user = mkOption {
40+ type = types.str;
41+ default = "calibre-web";
42+ description = "User account under which Calibre-Web runs.";
43+ };
44+
45+ group = mkOption {
46+ type = types.str;
47+ default = "calibre-web";
48+ description = "Group account under which Calibre-Web runs.";
49+ };
50+
51+ openFirewall = mkOption {
52+ type = types.bool;
53+ default = false;
54+ description = ''
55+ Open ports in the firewall for the server.
56+ '';
57+ };
58+
59+ options = {
60+ calibreLibrary = mkOption {
61+ type = types.nullOr types.path;
62+ default = null;
63+ description = ''
64+ Path to Calibre library.
65+ '';
66+ };
67+
68+ enableBookConversion = mkOption {
69+ type = types.bool;
70+ default = false;
71+ description = ''
72+ Configure path to the Calibre's ebook-convert in the DB.
73+ '';
74+ };
75+
76+ enableBookUploading = mkOption {
77+ type = types.bool;
78+ default = false;
79+ description = ''
80+ Allow books to be uploaded via Calibre-Web UI.
81+ '';
82+ };
83+
84+ reverseProxyAuth = {
85+ enable = mkOption {
86+ type = types.bool;
87+ default = false;
88+ description = ''
89+ Enable authorization using auth proxy.
90+ '';
91+ };
92+
93+ header = mkOption {
94+ type = types.str;
95+ default = "";
96+ description = ''
97+ Auth proxy header name.
98+ '';
99+ };
100+ };
101+ };
102+ };
103+ };
104+
105+ config = mkIf cfg.enable {
106+ systemd.services.calibre-web = let
107+ appDb = "/var/lib/${cfg.dataDir}/app.db";
108+ gdriveDb = "/var/lib/${cfg.dataDir}/gdrive.db";
109+ calibreWebCmd = "${pkgs.calibre-web}/bin/calibre-web -p ${appDb} -g ${gdriveDb}";
110+
111+ settings = concatStringsSep ", " (
112+ [
113+ "config_port = ${toString cfg.listen.port}"
114+ "config_uploading = ${if cfg.options.enableBookUploading then "1" else "0"}"
115+ "config_allow_reverse_proxy_header_login = ${if cfg.options.reverseProxyAuth.enable then "1" else "0"}"
116+ "config_reverse_proxy_login_header_name = '${cfg.options.reverseProxyAuth.header}'"
117+ ]
118+ ++ optional (cfg.options.calibreLibrary != null) "config_calibre_dir = '${cfg.options.calibreLibrary}'"
119+ ++ optional cfg.options.enableBookConversion "config_converterpath = '${pkgs.calibre}/bin/ebook-convert'"
120+ );
121+ in
122+ {
123+ description = "Web app for browsing, reading and downloading eBooks stored in a Calibre database";
124+ after = [ "network.target" ];
125+ wantedBy = [ "multi-user.target" ];
126+
127+ serviceConfig = {
128+ Type = "simple";
129+ User = cfg.user;
130+ Group = cfg.group;
131+
132+ StateDirectory = cfg.dataDir;
133+ ExecStartPre = pkgs.writeShellScript "calibre-web-pre-start" (
134+ ''
135+ __RUN_MIGRATIONS_AND_EXIT=1 ${calibreWebCmd}
136+
137+ ${pkgs.sqlite}/bin/sqlite3 ${appDb} "update settings set ${settings}"
138+ '' + optionalString (cfg.options.calibreLibrary != null) ''
139+ test -f ${cfg.options.calibreLibrary}/metadata.db || { echo "Invalid Calibre library"; exit 1; }
140+ ''
141+ );
142+
143+ ExecStart = "${calibreWebCmd} -i ${cfg.listen.ip}";
144+ Restart = "on-failure";
145+ };
146+ };
147+
148+ networking.firewall = mkIf cfg.openFirewall {
149+ allowedTCPPorts = [ cfg.listen.port ];
150+ };
151+
152+ users.users = mkIf (cfg.user == "calibre-web") {
153+ calibre-web = {
154+ isSystemUser = true;
155+ group = cfg.group;
156+ };
157+ };
158+
159+ users.groups = mkIf (cfg.group == "calibre-web") {
160+ calibre-web = {};
161+ };
162+ };
163+
164+ meta.maintainers = with lib.maintainers; [ pborzenkov ];
165+}
nixos/tests/all-tests.nix+1
@@ -49,6 +49,7 @@ in
4949 cadvisor = handleTestOn ["x86_64-linux"] ./cadvisor.nix {};
5050 cage = handleTest ./cage.nix {};
5151 cagebreak = handleTest ./cagebreak.nix {};
52+ calibre-web = handleTest ./calibre-web.nix {};
5253 cassandra_2_1 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_2_1; };
5354 cassandra_2_2 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_2_2; };
5455 cassandra_3_0 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_3_0; };
nixos/tests/calibre-web.nixnew file+53
@@ -0,0 +1,53 @@
1+import ./make-test-python.nix (
2+ { pkgs, lib, ... }:
3+
4+ let
5+ port = 3142;
6+ defaultPort = 8083;
7+ in
8+ with lib;
9+ {
10+ name = "calibre-web";
11+ meta.maintainers = with pkgs.lib.maintainers; [ pborzenkov ];
12+
13+ nodes = {
14+ default = { ... }: {
15+ services.calibre-web.enable = true;
16+ };
17+
18+ customized = { pkgs, ... }: {
19+ services.calibre-web = {
20+ enable = true;
21+ listen.port = port;
22+ options = {
23+ calibreLibrary = "/tmp/books";
24+ reverseProxyAuth = {
25+ enable = true;
26+ header = "X-User";
27+ };
28+ };
29+ };
30+ environment.systemPackages = [ pkgs.calibre ];
31+ };
32+ };
33+ testScript = ''
34+ start_all()
35+
36+ default.wait_for_unit("calibre-web.service")
37+ default.wait_for_open_port(${toString defaultPort})
38+ default.succeed(
39+ "curl --fail 'http://localhost:${toString defaultPort}/basicconfig' | grep -q 'Basic Configuration'"
40+ )
41+
42+ customized.succeed(
43+ "mkdir /tmp/books && calibredb --library-path /tmp/books add -e --title test-book"
44+ )
45+ customized.succeed("systemctl restart calibre-web")
46+ customized.wait_for_unit("calibre-web.service")
47+ customized.wait_for_open_port(${toString port})
48+ customized.succeed(
49+ "curl --fail -H X-User:admin 'http://localhost:${toString port}' | grep -q test-book"
50+ )
51+ '';
52+ }
53+)
pkgs/servers/calibre-web/db-migrations.patchnew file+14
@@ -0,0 +1,14 @@
1+diff --git a/cps/__init__.py b/cps/__init__.py
2+index 627cca0b..233bb2dd 100644
3+--- a/cps/__init__.py
4++++ b/cps/__init__.py
5+@@ -87,6 +87,9 @@ db.CalibreDB.setup_db(config, cli.settingspath)
6+
7+ calibre_db = db.CalibreDB()
8+
9++if os.environ.get('__RUN_MIGRATIONS_AND_EXIT'):
10++ sys.exit(0)
11++
12+ def create_app():
13+ app.wsgi_app = ReverseProxied(app.wsgi_app)
14+ # For python2 convert path to unicode
pkgs/servers/calibre-web/default-logger.patchnew file+17
@@ -0,0 +1,17 @@
1+diff --git a/cps/logger.py b/cps/logger.py
2+index b204de31..3206e2bf 100644
3+--- a/cps/logger.py
4++++ b/cps/logger.py
5+@@ -32,10 +32,10 @@ ACCESS_FORMATTER_TORNADO = Formatter("[%(asctime)s] %(message)s")
6+
7+ FORMATTER = Formatter("[%(asctime)s] %(levelname)5s {%(name)s:%(lineno)d} %(message)s")
8+ DEFAULT_LOG_LEVEL = logging.INFO
9+-DEFAULT_LOG_FILE = os.path.join(_CONFIG_DIR, "calibre-web.log")
10+-DEFAULT_ACCESS_LOG = os.path.join(_CONFIG_DIR, "access.log")
11+ LOG_TO_STDERR = '/dev/stderr'
12+ LOG_TO_STDOUT = '/dev/stdout'
13++DEFAULT_LOG_FILE = LOG_TO_STDOUT
14++DEFAULT_ACCESS_LOG = LOG_TO_STDOUT
15+
16+ logging.addLevelName(logging.WARNING, "WARN")
17+ logging.addLevelName(logging.CRITICAL, "CRIT")
pkgs/servers/calibre-web/default.nixnew file+72
@@ -0,0 +1,72 @@
1+{ lib
2+, fetchFromGitHub
3+, nixosTests
4+, python3
5+, python3Packages
6+}:
7+
8+python3.pkgs.buildPythonApplication rec {
9+ pname = "calibre-web";
10+ version = "0.6.11";
11+
12+ src = fetchFromGitHub {
13+ owner = "janeczku";
14+ repo = "calibre-web";
15+ rev = version;
16+ sha256 = "10sjllhhcamswpa1wlim4mbm2zl4g804bwly5p4nmklg7n1v226g";
17+ };
18+
19+ prePatch = ''
20+ substituteInPlace setup.cfg \
21+ --replace "requests>=2.11.1,<2.25.0" "requests>=2.11.1,<2.26.0" \
22+ --replace "cps = calibreweb:main" "calibre-web = calibreweb:main"
23+ '';
24+
25+ patches = [
26+ # default-logger.patch switches default logger to /dev/stdout. Otherwise calibre-web tries to open a file relative
27+ # to its location, which can't be done as the store is read-only. Log file location can later be configured using UI
28+ # if needed.
29+ ./default-logger.patch
30+ # DB migrations adds an env var __RUN_MIGRATIONS_ANDEXIT that, when set, instructs calibre-web to run DB migrations
31+ # and exit. This is gonna be used to configure calibre-web declaratively, as most of its configuration parameters
32+ # are stored in the DB.
33+ ./db-migrations.patch
34+ ];
35+
36+ # calibre-web doesn't follow setuptools directory structure. The following is taken from the script
37+ # that calibre-web's maintainer is using to package it:
38+ # https://github.com/OzzieIsaacs/calibre-web-test/blob/master/build/make_release.py
39+ postPatch = ''
40+ mkdir -p src/calibreweb
41+ mv cps.py src/calibreweb/__init__.py
42+ mv cps src/calibreweb
43+ '';
44+
45+ # Upstream repo doesn't provide any tests.
46+ doCheck = false;
47+
48+ propagatedBuildInputs = with python3Packages; [
49+ backports_abc
50+ flask-babel
51+ flask_login
52+ flask_principal
53+ iso-639
54+ pypdf2
55+ requests
56+ singledispatch
57+ sqlalchemy
58+ tornado
59+ unidecode
60+ Wand
61+ ];
62+
63+ passthru.tests.calibre-web = nixosTests.calibre-web;
64+
65+ meta = with lib; {
66+ description = "Web app for browsing, reading and downloading eBooks stored in a Calibre database";
67+ maintainers = with maintainers; [ pborzenkov ];
68+ homepage = "https://github.com/janeczku/calibre-web";
69+ license = licenses.gpl3Plus;
70+ platforms = platforms.all;
71+ };
72+}
pkgs/top-level/all-packages.nix+2
@@ -21920,6 +21920,8 @@ in
2192021920
2192121921 calibre = libsForQt5.callPackage ../applications/misc/calibre { };
2192221922
21923+ calibre-web = callPackage ../servers/calibre-web { };
21924+
2192321925 calligra = libsForQt5.callPackage ../applications/office/calligra {
2192421926 # Must use the same Qt version as Calligra itself:
2192521927 poppler = libsForQt5.poppler_0_61;