1 { pkgs, config, lib, ... }:
3 cfg = config.myServices.databasesReplication.openldap;
4 eldiron_schemas = pkgs.callPackage ./openldap/eldiron_schemas.nix {};
5 ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" ''
7 pidfile /run/slapd_${name}/slapd.pid
8 argsfile /run/slapd_${name}/slapd.args
15 rootdn "cn=root,${hcfg.base}"
16 directory ${cfg.base}/${name}/openldap
22 include ${config.secrets.location}/openldap_replication/${name}/replication_config
26 options.myServices.databasesReplication.openldap = {
27 enable = lib.mkEnableOption "Enable openldap replication";
29 type = lib.types.path;
31 Base path to put the replications
34 hosts = lib.mkOption {
39 type = lib.types.attrsOf (lib.types.submodule {
41 package = lib.mkOption {
42 type = lib.types.package;
43 default = pkgs.openldap;
45 Openldap package for this host
66 password = lib.mkOption {
77 config = lib.mkIf cfg.enable {
78 users.users.openldap = {
79 description = "Openldap database user";
81 uid = config.ids.uids.openldap;
82 extraGroups = [ "keys" ];
84 users.groups.openldap.gid = config.ids.gids.openldap;
86 secrets.keys = lib.flatten (lib.mapAttrsToList (name: hcfg: [
88 dest = "openldap_replication/${name}/replication_config";
95 type=refreshAndPersist
96 searchbase="${hcfg.base}"
102 credentials="${hcfg.password}"
106 dest = "openldap_replication/${name}/replication_password";
109 permissions = "0400";
110 text = hcfg.password;
116 systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
118 dataDir = "${cfg.base}/${name}/openldap";
119 backupDir = "${cfg.base}/${name}/openldap_backup";
120 backup_script = pkgs.writeScript "backup_openldap_${name}" ''
121 #!${pkgs.stdenv.shell}
123 ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iseconds).ldif
125 u = pkgs.callPackage ./utils.nix {};
126 cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir);
128 "0 22,4,10,16 * * * root ${backup_script}"
129 "0 3 * * * root ${cleanup_script}"
133 system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
134 lib.attrsets.nameValuePair "openldap_replication_${name}" {
135 deps = [ "users" "groups" ];
137 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap
138 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup
142 systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
144 dataDir = "${cfg.base}/${name}/openldap";
146 lib.attrsets.nameValuePair "openldap_backup_${name}" {
147 description = "Openldap replication for ${name}";
148 wantedBy = [ "multi-user.target" ];
149 after = [ "network.target" ];
150 unitConfig.RequiresMountsFor = dataDir;
153 mkdir -p /run/slapd_${name}
154 chown -R "openldap:openldap" /run/slapd_${name}
158 ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}";