Skip to content

Commit

Permalink
feat: Add support for mongodb
Browse files Browse the repository at this point in the history
MongoDB currently fails to build on Darwin because of a transitive build
dep on jaraco-path. So we make the test dependent on that. I do not,
however, choose to completely disable MongoDB because someone might have
workarounds for the package building in their environment.
  • Loading branch information
greg-hellings authored and shivaraj-bh committed Oct 10, 2024
1 parent ca67793 commit a5b0c00
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/mongodb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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.

This comment has been minimized.

Copy link
@drupol

drupol Oct 12, 2024

Contributor

As an alternative, mongodb-ce can be used.


## Usage example

<https://github.com/juspay/services-flake/blob/main/nix/services/mongodb_test.nix>
1 change: 1 addition & 0 deletions doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ short-title: Services
- [[grafana]]#
- [[tempo]]
- [[memcached]]#
- [[mongodb]]#
- [[mysql]]#
- [[nginx]]#
- [[ollama]]#
Expand Down
1 change: 1 addition & 0 deletions nix/services/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ in
./weaviate.nix
./searxng.nix
./tika.nix
./mongodb.nix
];
}
133 changes: 133 additions & 0 deletions nix/services/mongodb.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{ 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 = ''
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
${pkgs.mongosh}/bin/mongosh <<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"
else
echo "Database previously configured. If this is in error, remove"
echo "the file at '${config.dataDir}/.auth_configured' and restart"
echo "this process."
fi
'';
};
in
{
command = configScript;
depends_on."${name}".condition = "process_healthy";
};
};
};
};
};
}
22 changes: 22 additions & 0 deletions nix/services/mongodb_test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ 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";
};
}
4 changes: 4 additions & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"${inputs.services-flake}/nix/services/tika_test.nix"
"${inputs.services-flake}/nix/services/weaviate_test.nix"
"${inputs.services-flake}/nix/services/zookeeper_test.nix"
] ++ lib.optionals pkgs.stdenv.hostPlatform.isLinux [
# Due to a dependency problem, MongoDB does not build on Darwin,
# See https://github.com/NixOS/nixpkgs/issues/346003
"${inputs.services-flake}/nix/services/mongodb_test.nix"
]));
};
};
Expand Down

0 comments on commit a5b0c00

Please sign in to comment.