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