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