1 { pkgs, config, lib, ... }:
3 cfg = config.myServices.databasesReplication.openldap;
4 ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" ''
5 include ${pkgs.openldap}/etc/schema/core.schema
6 include ${pkgs.openldap}/etc/schema/cosine.schema
7 include ${pkgs.openldap}/etc/schema/inetorgperson.schema
8 include ${pkgs.openldap}/etc/schema/nis.schema
9 include ${./openldap/immae.schema}
10 pidfile /run/slapd_${name}/slapd.pid
11 argsfile /run/slapd_${name}/slapd.args
18 rootdn "cn=root,${hcfg.base}"
19 directory ${cfg.base}/${name}/openldap
25 include ${config.secrets.fullPaths."openldap_replication/${name}/replication_config"}
29 options.myServices.databasesReplication.openldap = {
30 enable = lib.mkEnableOption "Enable openldap replication";
32 type = lib.types.path;
34 Base path to put the replications
37 hosts = lib.mkOption {
42 type = lib.types.attrsOf (lib.types.submodule {
44 package = lib.mkOption {
45 type = lib.types.package;
46 default = pkgs.openldap;
48 Openldap package for this host
69 password = lib.mkOption {
80 config = lib.mkIf cfg.enable {
81 users.users.openldap = {
82 description = "Openldap database user";
84 uid = config.ids.uids.openldap;
85 extraGroups = [ "keys" ];
87 users.groups.openldap.gid = config.ids.gids.openldap;
89 secrets.keys = lib.listToAttrs (lib.flatten (lib.mapAttrsToList (name: hcfg: [
90 (lib.nameValuePair "openldap_replication/${name}/replication_config" {
97 type=refreshAndPersist
98 searchbase="${hcfg.base}"
104 credentials="${hcfg.password}"
107 (lib.nameValuePair "openldap_replication/${name}/replication_password" {
110 permissions = "0400";
111 text = hcfg.password;
117 systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
119 dataDir = "${cfg.base}/${name}/openldap";
120 backupDir = "${cfg.base}/${name}/openldap_backup";
121 backup_script = pkgs.writeScript "backup_openldap_${name}" ''
122 #!${pkgs.stdenv.shell}
124 ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).ldif
126 u = pkgs.callPackage ./utils.nix {};
127 cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir);
129 "0 22,4,10,16 * * * root ${backup_script}"
130 "0 3 * * * root ${cleanup_script}"
134 system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
135 lib.attrsets.nameValuePair "openldap_replication_${name}" {
136 deps = [ "users" "groups" ];
138 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap
139 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup
143 systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
145 dataDir = "${cfg.base}/${name}/openldap";
147 lib.attrsets.nameValuePair "openldap_backup_${name}" {
148 description = "Openldap replication for ${name}";
149 wantedBy = [ "multi-user.target" ];
150 after = [ "network.target" ];
151 unitConfig.RequiresMountsFor = dataDir;
154 mkdir -p /run/slapd_${name}
155 chown -R "openldap:openldap" /run/slapd_${name}
159 ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}";