From 9dd2f60e82056f2f1eabd1f45f03f0b04bb4dffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Reyrol?= Date: Mon, 26 Jun 2023 19:53:34 +0200 Subject: [PATCH] nixos/doc: add calibre-server new options --- .../manual/release-notes/rl-2311.section.md | 2 + .../modules/services/misc/calibre-server.nix | 52 +++++--- nixos/tests/calibre-server.nix | 126 ++++++++++++++---- 3 files changed, 133 insertions(+), 47 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2311.section.md b/nixos/doc/manual/release-notes/rl-2311.section.md index 66bed591d48e5c3..2823e262fa8c114 100644 --- a/nixos/doc/manual/release-notes/rl-2311.section.md +++ b/nixos/doc/manual/release-notes/rl-2311.section.md @@ -76,6 +76,8 @@ - `services.nginx` gained a `defaultListen` option at server-level with support for PROXY protocol listeners, also `proxyProtocol` is now exposed in `services.nginx.virtualHosts..listen` option. It is now possible to run PROXY listeners and non-PROXY listeners at a server-level, see [#213510](https://github.com/NixOS/nixpkgs/pull/213510/) for more details. +- 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. + ## Nixpkgs internals {#sec-release-23.11-nixpkgs-internals} - The `qemu-vm.nix` module by default now identifies block devices via diff --git a/nixos/modules/services/misc/calibre-server.nix b/nixos/modules/services/misc/calibre-server.nix index 0444cea9af2bc2e..1af40577b527316 100644 --- a/nixos/modules/services/misc/calibre-server.nix +++ b/nixos/modules/services/misc/calibre-server.nix @@ -6,18 +6,17 @@ 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) - ]) - ); + documentationLink = "https://manual.calibre-ebook.com"; + generatedDocumentationLink = documentationLink + "/generated/en/calibre-server.html"; + + execFlags = (concatStringsSep " " + (mapAttrsToList (k: v: "${k} ${toString v}") (filterAttrs (name: value: value != null) { + "--listen-on" = cfg.host; + "--port" = cfg.port; + "--auth-mode" = cfg.auth.mode; + "--userdb" = cfg.auth.userDb; + }) ++ [(optionalString (cfg.auth.enable == true) "--enable-auth")]) + ); in { @@ -38,22 +37,25 @@ in enable = mkEnableOption (lib.mdDoc "calibre-server"); libraries = mkOption { + type = types.listOf types.path; + default = [ "/var/lib/calibre-server" ]; description = lib.mdDoc '' + Make sure each library path is initialized before service startup. The directories of the libraries to serve. They must be readable for the user under which the server runs. + See the [calibredb documentation](${documentationLink}/generated/en/calibredb.html#add) for details. ''; - type = types.listOf types.path; }; user = mkOption { - description = lib.mdDoc "The user under which calibre-server runs."; type = types.str; default = "calibre-server"; + description = lib.mdDoc "The user under which calibre-server runs."; }; group = mkOption { - description = lib.mdDoc "The group under which calibre-server runs."; type = types.str; default = "calibre-server"; + description = lib.mdDoc "The group under which calibre-server runs."; }; host = mkOption { @@ -62,7 +64,7 @@ in 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. + See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-listen-on) for details. ''; }; @@ -71,7 +73,7 @@ in 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. + See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-port) for details. ''; }; @@ -81,7 +83,7 @@ in 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. + See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-enable-auth) for details. ''; }; @@ -91,7 +93,17 @@ in 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. + See the [calibre-server documentation](${generatedDocumentationLink}#cmdoption-calibre-server-auth-mode) for details. + ''; + }; + + userDb = mkOption { + default = null; + type = types.nullOr types.path; + description = lib.mdDoc '' + Choose users database file to use for authentication. + Make sure users database file is initialized before service startup. + See the [calibre-server documentation](${documentationLink}/server.html#managing-user-accounts-from-the-command-line-only) for details. ''; }; }; @@ -131,5 +143,5 @@ in }; - meta.maintainers = with lib.maintainers; [ ]; + meta.maintainers = with lib.maintainers; [ gaelreyrol ]; } diff --git a/nixos/tests/calibre-server.nix b/nixos/tests/calibre-server.nix index 5768a99853d05e6..4b1753aaa7049f4 100644 --- a/nixos/tests/calibre-server.nix +++ b/nixos/tests/calibre-server.nix @@ -1,32 +1,104 @@ -import ./make-test-python.nix ( - { pkgs, lib, ... }: +{ system ? builtins.currentSystem +, config ? { } +, pkgs ? import ../.. { inherit system config; } +}: - with lib; +let + inherit (import ../lib/testing-python.nix { inherit system pkgs; }) makeTest; + inherit (pkgs.lib) concatStringsSep maintainers mapAttrs mkMerge + removeSuffix splitString; - 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 ]; + tests = { + default = { + calibreConfig = {}; + calibreScript = '' + wait_for_unit("calibre-server.service") + ''; + }; + customLibrary = { + calibreConfig = { + libraries = [ "/var/lib/calibre-data" ]; + }; + calibreScript = '' + succeed("ls -la /var/lib/calibre-data") + wait_for_unit("calibre-server.service") + ''; + }; + multipleLibraries = { + calibreConfig = { + libraries = [ "/var/lib/calibre-data" "/var/lib/calibre-server" ]; + }; + calibreScript = '' + succeed("ls -la /var/lib/calibre-data") + succeed("ls -la /var/lib/calibre-server") + wait_for_unit("calibre-server.service") + ''; + }; + hostAndPort = { + calibreConfig = { + host = "127.0.0.1"; + port = 8888; }; + calibreScript = '' + wait_for_unit("calibre-server.service") + wait_for_open_port(8888) + succeed("curl --fail http://127.0.0.1:8888") + ''; }; + basicAuth = { + calibreConfig = { + host = "127.0.0.1"; + port = 8888; + auth = { + enable = true; + mode = "basic"; + }; + }; + calibreScript = '' + wait_for_unit("calibre-server.service") + wait_for_open_port(8888) + fail("curl --fail http://127.0.0.1:8888") + ''; + }; + }; +in +mapAttrs + (test: testConfig: (makeTest ( + let + nodeName = testConfig.nodeName or test; + calibreConfig = { + enable = true; + libraries = [ "/var/lib/calibre-server" ]; + } // testConfig.calibreConfig or {}; + librariesInitScript = path: '' + ${nodeName}.execute("touch /tmp/test.epub") + ${nodeName}.execute("zip -r /tmp/test.zip /tmp/test.epub") + ${nodeName}.execute("mkdir -p ${path}") + ${nodeName}.execute("calibredb add -d --with-library ${path} /tmp/test.zip") + ''; + in + { + name = "calibre-server-${test}"; - 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}") - ''; - } -) + nodes.${nodeName} = mkMerge [{ + environment.systemPackages = [ pkgs.zip ]; + services.calibre-server = calibreConfig; + } testConfig.calibreProvider or { }]; + + testScript = '' + ${nodeName}.start() + ${concatStringsSep "\n" (map librariesInitScript calibreConfig.libraries)} + ${concatStringsSep "\n" (map (line: + if (builtins.substring 0 1 line == " " || builtins.substring 0 1 line == ")") + then line + else "${nodeName}.${line}" + ) (splitString "\n" (removeSuffix "\n" testConfig.calibreScript)))} + ${nodeName}.shutdown() + ''; + + meta = with maintainers; { + maintainers = [ gaelreyrol ]; + }; + } + ))) + tests