1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.calibre-server;
8
9 documentationLink = "https://manual.calibre-ebook.com";
10 generatedDocumentationLink = documentationLink + "/generated/en/calibre-server.html";
11
12 execFlags = (concatStringsSep " "
13 (mapAttrsToList (k: v: "${k} ${toString v}") (filterAttrs (name: value: value != null) {
14 "--listen-on" = cfg.host;
15 "--port" = cfg.port;
16 "--auth-mode" = cfg.auth.mode;
17 "--userdb" = cfg.auth.userDb;
18 }) ++ [(optionalString (cfg.auth.enable == true) "--enable-auth")])
19 );
20in
21
22{
23 imports = [
24 (mkChangedOptionModule [ "services" "calibre-server" "libraryDir" ] [ "services" "calibre-server" "libraries" ]
25 (config:
26 let libraryDir = getAttrFromPath [ "services" "calibre-server" "libraryDir" ] config;
27 in [ libraryDir ]
28 )
29 )
30 ];
31
32 options = {
33 services.calibre-server = {
34
35 enable = mkEnableOption "calibre-server (e-book software)";
36 package = lib.mkPackageOption pkgs "calibre" { };
37
38 libraries = mkOption {
39 type = types.listOf types.path;
40 default = [ "/var/lib/calibre-server" ];
41 description = ''
42 Make sure each library path is initialized before service startup.
43 The directories of the libraries to serve. They must be readable for the user under which the server runs.
44 See the [calibredb documentation](${documentationLink}/generated/en/calibredb.html#add) for details.
45 '';
46 };
47
48 user = mkOption {
49 type = types.str;
50 default = "calibre-server";
51 description = "The user under which calibre-server runs.";
52 };
53
54 group = mkOption {
55 type = types.str;
56 default = "calibre-server";
57 description = "The group under which calibre-server runs.";
58 };
59
60 host = mkOption {
61 type = types.str;
62 default = "0.0.0.0";
63 example = "::1";
64 description = ''
65 The interface on which to listen for connections.
66 See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-listen-on) for details.
67 '';
68 };
69
70 port = mkOption {
71 default = 8080;
72 type = types.port;
73 description = ''
74 The port on which to listen for connections.
75 See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-port) for details.
76 '';
77 };
78
79 auth = {
80 enable = mkOption {
81 type = types.bool;
82 default = false;
83 description = ''
84 Password based authentication to access the server.
85 See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-enable-auth) for details.
86 '';
87 };
88
89 mode = mkOption {
90 type = types.enum [ "auto" "basic" "digest" ];
91 default = "auto";
92 description = ''
93 Choose the type of authentication used.
94 Set the HTTP authentication mode used by the server.
95 See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-auth-mode) for details.
96 '';
97 };
98
99 userDb = mkOption {
100 default = null;
101 type = types.nullOr types.path;
102 description = ''
103 Choose users database file to use for authentication.
104 Make sure users database file is initialized before service startup.
105 See the [calibre-server documentation](${documentationLink}/server.html#managing-user-accounts-from-the-command-line-only) for details.
106 '';
107 };
108 };
109 };
110 };
111
112 config = mkIf cfg.enable {
113
114 systemd.services.calibre-server = {
115 description = "Calibre Server";
116 after = [ "network.target" ];
117 wantedBy = [ "multi-user.target" ];
118 serviceConfig = {
119 User = cfg.user;
120 Restart = "always";
121 ExecStart = "${cfg.package}/bin/calibre-server ${lib.concatStringsSep " " cfg.libraries} ${execFlags}";
122 };
123
124 };
125
126 environment.systemPackages = [ pkgs.calibre ];
127
128 users.users = optionalAttrs (cfg.user == "calibre-server") {
129 calibre-server = {
130 home = "/var/lib/calibre-server";
131 createHome = true;
132 uid = config.ids.uids.calibre-server;
133 group = cfg.group;
134 };
135 };
136
137 users.groups = optionalAttrs (cfg.group == "calibre-server") {
138 calibre-server = {
139 gid = config.ids.gids.calibre-server;
140 };
141 };
142
143 };
144
145 meta.maintainers = with lib.maintainers; [ gaelreyrol ];
146}