1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 7 b2s = x: if x then "true" else "false"; 8 9 cfg = config.services.mongodb; 10 11 mongodb = cfg.package; 12 13 mongoCnf = pkgs.writeText "mongodb.conf" 14 '' 15 bind_ip = ${cfg.bind_ip} 16 ${optionalString cfg.quiet "quiet = true"} 17 dbpath = ${cfg.dbpath} 18 syslog = true 19 fork = true 20 pidfilepath = ${cfg.pidFile} 21 ${optionalString (cfg.replSetName != "") "replSet = ${cfg.replSetName}"} 22 ${cfg.extraConfig} 23 ''; 24 25in 26 27{ 28 29 ###### interface 30 31 options = { 32 33 services.mongodb = { 34 35 enable = mkOption { 36 default = false; 37 description = " 38 Whether to enable the MongoDB server. 39 "; 40 }; 41 42 package = mkOption { 43 default = pkgs.mongodb; 44 defaultText = "pkgs.mongodb"; 45 type = types.package; 46 description = " 47 Which MongoDB derivation to use. 48 "; 49 }; 50 51 user = mkOption { 52 default = "mongodb"; 53 description = "User account under which MongoDB runs"; 54 }; 55 56 bind_ip = mkOption { 57 default = "127.0.0.1"; 58 description = "IP to bind to"; 59 }; 60 61 quiet = mkOption { 62 default = false; 63 description = "quieter output"; 64 }; 65 66 dbpath = mkOption { 67 default = "/var/db/mongodb"; 68 description = "Location where MongoDB stores its files"; 69 }; 70 71 pidFile = mkOption { 72 default = "/var/run/mongodb.pid"; 73 description = "Location of MongoDB pid file"; 74 }; 75 76 replSetName = mkOption { 77 default = ""; 78 description = '' 79 If this instance is part of a replica set, set its name here. 80 Otherwise, leave empty to run as single node. 81 ''; 82 }; 83 84 extraConfig = mkOption { 85 default = ""; 86 example = '' 87 nojournal = true 88 ''; 89 description = "MongoDB extra configuration"; 90 }; 91 }; 92 93 }; 94 95 96 ###### implementation 97 98 config = mkIf config.services.mongodb.enable { 99 100 users.extraUsers.mongodb = mkIf (cfg.user == "mongodb") 101 { name = "mongodb"; 102 uid = config.ids.uids.mongodb; 103 description = "MongoDB server user"; 104 }; 105 106 environment.systemPackages = [ mongodb ]; 107 108 systemd.services.mongodb = 109 { description = "MongoDB server"; 110 111 wantedBy = [ "multi-user.target" ]; 112 after = [ "network.target" ]; 113 114 serviceConfig = { 115 ExecStart = "${mongodb}/bin/mongod --quiet --config ${mongoCnf}"; 116 User = cfg.user; 117 PIDFile = cfg.pidFile; 118 Type = "forking"; 119 TimeoutStartSec=120; # intial creating of journal can take some time 120 PermissionsStartOnly = true; 121 }; 122 123 preStart = '' 124 rm ${cfg.dbpath}/mongod.lock || true 125 if ! test -e ${cfg.dbpath}; then 126 install -d -m0700 -o ${cfg.user} ${cfg.dbpath} 127 fi 128 if ! test -e ${cfg.pidFile}; then 129 install -D -o ${cfg.user} /dev/null ${cfg.pidFile} 130 fi 131 ''; 132 }; 133 134 }; 135 136}