1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.riak;
8
9in
10
11{
12
13 ###### interface
14
15 options = {
16
17 services.riak = {
18
19 enable = mkEnableOption "riak";
20
21 package = mkOption {
22 type = types.package;
23 default = pkgs.riak;
24 defaultText = "pkgs.riak";
25 example = literalExample "pkgs.riak";
26 description = ''
27 Riak package to use.
28 '';
29 };
30
31 nodeName = mkOption {
32 type = types.string;
33 default = "riak@127.0.0.1";
34 description = ''
35 Name of the Erlang node.
36 '';
37 };
38
39 distributedCookie = mkOption {
40 type = types.string;
41 default = "riak";
42 description = ''
43 Cookie for distributed node communication. All nodes in the
44 same cluster should use the same cookie or they will not be able to
45 communicate.
46 '';
47 };
48
49 dataDir = mkOption {
50 type = types.path;
51 default = "/var/db/riak";
52 description = ''
53 Data directory for Riak.
54 '';
55 };
56
57 logDir = mkOption {
58 type = types.path;
59 default = "/var/log/riak";
60 description = ''
61 Log directory for Riak.
62 '';
63 };
64
65 extraConfig = mkOption {
66 type = types.lines;
67 default = "";
68 description = ''
69 Additional text to be appended to <filename>riak.conf</filename>.
70 '';
71 };
72
73 extraAdvancedConfig = mkOption {
74 type = types.lines;
75 default = "";
76 description = ''
77 Additional text to be appended to <filename>advanced.config</filename>.
78 '';
79 };
80
81 };
82
83 };
84
85 ###### implementation
86
87 config = mkIf cfg.enable {
88
89 environment.systemPackages = [ cfg.package ];
90 environment.etc."riak/riak.conf".text = ''
91 nodename = ${cfg.nodeName}
92 distributed_cookie = ${cfg.distributedCookie}
93
94 platform_log_dir = ${cfg.logDir}
95 platform_etc_dir = /etc/riak
96 platform_data_dir = ${cfg.dataDir}
97
98 ${cfg.extraConfig}
99 '';
100
101 environment.etc."riak/advanced.config".text = ''
102 ${cfg.extraAdvancedConfig}
103 '';
104
105 users.extraUsers.riak = {
106 name = "riak";
107 uid = config.ids.uids.riak;
108 group = "riak";
109 description = "Riak server user";
110 };
111
112 users.extraGroups.riak.gid = config.ids.gids.riak;
113
114 systemd.services.riak = {
115 description = "Riak Server";
116
117 wantedBy = [ "multi-user.target" ];
118 after = [ "network.target" ];
119
120 path = [
121 pkgs.utillinux # for `logger`
122 pkgs.bash
123 ];
124
125 environment.HOME = "${cfg.dataDir}";
126 environment.RIAK_DATA_DIR = "${cfg.dataDir}";
127 environment.RIAK_LOG_DIR = "${cfg.logDir}";
128 environment.RIAK_ETC_DIR = "/etc/riak";
129
130 preStart = ''
131 if ! test -e ${cfg.logDir}; then
132 mkdir -m 0755 -p ${cfg.logDir}
133 chown -R riak ${cfg.logDir}
134 fi
135
136 if ! test -e ${cfg.dataDir}; then
137 mkdir -m 0700 -p ${cfg.dataDir}
138 chown -R riak ${cfg.dataDir}
139 fi
140 '';
141
142 serviceConfig = {
143 ExecStart = "${cfg.package}/bin/riak console";
144 ExecStop = "${cfg.package}/bin/riak stop";
145 StandardInput = "tty";
146 User = "riak";
147 Group = "riak";
148 PermissionsStartOnly = true;
149 # Give Riak a decent amount of time to clean up.
150 TimeoutStopSec = 120;
151 LimitNOFILE = 65536;
152 };
153
154 unitConfig.RequiresMountsFor = [
155 "${cfg.dataDir}"
156 "${cfg.logDir}"
157 "/etc/riak"
158 ];
159 };
160
161 };
162
163}