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