summary refs log tree commit diff
path: root/nixos/modules/services/misc/calibre-server.nix
blob: 2467d34b524a5befae520a877b4a02c802242cd6 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.calibre-server;

in

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

  ###### interface

  options = {
    services.calibre-server = {

      enable = mkEnableOption "calibre-server";

      libraries = mkOption {
        description = ''
          The directories of the libraries to serve. They must be readable for the user under which the server runs.
        '';
        type = types.listOf types.path;
      };

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

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

    };
  };


  ###### implementation

  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}";
        };

      };

    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;
      };
    };

  };

}