1 { config, options, lib, pkgs, ... }:
3 cfg = config.services.couchdb;
4 opt = options.services.couchdb;
5 configFile = pkgs.writeText "couchdb.ini" (
8 database_dir = ${cfg.databaseDir}
9 uri_file = ${cfg.uriFile}
10 view_index_dir = ${cfg.viewIndexDir}
11 '' + (lib.optionalString (cfg.adminPass != null) ''
13 ${cfg.adminUser} = ${cfg.adminPass}
18 port = ${toString cfg.port}
19 bind_address = ${cfg.bindAddress}
24 executable = "${cfg.package}/bin/couchdb";
34 enable = lib.mkEnableOption "CouchDB Server";
36 package = lib.mkPackageOption pkgs "couchdb3" { };
38 adminUser = lib.mkOption {
42 Couchdb (i.e. fauxton) account with permission for all dbs and
47 adminPass = lib.mkOption {
48 type = lib.types.nullOr lib.types.str;
51 Couchdb (i.e. fauxton) account with permission for all dbs and
60 User account under which couchdb runs.
64 group = lib.mkOption {
68 Group account under which couchdb runs.
72 # couchdb options: https://docs.couchdb.org/en/latest/config/index.html
74 databaseDir = lib.mkOption {
75 type = lib.types.path;
76 default = "/var/lib/couchdb";
78 Specifies location of CouchDB database files (*.couch named). This
79 location should be writable and readable for the user the CouchDB
80 service runs as (couchdb by default).
84 uriFile = lib.mkOption {
85 type = lib.types.path;
86 default = "/run/couchdb/couchdb.uri";
88 This file contains the full URI that can be used to access this
89 instance of CouchDB. It is used to help discover the port CouchDB is
90 running on (if it was set to 0 (e.g. automatically assigned any free
91 one). This file should be writable and readable for the user that
92 runs the CouchDB service (couchdb by default).
96 viewIndexDir = lib.mkOption {
97 type = lib.types.path;
98 default = "/var/lib/couchdb";
100 Specifies location of CouchDB view index files. This location should
101 be writable and readable for the user that runs the CouchDB service
102 (couchdb by default).
106 bindAddress = lib.mkOption {
107 type = lib.types.str;
108 default = "127.0.0.1";
110 Defines the IP address by which CouchDB will be accessible.
114 port = lib.mkOption {
115 type = lib.types.port;
118 Defined the port number to listen.
122 logFile = lib.mkOption {
123 type = lib.types.path;
124 default = "/var/log/couchdb.log";
126 Specifies the location of file for logging output.
130 extraConfig = lib.mkOption {
131 type = lib.types.lines;
134 Extra configuration. Overrides any other configuration.
138 argsFile = lib.mkOption {
139 type = lib.types.path;
140 default = "${cfg.package}/etc/vm.args";
141 defaultText = lib.literalExpression ''"config.${opt.package}/etc/vm.args"'';
143 vm.args configuration. Overrides Couchdb's Erlang VM parameters file.
147 configFile = lib.mkOption {
148 type = lib.types.path;
150 Configuration file for persisting runtime changes. File
151 needs to be readable and writable from couchdb user/group.
159 ###### implementation
161 config = lib.mkIf config.services.couchdb.enable {
163 environment.systemPackages = [ cfg.package ];
165 services.couchdb.configFile = lib.mkDefault "/var/lib/couchdb/local.ini";
167 systemd.tmpfiles.rules = [
168 "d '${dirOf cfg.uriFile}' - ${cfg.user} ${cfg.group} - -"
169 "f '${cfg.logFile}' - ${cfg.user} ${cfg.group} - -"
170 "d '${cfg.databaseDir}' - ${cfg.user} ${cfg.group} - -"
171 "d '${cfg.viewIndexDir}' - ${cfg.user} ${cfg.group} - -"
174 systemd.services.couchdb = {
175 description = "CouchDB Server";
176 wantedBy = [ "multi-user.target" ];
179 touch ${cfg.configFile}
180 if ! test -e ${cfg.databaseDir}/.erlang.cookie; then
181 touch ${cfg.databaseDir}/.erlang.cookie
182 chmod 600 ${cfg.databaseDir}/.erlang.cookie
183 dd if=/dev/random bs=16 count=1 | base64 > ${cfg.databaseDir}/.erlang.cookie
188 # we are actually specifying 5 configuration files:
189 # 1. the preinstalled default.ini
190 # 2. the module configuration
191 # 3. the extraConfig from the module options
192 # 4. the locally writable config file, which couchdb itself writes to
193 ERL_FLAGS= ''-couch_ini ${cfg.package}/etc/default.ini ${configFile} ${pkgs.writeText "couchdb-extra.ini" cfg.extraConfig} ${cfg.configFile}'';
194 # 5. the vm.args file
195 COUCHDB_ARGS_FILE=''${cfg.argsFile}'';
196 HOME =''${cfg.databaseDir}'';
202 ExecStart = executable;
206 users.users.couchdb = {
207 description = "CouchDB Server user";
209 uid = config.ids.uids.couchdb;
212 users.groups.couchdb.gid = config.ids.gids.couchdb;