1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.mongodb;
8
9 mongodb = cfg.package;
10
11 mongoCnf = cfg: pkgs.writeText "mongodb.conf"
12 ''
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}"}
19 ${cfg.extraConfig}
20 '';
21
22in
23
24{
25
26 ###### interface
27
28 options = {
29
30 services.mongodb = {
31
32 enable = mkEnableOption "the MongoDB server";
33
34 package = mkOption {
35 default = pkgs.mongodb;
36 defaultText = "pkgs.mongodb";
37 type = types.package;
38 description = "
39 Which MongoDB derivation to use.
40 ";
41 };
42
43 user = mkOption {
44 type = types.str;
45 default = "mongodb";
46 description = "User account under which MongoDB runs";
47 };
48
49 bind_ip = mkOption {
50 type = types.str;
51 default = "127.0.0.1";
52 description = "IP to bind to";
53 };
54
55 quiet = mkOption {
56 type = types.bool;
57 default = false;
58 description = "quieter output";
59 };
60
61 enableAuth = mkOption {
62 type = types.bool;
63 default = false;
64 description = "Enable client authentication. Creates a default superuser with username root!";
65 };
66
67 initialRootPassword = mkOption {
68 type = types.nullOr types.str;
69 default = null;
70 description = "Password for the root user if auth is enabled.";
71 };
72
73 dbpath = mkOption {
74 type = types.str;
75 default = "/var/db/mongodb";
76 description = "Location where MongoDB stores its files";
77 };
78
79 pidFile = mkOption {
80 type = types.str;
81 default = "/run/mongodb.pid";
82 description = "Location of MongoDB pid file";
83 };
84
85 replSetName = mkOption {
86 type = types.str;
87 default = "";
88 description = ''
89 If this instance is part of a replica set, set its name here.
90 Otherwise, leave empty to run as single node.
91 '';
92 };
93
94 extraConfig = mkOption {
95 type = types.lines;
96 default = "";
97 example = ''
98 storage.journal.enabled: false
99 '';
100 description = "MongoDB extra configuration in YAML format";
101 };
102
103 initialScript = mkOption {
104 type = types.nullOr types.path;
105 default = null;
106 description = ''
107 A file containing MongoDB statements to execute on first startup.
108 '';
109 };
110 };
111
112 };
113
114
115 ###### implementation
116
117 config = mkIf config.services.mongodb.enable {
118 assertions = [
119 { assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
120 message = "`enableAuth` requires `initialRootPassword` to be set.";
121 }
122 ];
123
124 users.users.mongodb = mkIf (cfg.user == "mongodb")
125 { name = "mongodb";
126 uid = config.ids.uids.mongodb;
127 description = "MongoDB server user";
128 };
129
130 environment.systemPackages = [ mongodb ];
131
132 systemd.services.mongodb =
133 { description = "MongoDB server";
134
135 wantedBy = [ "multi-user.target" ];
136 after = [ "network.target" ];
137
138 serviceConfig = {
139 ExecStart = "${mongodb}/bin/mongod --config ${mongoCnf cfg} --fork --pidfilepath ${cfg.pidFile}";
140 User = cfg.user;
141 PIDFile = cfg.pidFile;
142 Type = "forking";
143 TimeoutStartSec=120; # intial creating of journal can take some time
144 PermissionsStartOnly = true;
145 };
146
147 preStart = let
148 cfg_ = cfg // { enableAuth = false; bind_ip = "127.0.0.1"; };
149 in ''
150 rm ${cfg.dbpath}/mongod.lock || true
151 if ! test -e ${cfg.dbpath}; then
152 install -d -m0700 -o ${cfg.user} ${cfg.dbpath}
153 # See postStart!
154 touch ${cfg.dbpath}/.first_startup
155 fi
156 if ! test -e ${cfg.pidFile}; then
157 install -D -o ${cfg.user} /dev/null ${cfg.pidFile}
158 fi '' + lib.optionalString cfg.enableAuth ''
159
160 if ! test -e "${cfg.dbpath}/.auth_setup_complete"; then
161 systemd-run --unit=mongodb-for-setup --uid=${cfg.user} ${mongodb}/bin/mongod --config ${mongoCnf cfg_}
162 # wait for mongodb
163 while ! ${mongodb}/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
164
165 ${mongodb}/bin/mongo <<EOF
166 use admin
167 db.createUser(
168 {
169 user: "root",
170 pwd: "${cfg.initialRootPassword}",
171 roles: [
172 { role: "userAdminAnyDatabase", db: "admin" },
173 { role: "dbAdminAnyDatabase", db: "admin" },
174 { role: "readWriteAnyDatabase", db: "admin" }
175 ]
176 }
177 )
178 EOF
179 touch "${cfg.dbpath}/.auth_setup_complete"
180 systemctl stop mongodb-for-setup
181 fi
182 '';
183 postStart = ''
184 if test -e "${cfg.dbpath}/.first_startup"; then
185 ${optionalString (cfg.initialScript != null) ''
186 ${mongodb}/bin/mongo ${optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}"
187 ''}
188 rm -f "${cfg.dbpath}/.first_startup"
189 fi
190 '';
191 };
192
193 };
194
195}