dovecot: remove month folder in list.sieve
[sourcephile-nix.git] / nixos / modules / services / databases / openldap.nix
index b42fac33a942c007ef5300a124d15f9f94d57e0d..1ae5eea592bbbf98e43fc46707ed09d31e2d945b 100644 (file)
@@ -1,15 +1,16 @@
 { pkgs, lib, config, ... }:
-let inherit (builtins) baseNameOf readFile;
-    inherit (lib) types;
-    inherit (pkgs.lib) unlinesAttrs;
-    inherit (config.services) openldap;
-    inherit (config.users) ldap;
+let
+  inherit (builtins) baseNameOf readFile;
+  inherit (lib) types;
+  inherit (pkgs.lib) unlinesAttrs;
+  inherit (config.services) openldap;
+  inherit (config.users) ldap;
 in
 {
 options = {
-services.openldap.initConfig = lib.mkOption {
+services.openldap.cnConfig = lib.mkOption {
   type = types.lines;
-  description = "The databases' initial config in LDIF.";
+  description = "The cn=config in LDIF";
   apply = lines: pkgs.writeText "cn=config.ldif"
     (lines + "\n" + unlinesAttrs (olcSuffix: {conf, olcDbDirectory, ...}:
       "include: file://" + pkgs.writeText "config.ldif" (conf + ''
@@ -17,14 +18,11 @@ services.openldap.initConfig = lib.mkOption {
         olcDbDirectory: ${olcDbDirectory}
       '')
     ) openldap.databases);
-  # sudo ldapsearch -LLL -H ldapi:// -D cn=admin,cn=config -Y EXTERNAL -b "" -s base supportedControl
   default = ''
     dn: cn=config
     objectClass: olcGlobal
     olcLogLevel: none
-    # The tool-threads parameter sets the actual amount of CPU's
-    # that is used for indexing.
-    olcToolThreads: ${config.nix.maxJobs}
+    olcToolThreads: 1
 
     dn: cn={0}module,cn=config
     objectClass: olcModuleList
@@ -33,15 +31,9 @@ services.openldap.initConfig = lib.mkOption {
     olcModuleLoad: pw-pbkdf2
     olcModuleLoad: back_mdb
 
-    # The first database is the special frontend database
-    # whose settings are applied globally to all the other databases.
-    # Beware that cn={0}module,cn=config must appear before
-    # for enabling password schemes provided by the modules in olcPasswordHash.
-    # ldapsearch -LLL -H ldapi:// -D cn=admin,cn=config -Y EXTERNAL -b 'olcDatabase={-1}frontend,cn=config' -s sub '*'
     dn: olcDatabase={-1}frontend,cn=config
     objectClass: olcDatabaseConfig
     objectClass: olcFrontendConfig
-    # The maximum number of entries that is returned for a search operation
     olcSizeLimit: 500
     # Allow unlimited access to local connection from the local root user
     olcAccess: to *
@@ -71,7 +63,6 @@ services.openldap.initConfig = lib.mkOption {
     include: file://${pkgs.openldap}/etc/schema/cosine.ldif
     include: file://${pkgs.openldap}/etc/schema/nis.ldif
     include: file://${pkgs.openldap}/etc/schema/inetorgperson.ldif
-    include: file://${openldap/schema/postfix-book.ldif}
   '';
 };
 services.openldap.databases = lib.mkOption {
@@ -83,7 +74,7 @@ services.openldap.databases = lib.mkOption {
         description = "The database's config in LDIF.";
       };
       data = lib.mkOption {
-        type = types.lines;
+        type = types.nullOr types.lines;
         description = "The database's data in LDIF.";
       };
       olcDbDirectory = lib.mkOption {
@@ -91,53 +82,46 @@ services.openldap.databases = lib.mkOption {
         description = "The directory where the database is stored.";
         default = "${openldap.dataDir}/${name}";
       };
-      resetData = lib.mkOption {
-        type = types.bool;
-        description = "Whether to reset the data at each start of the slapd service.";
-        default = false;
-      };
     };
   }));
 };
 };
 config = lib.mkIf openldap.enable {
-systemd.services.openldap.preStart = ''
+systemd.services.openldap.preStart =
+  # olcDbDirectory must be created before adding the config.
+  ''
   set -e
-  # NOTE: slapd's config is always re-initialized.
-  rm -rf "${openldap.configDir}"/cn=config \
-         "${openldap.configDir}"/cn=config.ldif
   install -D -d -m 0700 -o "${openldap.user}" -g "${openldap.group}" "${openldap.configDir}"
-  # NOTE: olcDbDirectory must be created before adding the config.
   '' +
-  unlinesAttrs (olcSuffix: {data, olcDbDirectory, resetData, ...}:
-    lib.optionalString resetData ''
-      rm -rf "${olcDbDirectory}"
-    '' + ''
+  unlinesAttrs (olcSuffix: {data, olcDbDirectory, ...}: lib.optionalString (data != null) ''
+    rm -rf "${olcDbDirectory}"
     install -D -d -m 0700 -o "${openldap.user}" -g "${openldap.group}" "${olcDbDirectory}"
-    '') openldap.databases
-  + ''
-  # NOTE: slapd is supposed to have been stopped by systemd
+  '') openldap.databases
+  # slapd is supposed to have been stopped by systemd
   # before entering this preStart,
   # hence slap* commands can safely be used.
   #
-  # NOTE: slapadd(8):
+  # slapadd(8):
   # To populate the config database slapd-config(5),
   # use -n 0 as it is always the first database.
   # It must physically exist on the filesystem prior to this, however.
+  + ''
   umask 0077
+  rm -rf "${openldap.configDir}"/cn=config \
+         "${openldap.configDir}"/cn=config.ldif
   ${pkgs.openldap}/bin/slapadd -n 0 \
    -F "${openldap.configDir}" \
-   -l ${openldap.initConfig}
+   -l ${openldap.cnConfig}
   chown -R "${openldap.user}:${openldap.group}" "${openldap.configDir}"
-'' +
-unlinesAttrs (olcSuffix: {data, olcDbDirectory, resetData, ...}:
-  lib.optionalString resetData ''
+  '' +
+  unlinesAttrs (olcSuffix: {data, olcDbDirectory, ...}: lib.optionalString (data != null) ''
     ${pkgs.openldap}/bin/slapadd \
      -F "${openldap.configDir}" \
+     -b ${olcSuffix} \
      -l ${pkgs.writeText "data.ldif" data}
-  '' + ''
-  test ! -e "${olcDbDirectory}" ||
-  chown -R "${openldap.user}:${openldap.group}" "${olcDbDirectory}"
-'') openldap.databases;
+    '' + ''
+    test ! -e "${olcDbDirectory}" ||
+    chown -R "${openldap.user}:${openldap.group}" "${olcDbDirectory}"
+  '') openldap.databases;
 };
 }