nixos/mongodb: add support for mongodb-ce package (#375587)

This commit is contained in:
Pol Dellaiera
2025-01-24 19:24:49 +01:00
committed by GitHub
5 changed files with 79 additions and 74 deletions
@@ -226,6 +226,8 @@
- `racket_7_9` has been removed, as it is insecure. It is recommended to use Racket 8 instead.
- `services.mongodb.initialRootPassword` has been replaced with the more secure option [`services.mongodb.initialRootPasswordFile`](#opt-services.mongodb.initialRootPasswordFile)
- `rofi` has been updated from 1.7.5 to 1.7.6 which introduces some breaking changes to binary plugins, and also contains a lot of new features and bug fixes. This is highlighted because the patch version bump does not indicate the volume of changes by itself. See the [upstream release notes](https://github.com/davatorium/rofi/releases/tag/1.7.6) for the full list of changes.
- `ente-auth` now uses the name `enteauth` for its binary. The previous name was `ente_auth`.
@@ -386,6 +388,8 @@
- GOverlay has been updated to 1.2, please check the [upstream changelog](https://github.com/benjamimgois/goverlay/releases) for more details.
- [`services.mongodb`](#opt-services.mongodb.enable) is now compatible with the `mongodb-ce` binary package. To make use of it, set [`services.mongodb.package`](#opt-services.mongodb.package) to `pkgs.mongodb-ce`.
- [`services.jupyter`](#opt-services.jupyter.enable) is now compatible with `Jupyter Notebook 7`. See [the migration guide](https://jupyter-notebook.readthedocs.io/en/latest/migrate_to_notebook7.html) for details.
- `networking.wireguard` now has an optional networkd backend. It is enabled by default when `networking.useNetworkd` is enabled, and it can be enabled alongside scripted networking with `networking.wireguard.useNetworkd`. Some `networking.wireguard` options have slightly different behavior with the networkd and script-based backends, documented in each option.
@@ -398,6 +402,8 @@
- `bind.cacheNetworks` now only controls access for recursive queries, where it previously controlled access for all queries.
- [`services.mongodb.enableAuth`](#opt-services.mongodb.enableAuth) now uses the newer [mongosh](https://github.com/mongodb-js/mongosh) shell instead of the legacy shell to configure the initial superuser. You can configure the mongosh package to use through the [`services.mongodb.mongoshPackage`](#opt-services.mongodb.mongoshPackage) option.
- The paperless module now has an option for regular automatic export of
documents data using the integrated document exporter.
+26 -13
View File
@@ -10,6 +10,8 @@ let
mongodb = cfg.package;
mongoshExe = lib.getExe cfg.mongoshPackage;
mongoCnf =
cfg:
pkgs.writeText "mongodb.conf" ''
@@ -25,6 +27,13 @@ let
in
{
imports = [
(lib.mkRemovedOptionModule [
"services"
"mongodb"
"initialRootPassword"
] "Use services.mongodb.initialRootPasswordFile to securely provide the initial root password.")
];
###### interface
@@ -34,7 +43,11 @@ in
enable = lib.mkEnableOption "the MongoDB server";
package = lib.mkPackageOption pkgs "mongodb" { };
package = lib.mkPackageOption pkgs "mongodb" {
example = "pkgs.mongodb-ce";
};
mongoshPackage = lib.mkPackageOption pkgs "mongosh" { };
user = lib.mkOption {
type = lib.types.str;
@@ -60,10 +73,10 @@ in
description = "Enable client authentication. Creates a default superuser with username root!";
};
initialRootPassword = lib.mkOption {
type = lib.types.nullOr lib.types.str;
initialRootPasswordFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Password for the root user if auth is enabled.";
description = "Path to the file containing the password for the root user if auth is enabled.";
};
dbpath = lib.mkOption {
@@ -112,8 +125,8 @@ in
config = lib.mkIf config.services.mongodb.enable {
assertions = [
{
assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
message = "`enableAuth` requires `initialRootPassword` to be set.";
assertion = !cfg.enableAuth || cfg.initialRootPasswordFile != null;
message = "`enableAuth` requires `initialRootPasswordFile` to be set.";
}
];
@@ -125,8 +138,6 @@ in
};
users.groups.mongodb = lib.mkIf (cfg.user == "mongodb") { };
environment.systemPackages = [ mongodb ];
systemd.services.mongodb = {
description = "MongoDB server";
@@ -164,14 +175,15 @@ in
if ! test -e "${cfg.dbpath}/.auth_setup_complete"; then
systemd-run --unit=mongodb-for-setup --uid=${cfg.user} ${mongodb}/bin/mongod --config ${mongoCnf cfg_}
# wait for mongodb
while ! ${mongodb}/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
while ! ${mongoshExe} --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done
${mongodb}/bin/mongo <<EOF
use admin
initialRootPassword=$(<${cfg.initialRootPasswordFile})
${mongoshExe} <<EOF
use admin;
db.createUser(
{
user: "root",
pwd: "${cfg.initialRootPassword}",
pwd: "$initialRootPassword",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
@@ -187,7 +199,8 @@ in
postStart = ''
if test -e "${cfg.dbpath}/.first_startup"; then
${lib.optionalString (cfg.initialScript != null) ''
${mongodb}/bin/mongo ${lib.optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}"
initialRootPassword=$(<${cfg.initialRootPasswordFile})
${mongoshExe} ${lib.optionalString (cfg.enableAuth) "-u root -p $initialRootPassword"} admin "${cfg.initialScript}"
''}
rm -f "${cfg.dbpath}/.first_startup"
fi
+5 -1
View File
@@ -622,7 +622,11 @@ in {
monado = handleTest ./monado.nix {};
monetdb = handleTest ./monetdb.nix {};
monica = handleTest ./web-apps/monica.nix {};
mongodb = handleTest ./mongodb.nix {};
mongodb = runTest ./mongodb.nix;
mongodb-ce = runTest ({ config, ... }: {
imports = [ ./mongodb.nix ];
defaults.services.mongodb.package = config.node.pkgs.mongodb-ce;
});
moodle = handleTest ./moodle.nix {};
moonraker = handleTest ./moonraker.nix {};
mopidy = handleTest ./mopidy.nix {};
+33 -55
View File
@@ -1,59 +1,37 @@
# This test start mongodb, runs a query using mongo shell
# This test starts mongodb and runs a query using mongo shell
{ config, lib, ... }:
let
# required for test execution on darwin
pkgs = config.node.pkgs;
testQuery = pkgs.writeScript "nixtest.js" ''
db.greetings.insertOne({ "greeting": "hello" });
print(db.greetings.findOne().greeting);
'';
mongoshExe = lib.getExe pkgs.mongosh;
in
{
name = "mongodb";
meta.maintainers = with pkgs.lib.maintainers; [
bluescreen303
offline
phile314
niklaskorz
];
import ./make-test-python.nix (
{ pkgs, ... }:
let
testQuery = pkgs.writeScript "nixtest.js" ''
db.greetings.insert({ "greeting": "hello" });
print(db.greetings.findOne().greeting);
'';
nodes.mongodb = {
services.mongodb.enable = true;
};
runMongoDBTest = pkg: ''
node.execute("(rm -rf data || true) && mkdir data")
node.execute(
"${pkg}/bin/mongod --fork --logpath logs --dbpath data"
)
node.wait_for_open_port(27017)
testScript = ''
start_all()
assert "hello" in node.succeed(
"${pkg}/bin/mongo ${testQuery}"
)
with subtest("start mongodb"):
mongodb.wait_for_unit("mongodb.service")
mongodb.wait_for_open_port(27017)
node.execute(
"${pkg}/bin/mongod --shutdown --dbpath data"
)
node.wait_for_closed_port(27017)
'';
in
{
name = "mongodb";
meta = with pkgs.lib.maintainers; {
maintainers = [
bluescreen303
offline
phile314
];
};
nodes = {
node =
{ ... }:
{
environment.systemPackages = with pkgs; [
# remember to update mongodb.passthru.tests if you change this
mongodb-7_0
];
};
};
testScript =
''
node.start()
''
+ runMongoDBTest pkgs.mongodb-7_0
+ ''
node.shutdown()
'';
}
)
with subtest("insert and find a document"):
result = mongodb.succeed("${mongoshExe} ${testQuery}")
print("Test output:", result)
assert result.strip() == "hello"
'';
}
+9 -5
View File
@@ -5,13 +5,13 @@
autoPatchelfHook,
curl,
openssl,
testers,
mongodb-ce,
versionCheckHook,
writeShellApplication,
jq,
nix-update,
gitMinimal,
pup,
nixosTests,
}:
let
@@ -63,6 +63,11 @@ stdenv.mkDerivation (finalAttrs: {
runHook postInstall
'';
nativeInstallCheckInputs = [ versionCheckHook ];
versionCheckProgram = "${placeholder "out"}/bin/mongod";
versionCheckProgramArg = [ "--version" ];
doInstallCheck = true;
passthru = {
updateScript =
@@ -102,9 +107,8 @@ stdenv.mkDerivation (finalAttrs: {
command = lib.getExe script;
};
tests.version = testers.testVersion {
package = mongodb-ce;
command = "mongod --version";
tests = {
inherit (nixosTests) mongodb-ce;
};
};