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