Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for mongodb #339

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

## 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
shivaraj-bh marked this conversation as resolved.
Show resolved Hide resolved
${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