2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.mongodb;
|
|
|
|
|
|
|
|
mongodb = cfg.package;
|
|
|
|
|
|
|
|
mongoCnf = cfg: pkgs.writeText "mongodb.conf"
|
|
|
|
''
|
|
|
|
net.bindIp: ${cfg.bind_ip}
|
|
|
|
${optionalString cfg.quiet "systemLog.quiet: true"}
|
|
|
|
systemLog.destination: syslog
|
|
|
|
storage.dbPath: ${cfg.dbpath}
|
|
|
|
${optionalString cfg.enableAuth "security.authorization: enabled"}
|
|
|
|
${optionalString (cfg.replSetName != "") "replication.replSetName: ${cfg.replSetName}"}
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.mongodb = {
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "the MongoDB server");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.mongodb;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.mongodb";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.package;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Which MongoDB derivation to use.
|
2022-08-21 13:32:41 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "mongodb";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "User account under which MongoDB runs";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bind_ip = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "127.0.0.1";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "IP to bind to";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
quiet = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "quieter output";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enableAuth = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Enable client authentication. Creates a default superuser with username root!";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
initialRootPassword = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Password for the root user if auth is enabled.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
dbpath = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "/var/db/mongodb";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Location where MongoDB stores its files";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pidFile = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "/run/mongodb.pid";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Location of MongoDB pid file";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
replSetName = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
If this instance is part of a replica set, set its name here.
|
|
|
|
Otherwise, leave empty to run as single node.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
2021-01-15 22:18:51 +00:00
|
|
|
type = types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
storage.journal.enabled: false
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "MongoDB extra configuration in YAML format";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
initialScript = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
A file containing MongoDB statements to execute on first startup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.mongodb.enable {
|
|
|
|
assertions = [
|
|
|
|
{ assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
|
|
|
|
message = "`enableAuth` requires `initialRootPassword` to be set.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.mongodb = mkIf (cfg.user == "mongodb")
|
|
|
|
{ name = "mongodb";
|
2021-09-18 10:52:07 +00:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "mongodb";
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "MongoDB server user";
|
|
|
|
};
|
2021-09-18 10:52:07 +00:00
|
|
|
users.groups.mongodb = mkIf (cfg.user == "mongodb") {};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
environment.systemPackages = [ mongodb ];
|
|
|
|
|
|
|
|
systemd.services.mongodb =
|
|
|
|
{ description = "MongoDB server";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf cfg} --fork --pidfilepath ${cfg.pidFile}";
|
|
|
|
User = cfg.user;
|
|
|
|
PIDFile = cfg.pidFile;
|
|
|
|
Type = "forking";
|
|
|
|
TimeoutStartSec=120; # intial creating of journal can take some time
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
preStart = let
|
|
|
|
cfg_ = cfg // { enableAuth = false; bind_ip = "127.0.0.1"; };
|
|
|
|
in ''
|
|
|
|
rm ${cfg.dbpath}/mongod.lock || true
|
|
|
|
if ! test -e ${cfg.dbpath}; then
|
|
|
|
install -d -m0700 -o ${cfg.user} ${cfg.dbpath}
|
|
|
|
# See postStart!
|
|
|
|
touch ${cfg.dbpath}/.first_startup
|
|
|
|
fi
|
|
|
|
if ! test -e ${cfg.pidFile}; then
|
|
|
|
install -D -o ${cfg.user} /dev/null ${cfg.pidFile}
|
|
|
|
fi '' + lib.optionalString cfg.enableAuth ''
|
|
|
|
|
|
|
|
if ! test -e "${cfg.dbpath}/.auth_setup_complete"; then
|
|
|
|
systemd-run --unit=mongodb-for-setup --uid=${cfg.user} ${mongodb}/bin/mongod --config ${mongoCnf cfg_}
|
|
|
|
# wait for mongodb
|
|
|
|
while ! ${mongodb}/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
|
|
|
|
|
|
|
|
${mongodb}/bin/mongo <<EOF
|
|
|
|
use admin
|
|
|
|
db.createUser(
|
|
|
|
{
|
|
|
|
user: "root",
|
|
|
|
pwd: "${cfg.initialRootPassword}",
|
|
|
|
roles: [
|
|
|
|
{ role: "userAdminAnyDatabase", db: "admin" },
|
|
|
|
{ role: "dbAdminAnyDatabase", db: "admin" },
|
|
|
|
{ role: "readWriteAnyDatabase", db: "admin" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
EOF
|
|
|
|
touch "${cfg.dbpath}/.auth_setup_complete"
|
|
|
|
systemctl stop mongodb-for-setup
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
postStart = ''
|
|
|
|
if test -e "${cfg.dbpath}/.first_startup"; then
|
|
|
|
${optionalString (cfg.initialScript != null) ''
|
2020-12-25 13:55:36 +00:00
|
|
|
${mongodb}/bin/mongo ${optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}"
|
2020-04-24 23:36:52 +00:00
|
|
|
''}
|
|
|
|
rm -f "${cfg.dbpath}/.first_startup"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|