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