Skip to content

Commit caa3c0f

Browse files
committed
services/calibre-server: Add new http & auth options
nixos/doc: add calibre-server new options
1 parent e1b3f7b commit caa3c0f

File tree

4 files changed

+184
-17
lines changed

4 files changed

+184
-17
lines changed

nixos/doc/manual/release-notes/rl-2311.section.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878

7979
- `services.prometheus.exporters` has a new exporter to monitor electrical power consumption based on PowercapRAPL sensor called [Scaphandre](https://github.com/hubblo-org/scaphandre), see [#239803](https://github.com/NixOS/nixpkgs/pull/239803) for more details.
8080

81+
- The module `services.calibre-server` has new options to configure the `host`, `port`, `auth.enable`, `auth.mode` and `auth.userDb` path, see [#216497](https://github.com/NixOS/nixpkgs/pull/216497/) for more details.
82+
8183
## Nixpkgs internals {#sec-release-23.11-nixpkgs-internals}
8284

8385
- The `qemu-vm.nix` module by default now identifies block devices via

nixos/modules/services/misc/calibre-server.nix

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ let
66

77
cfg = config.services.calibre-server;
88

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+
);
920
in
1021

1122
{
@@ -18,52 +29,100 @@ in
1829
)
1930
];
2031

21-
###### interface
22-
2332
options = {
2433
services.calibre-server = {
2534

2635
enable = mkEnableOption (lib.mdDoc "calibre-server");
36+
package = lib.mkPackageOptionMD pkgs "calibre" { };
2737

2838
libraries = mkOption {
39+
type = types.listOf types.path;
40+
default = [ "/var/lib/calibre-server" ];
2941
description = lib.mdDoc ''
42+
Make sure each library path is initialized before service startup.
3043
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.
3145
'';
32-
type = types.listOf types.path;
3346
};
3447

3548
user = mkOption {
36-
description = lib.mdDoc "The user under which calibre-server runs.";
3749
type = types.str;
3850
default = "calibre-server";
51+
description = lib.mdDoc "The user under which calibre-server runs.";
3952
};
4053

4154
group = mkOption {
42-
description = lib.mdDoc "The group under which calibre-server runs.";
4355
type = types.str;
4456
default = "calibre-server";
57+
description = lib.mdDoc "The group under which calibre-server runs.";
4558
};
4659

47-
};
48-
};
60+
host = mkOption {
61+
type = types.str;
62+
default = "0.0.0.0";
63+
example = "::1";
64+
description = lib.mdDoc ''
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 = lib.mdDoc ''
74+
The port on which to listen for connections.
75+
See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-port) for details.
76+
'';
77+
};
4978

79+
auth = {
80+
enable = mkOption {
81+
type = types.bool;
82+
default = false;
83+
description = lib.mdDoc ''
84+
Password based authentication to access the server.
85+
See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-enable-auth) for details.
86+
'';
87+
};
5088

51-
###### implementation
89+
mode = mkOption {
90+
type = types.enum [ "auto" "basic" "digest" ];
91+
default = "auto";
92+
description = lib.mdDoc ''
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 = lib.mdDoc ''
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+
};
52111

53112
config = mkIf cfg.enable {
54113

55114
systemd.services.calibre-server = {
56-
description = "Calibre Server";
57-
after = [ "network.target" ];
58-
wantedBy = [ "multi-user.target" ];
59-
serviceConfig = {
60-
User = cfg.user;
61-
Restart = "always";
62-
ExecStart = "${pkgs.calibre}/bin/calibre-server ${lib.concatStringsSep " " cfg.libraries}";
63-
};
64-
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}";
65122
};
66123

124+
};
125+
67126
environment.systemPackages = [ pkgs.calibre ];
68127

69128
users.users = optionalAttrs (cfg.user == "calibre-server") {
@@ -83,4 +142,5 @@ in
83142

84143
};
85144

145+
meta.maintainers = with lib.maintainers; [ gaelreyrol ];
86146
}

