summary refs log tree commit diff
path: root/nixos/modules/services/databases/rethinkdb.nix
blob: c764d6c21c6ced62da2f130f54c956535ff6b46a (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.rethinkdb;
  rethinkdb = cfg.package;
in

{

  ###### interface

  options = {

    services.rethinkdb = {

      enable = mkEnableOption "RethinkDB server";

      #package = mkOption {
      #  default = pkgs.rethinkdb;
      #  description = "Which RethinkDB derivation to use.";
      #};

      user = mkOption {
        default = "rethinkdb";
        description = "User account under which RethinkDB runs.";
      };

      group = mkOption {
        default = "rethinkdb";
        description = "Group which rethinkdb user belongs to.";
      };

      dbpath = mkOption {
        default = "/var/db/rethinkdb";
        description = "Location where RethinkDB stores its data, 1 data directory per instance.";
      };

      pidpath = mkOption {
        default = "/run/rethinkdb";
        description = "Location where each instance's pid file is located.";
      };

      #cfgpath = mkOption {
      #  default = "/etc/rethinkdb/instances.d";
      #  description = "Location where RethinkDB stores it config files, 1 config file per instance.";
      #};

      # TODO: currently not used by our implementation.
      #instances = mkOption {
      #  type = types.attrsOf types.str;
      #  default = {};
      #  description = "List of named RethinkDB instances in our cluster.";
      #};

    };

  };

  ###### implementation
  config = mkIf config.services.rethinkdb.enable {

    environment.systemPackages = [ rethinkdb ];

    systemd.services.rethinkdb = {
      description = "RethinkDB server";

      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      serviceConfig = {
        # TODO: abstract away 'default', which is a per-instance directory name
        #       allowing end user of this nix module to provide multiple instances,
        #       and associated directory per instance
        ExecStart = "${rethinkdb}/bin/rethinkdb -d ${cfg.dbpath}/default";
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
        User = cfg.user;
        Group = cfg.group;
        PIDFile = "${cfg.pidpath}/default.pid";
        PermissionsStartOnly = true;
      };

      preStart = ''
        if ! test -e ${cfg.dbpath}; then
            install -d -m0755 -o ${cfg.user} -g ${cfg.group} ${cfg.dbpath}
            install -d -m0755 -o ${cfg.user} -g ${cfg.group} ${cfg.dbpath}/default
            chown -R ${cfg.user}:${cfg.group} ${cfg.dbpath}
        fi
        if ! test -e "${cfg.pidpath}/default.pid"; then
            install -D -o ${cfg.user} -g ${cfg.group} /dev/null "${cfg.pidpath}/default.pid"
        fi
      '';
    };

    users.users.rethinkdb = mkIf (cfg.user == "rethinkdb")
      { name = "rethinkdb";
        description = "RethinkDB server user";
        isSystemUser = true;
      };

    users.groups = optionalAttrs (cfg.group == "rethinkdb") (singleton
      { name = "rethinkdb";
      });

  };

}