1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.rethinkdb;
7 rethinkdb = cfg.package;
8in
9
10{
11
12 ###### interface
13
14 options = {
15
16 services.rethinkdb = {
17
18 enable = mkEnableOption (lib.mdDoc "RethinkDB server");
19
20 #package = mkOption {
21 # default = pkgs.rethinkdb;
22 # description = "Which RethinkDB derivation to use.";
23 #};
24
25 user = mkOption {
26 default = "rethinkdb";
27 description = lib.mdDoc "User account under which RethinkDB runs.";
28 };
29
30 group = mkOption {
31 default = "rethinkdb";
32 description = lib.mdDoc "Group which rethinkdb user belongs to.";
33 };
34
35 dbpath = mkOption {
36 default = "/var/db/rethinkdb";
37 description = lib.mdDoc "Location where RethinkDB stores its data, 1 data directory per instance.";
38 };
39
40 pidpath = mkOption {
41 default = "/run/rethinkdb";
42 description = lib.mdDoc "Location where each instance's pid file is located.";
43 };
44
45 #cfgpath = mkOption {
46 # default = "/etc/rethinkdb/instances.d";
47 # description = "Location where RethinkDB stores it config files, 1 config file per instance.";
48 #};
49
50 # TODO: currently not used by our implementation.
51 #instances = mkOption {
52 # type = types.attrsOf types.str;
53 # default = {};
54 # description = "List of named RethinkDB instances in our cluster.";
55 #};
56
57 };
58
59 };
60
61 ###### implementation
62 config = mkIf config.services.rethinkdb.enable {
63
64 environment.systemPackages = [ rethinkdb ];
65
66 systemd.services.rethinkdb = {
67 description = "RethinkDB server";
68
69 wantedBy = [ "multi-user.target" ];
70 after = [ "network.target" ];
71
72 serviceConfig = {
73 # TODO: abstract away 'default', which is a per-instance directory name
74 # allowing end user of this nix module to provide multiple instances,
75 # and associated directory per instance
76 ExecStart = "${rethinkdb}/bin/rethinkdb -d ${cfg.dbpath}/default";
77 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
78 User = cfg.user;
79 Group = cfg.group;
80 PIDFile = "${cfg.pidpath}/default.pid";
81 PermissionsStartOnly = true;
82 };
83
84 preStart = ''
85 if ! test -e ${cfg.dbpath}; then
86 install -d -m0755 -o ${cfg.user} -g ${cfg.group} ${cfg.dbpath}
87 install -d -m0755 -o ${cfg.user} -g ${cfg.group} ${cfg.dbpath}/default
88 chown -R ${cfg.user}:${cfg.group} ${cfg.dbpath}
89 fi
90 if ! test -e "${cfg.pidpath}/default.pid"; then
91 install -D -o ${cfg.user} -g ${cfg.group} /dev/null "${cfg.pidpath}/default.pid"
92 fi
93 '';
94 };
95
96 users.users.rethinkdb = mkIf (cfg.user == "rethinkdb")
97 { name = "rethinkdb";
98 description = "RethinkDB server user";
99 isSystemUser = true;
100 };
101
102 users.groups = optionalAttrs (cfg.group == "rethinkdb") (singleton
103 { name = "rethinkdb";
104 });
105
106 };
107
108}