1 { config, lib, pkgs, ... }:
7 cfg = config.services.mongodb;
11 mongoCnf = cfg: pkgs.writeText "mongodb.conf"
13 net.bindIp: ${cfg.bind_ip}
14 ${optionalString cfg.quiet "systemLog.quiet: true"}
15 systemLog.destination: syslog
16 storage.dbPath: ${cfg.dbpath}
17 ${optionalString cfg.enableAuth "security.authorization: enabled"}
18 ${optionalString (cfg.replSetName != "") "replication.replSetName: ${cfg.replSetName}"}
32 enable = mkEnableOption "the MongoDB server";
34 package = mkPackageOption pkgs "mongodb" { };
39 description = "User account under which MongoDB runs";
44 default = "127.0.0.1";
45 description = "IP to bind to";
51 description = "quieter output";
54 enableAuth = mkOption {
57 description = "Enable client authentication. Creates a default superuser with username root!";
60 initialRootPassword = mkOption {
61 type = types.nullOr types.str;
63 description = "Password for the root user if auth is enabled.";
68 default = "/var/db/mongodb";
69 description = "Location where MongoDB stores its files";
74 default = "/run/mongodb.pid";
75 description = "Location of MongoDB pid file";
78 replSetName = mkOption {
82 If this instance is part of a replica set, set its name here.
83 Otherwise, leave empty to run as single node.
87 extraConfig = mkOption {
91 storage.journal.enabled: false
93 description = "MongoDB extra configuration in YAML format";
96 initialScript = mkOption {
97 type = types.nullOr types.path;
100 A file containing MongoDB statements to execute on first startup.
108 ###### implementation
110 config = mkIf config.services.mongodb.enable {
112 { assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
113 message = "`enableAuth` requires `initialRootPassword` to be set.";
117 users.users.mongodb = mkIf (cfg.user == "mongodb")
121 description = "MongoDB server user";
123 users.groups.mongodb = mkIf (cfg.user == "mongodb") {};
125 environment.systemPackages = [ mongodb ];
127 systemd.services.mongodb =
128 { description = "MongoDB server";
130 wantedBy = [ "multi-user.target" ];
131 after = [ "network.target" ];
134 ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf cfg} --fork --pidfilepath ${cfg.pidFile}";
136 PIDFile = cfg.pidFile;
138 TimeoutStartSec=120; # initial creating of journal can take some time
139 PermissionsStartOnly = true;
143 cfg_ = cfg // { enableAuth = false; bind_ip = "127.0.0.1"; };
145 rm ${cfg.dbpath}/mongod.lock || true
146 if ! test -e ${cfg.dbpath}; then
147 install -d -m0700 -o ${cfg.user} ${cfg.dbpath}
149 touch ${cfg.dbpath}/.first_startup
151 if ! test -e ${cfg.pidFile}; then
152 install -D -o ${cfg.user} /dev/null ${cfg.pidFile}
153 fi '' + lib.optionalString cfg.enableAuth ''
155 if ! test -e "${cfg.dbpath}/.auth_setup_complete"; then
156 systemd-run --unit=mongodb-for-setup --uid=${cfg.user} ${mongodb}/bin/mongod --config ${mongoCnf cfg_}
158 while ! ${mongodb}/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
160 ${mongodb}/bin/mongo <<EOF
165 pwd: "${cfg.initialRootPassword}",
167 { role: "userAdminAnyDatabase", db: "admin" },
168 { role: "dbAdminAnyDatabase", db: "admin" },
169 { role: "readWriteAnyDatabase", db: "admin" }
174 touch "${cfg.dbpath}/.auth_setup_complete"
175 systemctl stop mongodb-for-setup
179 if test -e "${cfg.dbpath}/.first_startup"; then
180 ${optionalString (cfg.initialScript != null) ''
181 ${mongodb}/bin/mongo ${optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}"
183 rm -f "${cfg.dbpath}/.first_startup"