git.iko.soy/typeable/nixpkgs
on master help sign in
openldap module: allow defining contents declaratively
commitaeef7c91f14bfbf0db7527acd36fc550cd37ea65 authorLéo Gaspard <leo@gaspard.io> dateSat 3 Mar 2018, 15:33 (9 years ago) parentcca9ec2
1 file changed+35
nixos/modules/services/databases/openldap.nix+35
nixos/modules/services/databases/openldap.nix+35
@@ -7,6 +7,7 @@ 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;
1112 configOpts = if cfg.configDir == null then "-f ${configFile}"
1213 else "-F ${cfg.configDir}";
@@ -82,6 +83,34 @@ in
8283 '''
8384 '';
8485 };
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+ };
85114 };
86115
87116 };
@@ -100,7 +129,13 @@ in
100129 preStart = ''
101130 mkdir -p /var/run/slapd
102131 chown -R "${cfg.user}:${cfg.group}" /var/run/slapd
132+ ${optionalString (cfg.declarativeContents != null) ''
133+ rm -Rf "${cfg.dataDir}"
134+ ''}
103135 mkdir -p "${cfg.dataDir}"
136+ ${optionalString (cfg.declarativeContents != null) ''
137+ ${openldap.out}/bin/slapadd ${configOpts} -l ${dataFile}
138+ ''}
104139 chown -R "${cfg.user}:${cfg.group}" "${cfg.dataDir}"
105140 '';
106141 serviceConfig.ExecStart =