From 2861aa76998630664c467fc5c8159a66791ca1f7 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Wed, 18 Sep 2024 12:51:07 -0500 Subject: [PATCH] feat: Add support for mongodb 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. --- doc/mongodb.md | 11 +++ doc/services.md | 1 + nix/services/default.nix | 1 + nix/services/mongodb.nix | 133 ++++++++++++++++++++++++++++++++++ nix/services/mongodb_test.nix | 22 ++++++ test/flake.nix | 4 + 6 files changed, 172 insertions(+) create mode 100644 doc/mongodb.md create mode 100644 nix/services/mongodb.nix create mode 100644 nix/services/mongodb_test.nix diff --git a/doc/mongodb.md b/doc/mongodb.md new file mode 100644 index 00000000..0f286b76 --- /dev/null +++ b/doc/mongodb.md @@ -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 + + diff --git a/doc/services.md b/doc/services.md index c52ac0dc..6c8404e2 100644 --- a/doc/services.md +++ b/doc/services.md @@ -11,6 +11,7 @@ short-title: Services - [[grafana]]# - [[tempo]] - [[memcached]]# +- [[mongodb]]# - [[mysql]]# - [[nginx]]# - [[ollama]]# diff --git a/nix/services/default.nix b/nix/services/default.nix index 5591c5e3..f3b9dc10 100644 --- a/nix/services/default.nix +++ b/nix/services/default.nix @@ -23,5 +23,6 @@ in ./weaviate.nix ./searxng.nix ./tika.nix + ./mongodb.nix ]; } diff --git a/nix/services/mongodb.nix b/nix/services/mongodb.nix new file mode 100644 index 00000000..6dc599c0 --- /dev/null +++ b/nix/services/mongodb.nix @@ -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 <