Skip to content

Commit

Permalink
(nixos/calibre-server): Add new http & auth options
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelreyrol committed Feb 15, 2023
1 parent fa90359 commit 0093c84
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
73 changes: 61 additions & 12 deletions nixos/modules/services/misc/calibre-server.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ let

cfg = config.services.calibre-server;

documentationLink = "https://manual.calibre-ebook.com/generated/en/calibre-server.html";

execFlags = concatStringsSep " "
(filter (match: match != null)
((mapAttrsToList (k: v: "${k} ${toString v}") {
"--listen-on" = cfg.host;
"--port" = cfg.port;
"--auth-mode" = cfg.auth.mode;
}) ++ [
(if cfg.auth.enable == true then "--enable-auth" else null)
])
);
in

{
Expand Down Expand Up @@ -44,26 +56,62 @@ in
default = "calibre-server";
};

};
};
host = mkOption {
type = types.str;
default = "0.0.0.0";
example = "::1";
description = lib.mdDoc ''
The interface on which to listen for connections.
See the [calibre-server documentation](${documentationLink}#cmdoption-calibre-server-listen-on) for details.
'';
};

port = mkOption {
default = 8080;
type = types.port;
description = lib.mdDoc ''
The port on which to listen for connections.
See the [calibre-server documentation](${documentationLink}#cmdoption-calibre-server-port) for details.
'';
};

###### implementation
auth = {
enable = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
Password based authentication to access the server.
See the [calibre-server documentation](${documentationLink}#cmdoption-calibre-server-enable-auth) for details.
'';
};

mode = mkOption {
type = types.enum [ "auto" "basic" "digest" ];
default = "auto";
description = lib.mdDoc ''
Choose the type of authentication used.
Set the HTTP authentication mode used by the server.
See the [calibre-server documentation](${documentationLink}#cmdoption-calibre-server-auth-mode) for details.
'';
};
};
};
};

config = mkIf cfg.enable {

systemd.services.calibre-server = {
description = "Calibre Server";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Restart = "always";
ExecStart = "${pkgs.calibre}/bin/calibre-server ${lib.concatStringsSep " " cfg.libraries}";
};

description = "Calibre Server";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Restart = "always";
ExecStart = "${pkgs.calibre}/bin/calibre-server ${lib.concatStringsSep " " cfg.libraries} ${execFlags}";
};

};

environment.systemPackages = [ pkgs.calibre ];

users.users = optionalAttrs (cfg.user == "calibre-server") {
Expand All @@ -83,4 +131,5 @@ in

};

meta.maintainers = with lib.maintainers; [ ];
}
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ in {
cage = handleTest ./cage.nix {};
cagebreak = handleTest ./cagebreak.nix {};
calibre-web = handleTest ./calibre-web.nix {};
calibre-server = handleTest ./calibre-server.nix {};
cassandra_3_0 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_3_0; };
cassandra_3_11 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_3_11; };
cassandra_4 = handleTest ./cassandra.nix { testPackage = pkgs.cassandra_4; };
Expand Down
32 changes: 32 additions & 0 deletions nixos/tests/calibre-server.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ./make-test-python.nix (
{ pkgs, lib, ... }:

with lib;

let
host = "127.0.0.1";
port = 8888;
libraryPath = "/var/lib/calibre-data";
in
{
name = "calibre-server";

nodes.machine = { config, pkgs, ... }: {
services.calibre-server = {
enable = true;
inherit host port;
libraries = [ libraryPath ];
};
};

testScript = ''
machine.start()
machine.execute("mkdir -p ${libraryPath}")
machine.execute("curl -o alice.epub https://www.gutenberg.org/ebooks/11.epub.noimages")
machine.execute("calibredb add /tmp/alice.epub --with-library ${libraryPath}")
machine.wait_for_unit("calibre-server.service")
machine.wait_for_open_port(${toString port})
machine.succeed("curl --fail http://${host}:${toString port}")
'';
}
)

0 comments on commit 0093c84

Please sign in to comment.