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