Add MongoDB example service

This commit is contained in:
Sander van der Burg 2020-04-16 20:09:25 +02:00 committed by Sander van der Burg
parent 72ba3b1452
commit df036873ab
4 changed files with 79 additions and 1 deletions

View File

@ -62,4 +62,14 @@ in
inherit createManagedProcess stateDir runtimeDir cacheDir forceDisableUserChange;
inherit (pkgs) stdenv writeTextFile nginx;
};
mongodb = import ./mongodb.nix {
inherit createManagedProcess runtimeDir;
inherit (pkgs) mongodb;
};
simplemongodb = import ./simplemongodb.nix {
inherit createManagedProcess runtimeDir stateDir forceDisableUserChange;
inherit (pkgs) stdenv mongodb writeTextFile;
};
}

View File

@ -0,0 +1,34 @@
{createManagedProcess, mongodb, runtimeDir}:
{instanceSuffix ? "", configFile, initialize ? "", postInstall ? ""}:
let
instanceName = "mongodb${instanceSuffix}";
user = instanceName;
group = instanceName;
in
createManagedProcess {
name = instanceName;
inherit instanceName initialize postInstall;
process = "${mongodb}/bin/mongod";
args = [ "--config" configFile ];
daemonExtraArgs = [ "--fork" "--pidfilepath" "${runtimeDir}/${instanceName}.pid" ];
user = instanceName;
credentials = {
groups = {
"${group}" = {};
};
users = {
"${user}" = {
inherit group;
description = "MongoDB user";
};
};
};
overrides = {
sysvinit = {
runlevels = [ 3 4 5 ];
};
};
}

View File

@ -3,6 +3,7 @@
, stateDir ? "/var"
, runtimeDir ? "${stateDir}/run"
, logDir ? "${stateDir}/log"
, cacheDir ? "${stateDir}/cache"
, tmpDir ? (if stateDir == "/var" then "/tmp" else "${stateDir}/tmp")
, forceDisableUserChange ? false
, processManager ? "sysvinit"
@ -10,7 +11,7 @@
let
constructors = import ./constructors.nix {
inherit pkgs stateDir runtimeDir logDir tmpDir forceDisableUserChange processManager;
inherit pkgs stateDir runtimeDir logDir tmpDir cacheDir forceDisableUserChange processManager;
};
in
rec {
@ -45,4 +46,8 @@ rec {
inherit httpPort;
};
};
simplemongodb = rec {
pkg = constructors.simplemongodb {};
};
}

View File

@ -0,0 +1,29 @@
{createManagedProcess, stdenv, writeTextFile, mongodb, runtimeDir, stateDir, forceDisableUserChange}:
{instanceSuffix ? "", bindIP ? "127.0.0.1", port ? 27017, postInstall ? ""}:
let
instanceName = "mongodb${instanceSuffix}";
mongodbDir = "${stateDir}/db/${instanceName}";
user = instanceName;
group = instanceName;
in
import ./mongodb.nix {
inherit createManagedProcess mongodb runtimeDir;
} {
inherit instanceSuffix postInstall;
configFile = writeTextFile {
name = "mongodb.conf";
text = ''
systemLog.destination: syslog
storage.dbPath: ${mongodbDir}
net.bindIp: ${bindIP}
net.port: ${toString port}
'';
};
initialize = ''
mkdir -p ${mongodbDir}
${stdenv.lib.optionalString (!forceDisableUserChange) ''
chown ${user}:${group} ${mongodbDir}
''}
'';
}