diff --git a/cluster/services/dns/authoritative.nix b/cluster/services/dns/authoritative.nix index 5d7e446..ea6403f 100644 --- a/cluster/services/dns/authoritative.nix +++ b/cluster/services/dns/authoritative.nix @@ -17,9 +17,11 @@ let configList = lib.mapAttrsToList (n: v: "${n}=${v}") cfg; in lib.concatStringsSep "\n" configList; - rewriteRecords = lib.filterAttrs (_: record: record.rewriteTarget != null) cluster.config.dns.records; + rewriteRecords = lib.filterAttrs (_: record: record.rewrite.target != null) cluster.config.dns.records; - rewrites = lib.mapAttrsToList (_: record: "rewrite stop name exact ${record.name}.${record.root}. ${record.rewriteTarget}.") rewriteRecords; + rewrites = lib.mapAttrsToList (_: record: let + maybeEscapeRegex = str: if record.write.type == "regex" then "^${lib.escapeRegex str}$" else str; + in "rewrite stop name ${record.rewrite.type} ${record.name}${maybeEscapeRegex ".${record.root}."} ${record.rewrite.target}. answer auto") rewriteRecords; rewriteConf = pkgs.writeText "coredns-rewrites.conf" (lib.concatStringsSep "\n" rewrites); in { diff --git a/cluster/services/dns/options.nix b/cluster/services/dns/options.nix index 1b1d040..597db7d 100644 --- a/cluster/services/dns/options.nix +++ b/cluster/services/dns/options.nix @@ -34,13 +34,19 @@ let type = with types; nullOr str; default = null; }; - rewriteTarget = mkOption { - type = with types; nullOr str; - default = null; + rewrite = { + target = mkOption { + type = with types; nullOr str; + default = null; + }; + type = mkOption { + type = types.enum [ "exact" "substring" "prefix" "suffix" "regex" ]; + default = "exact"; + }; }; }; config = { - rewriteTarget = mkIf (config.consulService != null) "${config.consulService}.${config.consulServicesRoot}"; + rewrite.target = mkIf (config.consulService != null) "${config.consulService}.${config.consulServicesRoot}"; }; }); in