1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-21 17:01:10 +03:00

nixos/services.mongodb: remove with lib;

This commit is contained in:
Felix Buehler 2024-12-08 13:18:24 +01:00
parent 035c17d408
commit 36828aceef

View file

@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.mongodb; cfg = config.services.mongodb;
@ -11,11 +8,11 @@ let
mongoCnf = cfg: pkgs.writeText "mongodb.conf" mongoCnf = cfg: pkgs.writeText "mongodb.conf"
'' ''
net.bindIp: ${cfg.bind_ip} net.bindIp: ${cfg.bind_ip}
${optionalString cfg.quiet "systemLog.quiet: true"} ${lib.optionalString cfg.quiet "systemLog.quiet: true"}
systemLog.destination: syslog systemLog.destination: syslog
storage.dbPath: ${cfg.dbpath} storage.dbPath: ${cfg.dbpath}
${optionalString cfg.enableAuth "security.authorization: enabled"} ${lib.optionalString cfg.enableAuth "security.authorization: enabled"}
${optionalString (cfg.replSetName != "") "replication.replSetName: ${cfg.replSetName}"} ${lib.optionalString (cfg.replSetName != "") "replication.replSetName: ${cfg.replSetName}"}
${cfg.extraConfig} ${cfg.extraConfig}
''; '';
@ -29,54 +26,54 @@ in
services.mongodb = { services.mongodb = {
enable = mkEnableOption "the MongoDB server"; enable = lib.mkEnableOption "the MongoDB server";
package = mkPackageOption pkgs "mongodb" { }; package = lib.mkPackageOption pkgs "mongodb" { };
user = mkOption { user = lib.mkOption {
type = types.str; type = lib.types.str;
default = "mongodb"; default = "mongodb";
description = "User account under which MongoDB runs"; description = "User account under which MongoDB runs";
}; };
bind_ip = mkOption { bind_ip = lib.mkOption {
type = types.str; type = lib.types.str;
default = "127.0.0.1"; default = "127.0.0.1";
description = "IP to bind to"; description = "IP to bind to";
}; };
quiet = mkOption { quiet = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "quieter output"; description = "quieter output";
}; };
enableAuth = mkOption { enableAuth = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = "Enable client authentication. Creates a default superuser with username root!"; description = "Enable client authentication. Creates a default superuser with username root!";
}; };
initialRootPassword = mkOption { initialRootPassword = lib.mkOption {
type = types.nullOr types.str; type = lib.types.nullOr lib.types.str;
default = null; default = null;
description = "Password for the root user if auth is enabled."; description = "Password for the root user if auth is enabled.";
}; };
dbpath = mkOption { dbpath = lib.mkOption {
type = types.str; type = lib.types.str;
default = "/var/db/mongodb"; default = "/var/db/mongodb";
description = "Location where MongoDB stores its files"; description = "Location where MongoDB stores its files";
}; };
pidFile = mkOption { pidFile = lib.mkOption {
type = types.str; type = lib.types.str;
default = "/run/mongodb.pid"; default = "/run/mongodb.pid";
description = "Location of MongoDB pid file"; description = "Location of MongoDB pid file";
}; };
replSetName = mkOption { replSetName = lib.mkOption {
type = types.str; type = lib.types.str;
default = ""; default = "";
description = '' description = ''
If this instance is part of a replica set, set its name here. If this instance is part of a replica set, set its name here.
@ -84,8 +81,8 @@ in
''; '';
}; };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = types.lines; type = lib.types.lines;
default = ""; default = "";
example = '' example = ''
storage.journal.enabled: false storage.journal.enabled: false
@ -93,8 +90,8 @@ in
description = "MongoDB extra configuration in YAML format"; description = "MongoDB extra configuration in YAML format";
}; };
initialScript = mkOption { initialScript = lib.mkOption {
type = types.nullOr types.path; type = lib.types.nullOr lib.types.path;
default = null; default = null;
description = '' description = ''
A file containing MongoDB statements to execute on first startup. A file containing MongoDB statements to execute on first startup.
@ -107,20 +104,20 @@ in
###### implementation ###### implementation
config = mkIf config.services.mongodb.enable { config = lib.mkIf config.services.mongodb.enable {
assertions = [ assertions = [
{ assertion = !cfg.enableAuth || cfg.initialRootPassword != null; { assertion = !cfg.enableAuth || cfg.initialRootPassword != null;
message = "`enableAuth` requires `initialRootPassword` to be set."; message = "`enableAuth` requires `initialRootPassword` to be set.";
} }
]; ];
users.users.mongodb = mkIf (cfg.user == "mongodb") users.users.mongodb = lib.mkIf (cfg.user == "mongodb")
{ name = "mongodb"; { name = "mongodb";
isSystemUser = true; isSystemUser = true;
group = "mongodb"; group = "mongodb";
description = "MongoDB server user"; description = "MongoDB server user";
}; };
users.groups.mongodb = mkIf (cfg.user == "mongodb") {}; users.groups.mongodb = lib.mkIf (cfg.user == "mongodb") {};
environment.systemPackages = [ mongodb ]; environment.systemPackages = [ mongodb ];
@ -177,8 +174,8 @@ in
''; '';
postStart = '' postStart = ''
if test -e "${cfg.dbpath}/.first_startup"; then if test -e "${cfg.dbpath}/.first_startup"; then
${optionalString (cfg.initialScript != null) '' ${lib.optionalString (cfg.initialScript != null) ''
${mongodb}/bin/mongo ${optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}" ${mongodb}/bin/mongo ${lib.optionalString (cfg.enableAuth) "-u root -p ${cfg.initialRootPassword}"} admin "${cfg.initialScript}"
''} ''}
rm -f "${cfg.dbpath}/.first_startup" rm -f "${cfg.dbpath}/.first_startup"
fi fi