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 committed Oct 2, 2024
1 parent 832eadd commit 18a962e
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doc/mongodb.md
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>
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
];
}
131 changes: 131 additions & 0 deletions nix/services/mongodb.nix
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";
};
};
};
};
};
}
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.python3.pkgs.jaraco-path.meta.broken) [
# Due to a dependency problem, MongoDB does not build on Darwin,
# and it is due to the dependency on jaraco-path
"${inputs.services-flake}/nix/services/mongodb_test.nix"
]));
};
};
Expand Down

0 comments on commit 18a962e

Please sign in to comment.