@@ -7,8 +7,10 @@ let
77 cfg = config.services.openldap;
88 openldap = pkgs.openldap;
99
10+ dataFile = pkgs.writeText "ldap-contents.ldif" cfg.declarativeContents;
1011 configFile = pkgs.writeText "slapd.conf" cfg.extraConfig;
11−
12+ configOpts = if cfg.configDir == null then "-f ${configFile}"
13+ else "-F ${cfg.configDir}";
1214in
1315
1416{
@@ -81,6 +83,34 @@ in
8183 '''
8284 '';
8385 };
86+
87+ declarativeContents = mkOption {
88+ type = with types; nullOr lines;
89+ default = null;
90+ description = ''
91+ Declarative contents for the LDAP database, in LDIF format.
92+
93+ Note a few facts when using it. First, the database
94+ <emphasis>must</emphasis> be stored in the directory defined by
95+ <code>dataDir</code>. Second, all <code>dataDir</code> will be erased
96+ when starting the LDAP server. Third, modifications to the database
97+ are not prevented, they are just dropped on the next reboot of the
98+ server. Finally, performance-wise the database and indexes are rebuilt
99+ on each server startup, so this will slow down server startup,
100+ especially with large databases.
101+ '';
102+ example = ''
103+ dn: dc=example,dc=org
104+ objectClass: domain
105+ dc: example
106+
107+ dn: ou=users,dc=example,dc=org
108+ objectClass = organizationalUnit
109+ ou: users
110+
111+ # ...
112+ '';
113+ };
84114 };
85115
86116 };
@@ -88,7 +118,7 @@ in
88118
89119 ###### implementation
90120
91− config = mkIf config.services.openldap.enable {
121+ config = mkIf cfg.enable {
92122
93123 environment.systemPackages = [ openldap ];
94124
@@ -98,11 +128,21 @@ in
98128 after = [ "network.target" ];
99129 preStart = ''
100130 mkdir -p /var/run/slapd
101− chown -R ${cfg.user}:${cfg.group} /var/run/slapd
102− mkdir -p ${cfg.dataDir}
103− chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
131+ chown -R "${cfg.user}:${cfg.group}" /var/run/slapd
132+ ${optionalString (cfg.declarativeContents != null) ''
133+ rm -Rf "${cfg.dataDir}"
134+ ''}
135+ mkdir -p "${cfg.dataDir}"
136+ ${optionalString (cfg.declarativeContents != null) ''
137+ ${openldap.out}/bin/slapadd ${configOpts} -l ${dataFile}
138+ ''}
139+ chown -R "${cfg.user}:${cfg.group}" "${cfg.dataDir}"
104140 '';
105− serviceConfig.ExecStart = "${openldap.out}/libexec/slapd -u ${cfg.user} -g ${cfg.group} -d 0 -h \"${concatStringsSep " " cfg.urlList}\" ${if cfg.configDir == null then "-f "+configFile else "-F "+cfg.configDir}";
141+ serviceConfig.ExecStart =
142+ "${openldap.out}/libexec/slapd -d 0 " +
143+ "-u '${cfg.user}' -g '${cfg.group}' " +
144+ "-h '${concatStringsSep " " cfg.urlList}' " +
145+ "${configOpts}";
106146 };
107147
108148 users.extraUsers.openldap =