about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/misc/calibre-server.nix
blob: 8e2ce2909239b595a00713676cd35a31c6686b66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.calibre-server;

  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

{
  imports = [
    (mkChangedOptionModule [ "services" "calibre-server" "libraryDir" ] [ "services" "calibre-server" "libraries" ]
      (config:
        let libraryDir = getAttrFromPath [ "services" "calibre-server" "libraryDir" ] config;
        in [ libraryDir ]
      )
    )
  ];

  options = {
    services.calibre-server = {

      enable = mkEnableOption "calibre-server (e-book software)";
      package = lib.mkPackageOption pkgs "calibre" { };

      libraries = mkOption {
        type = types.listOf types.path;
        default = [ "/var/lib/calibre-server" ];
        description = ''
          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.
        '';
      };

      user = mkOption {
        type = types.str;
        default = "calibre-server";
        description = "The user under which calibre-server runs.";
      };

      group = mkOption {
        type = types.str;
        default = "calibre-server";
        description = "The group under which calibre-server runs.";
      };

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

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

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

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

        userDb = mkOption {
          default = null;
          type = types.nullOr types.path;
          description = ''
            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.
          '';
        };
      };
    };
  };

  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 = "${cfg.package}/bin/calibre-server ${lib.concatStringsSep " " cfg.libraries} ${execFlags}";
      };

    };

    environment.systemPackages = [ pkgs.calibre ];

    users.users = optionalAttrs (cfg.user == "calibre-server") {
      calibre-server = {
        home = "/var/lib/calibre-server";
        createHome = true;
        uid = config.ids.uids.calibre-server;
        group = cfg.group;
      };
    };

    users.groups = optionalAttrs (cfg.group == "calibre-server") {
      calibre-server = {
        gid = config.ids.gids.calibre-server;
      };
    };

  };

  meta.maintainers = with lib.maintainers; [ gaelreyrol ];
}