nixos/tests/all-tests.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ in {
151151
cage = handleTest ./cage.nix {};
152152
cagebreak = handleTest ./cagebreak.nix {};
153153
calibre-web = handleTest ./calibre-web.nix {};
154+
calibre-server = handleTest ./calibre-server.nix {};
154155
cassandra_3_0 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_3_0; };
155156
cassandra_3_11 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_3_11; };
156157
cassandra_4 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_4; };

nixos/tests/calibre-server.nix

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{ system ? builtins.currentSystem
2+
, config ? { }
3+
, pkgs ? import ../.. { inherit system config; }
4+
}:
5+
6+
let
7+
inherit (import ../lib/testing-python.nix { inherit system pkgs; }) makeTest;
8+
inherit (pkgs.lib) concatStringsSep maintainers mapAttrs mkMerge
9+
removeSuffix splitString;
10+
11+
tests = {
12+
default = {
13+
calibreConfig = {};
14+
calibreScript = ''
15+
wait_for_unit("calibre-server.service")
16+
'';
17+
};
18+
customLibrary = {
19+
calibreConfig = {
20+
libraries = [ "/var/lib/calibre-data" ];
21+
};
22+
calibreScript = ''
23+
succeed("ls -la /var/lib/calibre-data")
24+
wait_for_unit("calibre-server.service")
25+
'';
26+
};
27+
multipleLibraries = {
28+
calibreConfig = {
29+
libraries = [ "/var/lib/calibre-data" "/var/lib/calibre-server" ];
30+
};
31+
calibreScript = ''
32+
succeed("ls -la /var/lib/calibre-data")
33+
succeed("ls -la /var/lib/calibre-server")
34+
wait_for_unit("calibre-server.service")
35+
'';
36+
};
37+
hostAndPort = {
38+
calibreConfig = {
39+
host = "127.0.0.1";
40+
port = 8888;
41+
};
42+
calibreScript = ''
43+
wait_for_unit("calibre-server.service")
44+
wait_for_open_port(8888)
45+
succeed("curl --fail http://127.0.0.1:8888")
46+
'';
47+
};
48+
basicAuth = {
49+
calibreConfig = {
50+
host = "127.0.0.1";
51+
port = 8888;
52+
auth = {
53+
enable = true;
54+
mode = "basic";
55+
};
56+
};
57+
calibreScript = ''
58+
wait_for_unit("calibre-server.service")
59+
wait_for_open_port(8888)
60+
fail("curl --fail http://127.0.0.1:8888")
61+
'';
62+
};
63+
};
64+
in
65+
mapAttrs
66+
(test: testConfig: (makeTest (
67+
let
68+
nodeName = testConfig.nodeName or test;
69+
calibreConfig = {
70+
enable = true;
71+
libraries = [ "/var/lib/calibre-server" ];
72+
} // testConfig.calibreConfig or {};
73+
librariesInitScript = path: ''
74+
${nodeName}.execute("touch /tmp/test.epub")
75+
${nodeName}.execute("zip -r /tmp/test.zip /tmp/test.epub")
76+
${nodeName}.execute("mkdir -p ${path}")
77+
${nodeName}.execute("calibredb add -d --with-library ${path} /tmp/test.zip")
78+
'';
79+
in
80+
{
81+
name = "calibre-server-${test}";
82+
83+
nodes.${nodeName} = mkMerge [{
84+
environment.systemPackages = [ pkgs.zip ];
85+
services.calibre-server = calibreConfig;
86+
} testConfig.calibreProvider or { }];
87+
88+
testScript = ''
89+
${nodeName}.start()
90+
${concatStringsSep "\n" (map librariesInitScript calibreConfig.libraries)}
91+
${concatStringsSep "\n" (map (line:
92+
if (builtins.substring 0 1 line == " " || builtins.substring 0 1 line == ")")
93+
then line
94+
else "${nodeName}.${line}"
95+
) (splitString "\n" (removeSuffix "\n" testConfig.calibreScript)))}
96+
${nodeName}.shutdown()
97+
'';
98+
99+
meta = with maintainers; {
100+
maintainers = [ gaelreyrol ];
101+
};
102+
}
103+
)))
104+
tests

0 commit comments

Comments
 (0)