1 { pkgs, lib, config, ... }:
2 let inherit (builtins) baseNameOf readFile;
4 inherit (pkgs.lib) unlinesAttrs;
5 inherit (config.services) openldap;
6 inherit (config.users) ldap;
8 copyFile = file: pkgs.writeText (baseNameOf file) (readFile file);
12 services.openldap.domainSuffix = lib.mkOption {
14 default = "dc=${lib.concatStringsSep ",dc=" (lib.splitString "." config.networking.domain)}";
15 description = ''LDAP suffix for config.networking.domain.'';
17 services.openldap.initConfig = lib.mkOption {
19 description = "The databases' initial config in LDIF.";
20 apply = lines: pkgs.writeText "cn=config.ldif.nix"
21 (lines + "\n" + unlinesAttrs (olcSuffix: {conf, olcDbDirectory, ...}:
22 "include: file://" + pkgs.writeText "config.ldif" (conf + ''
23 olcSuffix: ${olcSuffix}
24 olcDbDirectory: ${olcDbDirectory}
26 ) openldap.databases);
29 objectClass: olcGlobal
30 #olcPidFile: /run/slapd/slapd.pid
31 # List of arguments that were passed to the server
32 #olcArgsFile: /run/slapd/slapd.args
33 # Read slapd-config(5) for possible values
35 # The tool-threads parameter sets the actual amount of CPU's
36 # that is used for indexing.
39 dn: olcDatabase={-1}frontend,cn=config
40 objectClass: olcDatabaseConfig
41 objectClass: olcFrontendConfig
42 # The maximum number of entries that is returned for a search operation
44 # Allow unlimited access to local connection from the local root user
46 by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth manage
48 # Allow unauthenticated read access for schema and base DN autodiscovery
49 olcAccess: to dn.exact=""
51 olcAccess: to dn.base="cn=Subschema"
54 dn: olcDatabase=config,cn=config
55 objectClass: olcDatabaseConfig
56 olcRootDN: cn=admin,cn=config
57 # Access to cn=config, system root can be manager
58 # with SASL mechanism (-Y EXTERNAL) over unix socket (-H ldapi://)
60 by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage
63 dn: cn=schema,cn=config
64 objectClass: olcSchemaConfig
66 include: file://${pkgs.openldap}/etc/schema/core.ldif
67 include: file://${pkgs.openldap}/etc/schema/cosine.ldif
68 include: file://${pkgs.openldap}/etc/schema/nis.ldif
69 include: file://${pkgs.openldap}/etc/schema/inetorgperson.ldif
70 include: file://${copyFile openldap/schema/postfix-book.ldif}
72 dn: cn=module{0},cn=config
73 objectClass: olcModuleList
74 # Where the dynamically loaded modules are stored
75 #olcModulePath: /usr/lib/ldap
76 olcModuleLoad: back_mdb
79 services.openldap.databases = lib.mkOption {
81 type = types.attrsOf (types.submodule ({name, options, config, ...}: {
85 description = "The database's config in LDIF.";
89 description = "The database's data in LDIF.";
91 olcDbDirectory = lib.mkOption {
93 description = "The directory where the database is stored.";
94 default = "${openldap.dataDir}/${name}";
96 resetData = lib.mkOption {
98 description = "Whether to reset the data at each start of the slapd service.";
106 systemd.services.openldap = {
109 # NOTE: slapd's config is always re-initialized.
110 rm -rf "${openldap.configDir}"/cn=config \
111 "${openldap.configDir}"/cn=config.ldif
112 install -D -d -m 0700 -o "${openldap.user}" -g "${openldap.group}" "${openldap.configDir}"
113 # NOTE: olcDbDirectory must be created before adding the config.
115 unlinesAttrs (olcSuffix: {data, olcDbDirectory, resetData, ...}:
116 lib.optionalString resetData ''
117 rm -rf "${olcDbDirectory}"
119 install -D -d -m 0700 -o "${openldap.user}" -g "${openldap.group}" "${olcDbDirectory}"
120 '') openldap.databases
122 # NOTE: slapd is supposed to have been stopped by systemd
123 # before entering this preStart,
124 # hence slap* commands can safely be used.
127 # To populate the config database slapd-config(5),
128 # use -n 0 as it is always the first database.
129 # It must physically exist on the filesystem prior to this, however.
131 ${pkgs.openldap}/bin/slapadd -n 0 \
132 -F "${openldap.configDir}" \
133 -l ${openldap.initConfig}
134 chown -R "${openldap.user}:${openldap.group}" "${openldap.configDir}"
136 unlinesAttrs (olcSuffix: {data, olcDbDirectory, resetData, ...}:
137 lib.optionalString resetData ''
138 ${pkgs.openldap}/bin/slapadd \
139 -F "${openldap.configDir}" \
140 -l ${pkgs.writeText "data.ldif" data}
142 test ! -e "${olcDbDirectory}" ||
143 chown -R "${openldap.user}:${openldap.group}" "${olcDbDirectory}"
144 '') openldap.databases;