@@ -8,12 +8,13 @@ let
88
99 mongodb = cfg.package;
1010
11− mongoCnf = pkgs.writeText "mongodb.conf"
11+ mongoCnf = cfg: pkgs.writeText "mongodb.conf"
1212 ''
1313 net.bindIp: ${cfg.bind_ip}
1414 ${optionalString cfg.quiet "systemLog.quiet: true"}
1515 systemLog.destination: syslog
1616 storage.dbPath: ${cfg.dbpath}
17+ ${optionalString cfg.enableAuth "security.authorization: enabled"}
1718 ${optionalString (cfg.replSetName != "") "replication.replSetName: ${cfg.replSetName}"}
1819 ${cfg.extraConfig}
1920 '';
@@ -59,6 +60,18 @@ in
5960 description = "quieter output";
6061 };
6162
63+ enableAuth = mkOption {
64+ type = types.bool;
65+ default = false;
66+ description = "Enable client authentication. Creates a default superuser with username root!";
67+ };
68+
69+ initialRootPassword = mkOption {
70+ type = types.nullOr types.string;
71+ default = null;
72+ description = "Password for the root user if auth is enabled.";
73+ };
74+
6275 dbpath = mkOption {
6376 default = "/var/db/mongodb";
6477 description = "Location where MongoDB stores its files";
@@ -84,6 +97,14 @@ in
8497 '';
8598 description = "MongoDB extra configuration in YAML format";
8699 };
100+
101+ initialScript = mkOption {
102+ type = types.nullOr types.path;
103+ default = null;
104+ description = ''
105+ A file containing MongoDB statements to execute on first startup.
106+ '';
107+ };
87108 };
88109
89110 };
@@ -92,6 +113,11 @@ in
92113 ###### implementation
93114
94115 config = mkIf config.services.mongodb.enable {
116+ assertions = [
117+ { assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
118+ message = "`enableAuth` requires `initialRootPassword` to be set.";
119+ }
120+ ];
95121
96122 users.users.mongodb = mkIf (cfg.user == "mongodb")
97123 { name = "mongodb";
@@ -108,7 +134,7 @@ in
108134 after = [ "network.target" ];
109135
110136 serviceConfig = {
111− ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf} --fork --pidfilepath ${cfg.pidFile}";
137+ ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf cfg} --fork --pidfilepath ${cfg.pidFile}";
112138 User = cfg.user;
113139 PIDFile = cfg.pidFile;
114140 Type = "forking";
@@ -116,15 +142,50 @@ in
116142 PermissionsStartOnly = true;
117143 };
118144
119− preStart = ''
145+ preStart = let
146+ cfg_ = cfg // { enableAuth = false; bind_ip = "127.0.0.1"; };
147+ in ''
120148 rm ${cfg.dbpath}/mongod.lock || true
121149 if ! test -e ${cfg.dbpath}; then
122150 install -d -m0700 -o ${cfg.user} ${cfg.dbpath}
151+ # See postStart!
152+ touch ${cfg.dbpath}/.first_startup
123153 fi
124154 if ! test -e ${cfg.pidFile}; then
125155 install -D -o ${cfg.user} /dev/null ${cfg.pidFile}
156+ fi '' + lib.optionalString cfg.enableAuth ''
157+
158+ if ! test -e "${cfg.dbpath}/.auth_setup_complete"; then
159+ systemd-run --unit=mongodb-for-setup --uid=${cfg.user} ${mongodb}/bin/mongod --config ${mongoCnf cfg_}
160+ # wait for mongodb
161+ while ! ${mongodb}/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
162+
163+ ${mongodb}/bin/mongo <<EOF
164+ use admin
165+ db.createUser(
166+ {
167+ user: "root",
168+ pwd: "${cfg.initialRootPassword}",
169+ roles: [
170+ { role: "userAdminAnyDatabase", db: "admin" },
171+ { role: "dbAdminAnyDatabase", db: "admin" },
172+ { role: "readWriteAnyDatabase", db: "admin" }
173+ ]
174+ }
175+ )
176+ EOF
177+ touch "${cfg.dbpath}/.auth_setup_complete"
178+ systemctl stop mongodb-for-setup
126179 fi
127180 '';
181+ postStart = ''
182+ if test -e "${cfg.dbpath}/.first_startup"; then
183+ ${optionalString (cfg.initialScript != null) ''
184+ ${mongodb}/bin/mongo -u root -p ${cfg.initialRootPassword} admin "${cfg.initialScript}"
185+ ''}
186+ rm -f "${cfg.dbpath}/.first_startup"
187+ fi
188+ '';
128189 };
129190
130191 };