-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
832eadd
commit d4101b8
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# MongoDB | ||
|
||
[MongoDB](https://www.mongodb.com/) is a popular document database that is available. It comes in a variety of flavors, but the version packaged by NixPkgs is generally the community edition. | ||
|
||
Because of the licensing of MongoDB, the Nixpkgs derivation that provides the binaries of this database are generally not built and cached by the public nixpkgs cache. Because of this, the initial launch time can be very slow, the first time you use this service locally. On a laptop this time ran to about 3 hours for the initial compile. After the initial build, the start up should be very fast, as with other services, provided you do not update your flake.lock. | ||
|
||
If you are using this for your own development, you should either put in place a [local binary cache](https://nixos.wiki/wiki/Binary_Cache) to match your flake, or be aware that the first time you spin up the service it could possibly take a long time to build. | ||
|
||
## Options | ||
|
||
This module uses most of the same common options that other database modules in this flake use. Some of the most sommon ones are | ||
|
||
* `bind` - the address to bind to. Default: 127.0.0.1 | ||
* `port` - the port to serve on. Default: 27017 | ||
* `user` - the MongoDB user to create. Default: default | ||
* `password` - the password for the MongoDB user. Default: password | ||
* `extraConfig` - arbitrary extra lines to add to the MongoDB config file. Default: empty string | ||
* `dataDir` - directory to store data files in. See [datadir](https://github.com/juspay/services-flake/blob/main/doc/datadir.md) | ||
|
||
## Usage example | ||
|
||
<https://github.com/juspay/services-flake/blob/main/nix/services/mongodb_test.nix> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,6 @@ in | |
./weaviate.nix | ||
./searxng.nix | ||
./tika.nix | ||
./mongodb.nix | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{ pkgs, lib, name, config, ... }: | ||
let | ||
inherit (lib) types; | ||
in | ||
{ | ||
options = { | ||
package = lib.mkPackageOption pkgs "mongodb" { }; | ||
|
||
bind = lib.mkOption { | ||
type = types.nullOr types.str; | ||
default = "127.0.0.1"; | ||
description = '' | ||
The IP interface to bind to. | ||
`null` means "all interfaces". | ||
''; | ||
example = "127.0.0.1"; | ||
}; | ||
|
||
port = lib.mkOption { | ||
type = types.port; | ||
default = 27017; | ||
description = '' | ||
The TCP port to accept connections. | ||
''; | ||
}; | ||
|
||
user = lib.mkOption { | ||
type = types.str; | ||
default = "default"; | ||
description = '' | ||
The name of the first user to create in | ||
Mongo. | ||
''; | ||
example = "my_user"; | ||
}; | ||
|
||
password = lib.mkOption { | ||
type = types.str; | ||
default = "password"; | ||
description = '' | ||
The password of the user to configure | ||
for initial access. | ||
''; | ||
}; | ||
|
||
extraConfig = lib.mkOption { | ||
type = types.lines; | ||
default = ""; | ||
description = "Additional text to be appended to `mongodb.conf`."; | ||
}; | ||
}; | ||
|
||
config = { | ||
outputs = { | ||
settings = { | ||
processes = { | ||
"${name}" = | ||
let | ||
mongoConfig = pkgs.writeText "mongodb.conf" '' | ||
net.port: ${toString config.port} | ||
net.bindIp: ${config.bind} | ||
storage.dbPath: ${config.dataDir} | ||
${config.extraConfig} | ||
''; | ||
|
||
startScript = pkgs.writeShellApplication { | ||
name = "start-mongodb"; | ||
runtimeInputs = [ pkgs.coreutils config.package ]; | ||
text = '' | ||
set -euo pipefail | ||
export MONGODATA="${config.dataDir}" | ||
if [[ ! -d "$MONGODATA" ]]; then | ||
mkdir -p "$MONGODATA" | ||
fi | ||
exec mongod --config "${mongoConfig}" | ||
''; | ||
}; | ||
in | ||
{ | ||
command = startScript; | ||
|
||
readiness_probe = { | ||
exec.command = "${pkgs.mongosh}/bin/mongosh --eval \"db.version()\" > /dev/null 2>&1"; | ||
initial_delay_seconds = 2; | ||
period_seconds = 10; | ||
timeout_seconds = 4; | ||
success_threshold = 1; | ||
failure_threshold = 5; | ||
}; | ||
|
||
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy | ||
availability = { | ||
restart = "on_failure"; | ||
max_restarts = 5; | ||
}; | ||
}; | ||
"${name}-configure" = | ||
let | ||
configScript = pkgs.writeShellApplication { | ||
name = "configure-mongo"; | ||
text = '' | ||
if ! test -e "${config.dataDir}/.auth_configured"; then | ||
${config.package}/bin/mongo <<EOF | ||
use admin | ||
db.createUser({ | ||
user: "${config.user}", | ||
pwd: "${config.password}", | ||
roles: [ | ||
{ role: "userAdminAnyDatabase", db: "admin" }, | ||
{ role: "dbAdminAnyDatabase", db: "admin" }, | ||
{ role: "readWriteAnyDatabase", db: "admin" } | ||
] | ||
}) | ||
EOF | ||
touch "${config.dataDir}/.auth_configured" | ||
fi | ||
''; | ||
}; | ||
in | ||
{ | ||
command = configScript; | ||
depends_on."${name}".condition = "process_healthy"; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ pkgs, config, ... }: { | ||
services.mongodb."mongo1".enable = true; | ||
|
||
settings.processes.test = | ||
let | ||
cfg = config.services.mongodb."mongo1"; | ||
in | ||
{ | ||
command = pkgs.writeShellApplication { | ||
runtimeInputs = [ cfg.package pkgs.mongosh ]; | ||
text = '' | ||
mongosh --username default --password password mongodb://127.0.0.1/ --authenticationDatabase admin | ||
''; | ||
name = "mongo-test"; | ||
}; | ||
depends_on."mongo1-configure".condition = "process_completed_successfully"; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters