1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.couchdb;
7 configFile = pkgs.writeText "couchdb.ini"
8 ''
9 [couchdb]
10 database_dir = ${cfg.databaseDir}
11 uri_file = ${cfg.uriFile}
12 view_index_dir = ${cfg.viewIndexDir}
13
14 [httpd]
15 port = ${toString cfg.port}
16 bind_address = ${cfg.bindAddress}
17
18 [log]
19 file = ${cfg.logFile}
20 '';
21
22in {
23
24 ###### interface
25
26 options = {
27
28 services.couchdb = {
29
30 enable = mkOption {
31 type = types.bool;
32 default = false;
33 description = ''
34 Whether to run CouchDB Server.
35 '';
36 };
37
38 package = mkOption {
39 type = types.package;
40 default = pkgs.couchdb;
41 defaultText = "pkgs.couchdb";
42 example = literalExample "pkgs.couchdb";
43 description = ''
44 CouchDB package to use.
45 '';
46 };
47
48
49 user = mkOption {
50 type = types.string;
51 default = "couchdb";
52 description = ''
53 User account under which couchdb runs.
54 '';
55 };
56
57 group = mkOption {
58 type = types.string;
59 default = "couchdb";
60 description = ''
61 Group account under which couchdb runs.
62 '';
63 };
64
65 # couchdb options: http://docs.couchdb.org/en/latest/config/index.html
66
67 databaseDir = mkOption {
68 type = types.path;
69 default = "/var/lib/couchdb";
70 description = ''
71 Specifies location of CouchDB database files (*.couch named). This
72 location should be writable and readable for the user the CouchDB
73 service runs as (couchdb by default).
74 '';
75 };
76
77 uriFile = mkOption {
78 type = types.path;
79 default = "/var/run/couchdb/couchdb.uri";
80 description = ''
81 This file contains the full URI that can be used to access this
82 instance of CouchDB. It is used to help discover the port CouchDB is
83 running on (if it was set to 0 (e.g. automatically assigned any free
84 one). This file should be writable and readable for the user that
85 runs the CouchDB service (couchdb by default).
86 '';
87 };
88
89 viewIndexDir = mkOption {
90 type = types.path;
91 default = "/var/lib/couchdb";
92 description = ''
93 Specifies location of CouchDB view index files. This location should
94 be writable and readable for the user that runs the CouchDB service
95 (couchdb by default).
96 '';
97 };
98
99 bindAddress = mkOption {
100 type = types.string;
101 default = "127.0.0.1";
102 description = ''
103 Defines the IP address by which CouchDB will be accessible.
104 '';
105 };
106
107 port = mkOption {
108 type = types.int;
109 default = 5984;
110 description = ''
111 Defined the port number to listen.
112 '';
113 };
114
115 logFile = mkOption {
116 type = types.path;
117 default = "/var/log/couchdb.log";
118 description = ''
119 Specifies the location of file for logging output.
120 '';
121 };
122
123 extraConfig = mkOption {
124 type = types.lines;
125 default = "";
126 description = ''
127 Extra configuration. Overrides any other cofiguration.
128 '';
129 };
130
131 configFile = mkOption {
132 type = types.string;
133 default = "/var/lib/couchdb/couchdb.ini";
134 description = ''
135 Configuration file for persisting runtime changes. File
136 needs to be readable and writable from couchdb user/group.
137 '';
138 };
139
140 };
141
142 };
143
144 ###### implementation
145
146 config = mkIf config.services.couchdb.enable {
147
148 environment.systemPackages = [ cfg.package ];
149
150 systemd.services.couchdb = {
151 description = "CouchDB Server";
152 wantedBy = [ "multi-user.target" ];
153
154 preStart =
155 ''
156 mkdir -p `dirname ${cfg.uriFile}`;
157 mkdir -p `dirname ${cfg.logFile}`;
158 mkdir -p ${cfg.databaseDir};
159 mkdir -p ${cfg.viewIndexDir};
160 touch ${cfg.configFile}
161 touch -a ${cfg.logFile}
162
163 if [ "$(id -u)" = 0 ]; then
164 chown ${cfg.user}:${cfg.group} `dirname ${cfg.uriFile}`;
165 (-f ${cfg.uriFile} && chown ${cfg.user}:${cfg.group} ${cfg.uriFile}) || true
166 chown ${cfg.user}:${cfg.group} ${cfg.databaseDir}
167 chown ${cfg.user}:${cfg.group} ${cfg.viewIndexDir}
168 chown ${cfg.user}:${cfg.group} ${cfg.configFile}
169 chown ${cfg.user}:${cfg.group} ${cfg.logFile}
170 fi
171 '';
172
173 serviceConfig = {
174 PermissionsStartOnly = true;
175 User = cfg.user;
176 Group = cfg.group;
177 ExecStart = "${cfg.package}/bin/couchdb -a ${configFile} -a ${pkgs.writeText "couchdb-extra.ini" cfg.extraConfig} -a ${cfg.configFile}";
178 };
179 };
180
181 users.extraUsers.couchdb = {
182 description = "CouchDB Server user";
183 group = "couchdb";
184 uid = config.ids.uids.couchdb;
185 };
186
187 users.extraGroups.couchdb.gid = config.ids.gids.couchdb;
188
189 };
190}