2022-01-07 04:07:37 +00:00
|
|
|
{ config, options, lib, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.couchdb;
|
2022-01-07 04:07:37 +00:00
|
|
|
opt = options.services.couchdb;
|
2020-04-24 23:36:52 +00:00
|
|
|
configFile = pkgs.writeText "couchdb.ini" (
|
|
|
|
''
|
|
|
|
[couchdb]
|
|
|
|
database_dir = ${cfg.databaseDir}
|
|
|
|
uri_file = ${cfg.uriFile}
|
|
|
|
view_index_dir = ${cfg.viewIndexDir}
|
2021-05-20 23:08:51 +00:00
|
|
|
'' + (optionalString (cfg.adminPass != null) ''
|
2020-09-25 04:45:31 +00:00
|
|
|
[admins]
|
|
|
|
${cfg.adminUser} = ${cfg.adminPass}
|
2021-05-20 23:08:51 +00:00
|
|
|
'' + ''
|
2020-04-24 23:36:52 +00:00
|
|
|
[chttpd]
|
|
|
|
'') +
|
|
|
|
''
|
|
|
|
port = ${toString cfg.port}
|
|
|
|
bind_address = ${cfg.bindAddress}
|
|
|
|
|
|
|
|
[log]
|
|
|
|
file = ${cfg.logFile}
|
|
|
|
'');
|
2021-05-20 23:08:51 +00:00
|
|
|
executable = "${cfg.package}/bin/couchdb";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.couchdb = {
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "CouchDB Server");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2021-12-19 01:06:50 +00:00
|
|
|
default = pkgs.couchdb3;
|
|
|
|
defaultText = literalExpression "pkgs.couchdb3";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
CouchDB package to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
adminUser = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "admin";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-09-25 04:45:31 +00:00
|
|
|
Couchdb (i.e. fauxton) account with permission for all dbs and
|
|
|
|
tasks.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
adminPass = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-09-25 04:45:31 +00:00
|
|
|
Couchdb (i.e. fauxton) account with permission for all dbs and
|
|
|
|
tasks.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "couchdb";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
User account under which couchdb runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "couchdb";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Group account under which couchdb runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
# couchdb options: http://docs.couchdb.org/en/latest/config/index.html
|
|
|
|
|
|
|
|
databaseDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/couchdb";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies location of CouchDB database files (*.couch named). This
|
|
|
|
location should be writable and readable for the user the CouchDB
|
|
|
|
service runs as (couchdb by default).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
uriFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/run/couchdb/couchdb.uri";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This file contains the full URI that can be used to access this
|
|
|
|
instance of CouchDB. It is used to help discover the port CouchDB is
|
|
|
|
running on (if it was set to 0 (e.g. automatically assigned any free
|
|
|
|
one). This file should be writable and readable for the user that
|
|
|
|
runs the CouchDB service (couchdb by default).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
viewIndexDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/couchdb";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies location of CouchDB view index files. This location should
|
|
|
|
be writable and readable for the user that runs the CouchDB service
|
|
|
|
(couchdb by default).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
bindAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Defines the IP address by which CouchDB will be accessible.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 5984;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Defined the port number to listen.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
logFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/log/couchdb.log";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies the location of file for logging output.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Extra configuration. Overrides any other cofiguration.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
argsFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${cfg.package}/etc/vm.args";
|
2022-01-07 04:07:37 +00:00
|
|
|
defaultText = literalExpression ''"config.${opt.package}/etc/vm.args"'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-12-19 01:06:50 +00:00
|
|
|
vm.args configuration. Overrides Couchdb's Erlang VM parameters file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
configFile = mkOption {
|
|
|
|
type = types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Configuration file for persisting runtime changes. File
|
|
|
|
needs to be readable and writable from couchdb user/group.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.couchdb.enable {
|
|
|
|
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
services.couchdb.configFile = mkDefault "/var/lib/couchdb/local.ini";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${dirOf cfg.uriFile}' - ${cfg.user} ${cfg.group} - -"
|
|
|
|
"f '${cfg.logFile}' - ${cfg.user} ${cfg.group} - -"
|
|
|
|
"d '${cfg.databaseDir}' - ${cfg.user} ${cfg.group} - -"
|
|
|
|
"d '${cfg.viewIndexDir}' - ${cfg.user} ${cfg.group} - -"
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.couchdb = {
|
|
|
|
description = "CouchDB Server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
touch ${cfg.configFile}
|
2022-04-27 09:35:20 +00:00
|
|
|
if ! test -e ${cfg.databaseDir}/.erlang.cookie; then
|
|
|
|
touch ${cfg.databaseDir}/.erlang.cookie
|
|
|
|
chmod 600 ${cfg.databaseDir}/.erlang.cookie
|
|
|
|
dd if=/dev/random bs=16 count=1 | base64 > ${cfg.databaseDir}/.erlang.cookie
|
|
|
|
fi
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
environment = {
|
2021-12-19 01:06:50 +00:00
|
|
|
# we are actually specifying 5 configuration files:
|
2020-04-24 23:36:52 +00:00
|
|
|
# 1. the preinstalled default.ini
|
|
|
|
# 2. the module configuration
|
|
|
|
# 3. the extraConfig from the module options
|
|
|
|
# 4. the locally writable config file, which couchdb itself writes to
|
|
|
|
ERL_FLAGS= ''-couch_ini ${cfg.package}/etc/default.ini ${configFile} ${pkgs.writeText "couchdb-extra.ini" cfg.extraConfig} ${cfg.configFile}'';
|
2021-12-19 01:06:50 +00:00
|
|
|
# 5. the vm.args file
|
|
|
|
COUCHDB_ARGS_FILE=''${cfg.argsFile}'';
|
2022-04-27 09:35:20 +00:00
|
|
|
HOME =''${cfg.databaseDir}'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
ExecStart = executable;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.couchdb = {
|
|
|
|
description = "CouchDB Server user";
|
|
|
|
group = "couchdb";
|
|
|
|
uid = config.ids.uids.couchdb;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.couchdb.gid = config.ids.gids.couchdb;
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|