#!/usr/bin/env nu # Derived variables for docker-mailserver bundle rendering. # Receives component record as JSON argument, outputs extra vars as JSON. def render_mx_endpoints [domain: string, mx: list]: nothing -> string { $mx | each {|m| [ $" - dnsName: ($domain)", $" recordType: MX", $" targets: [\"($m.priority) ($m.value)\"]", $" recordTTL: 300", ] | str join "\n" } | str join "\n" } def render_dmarc_value [d: record]: nothing -> string { let rua_part = ($d.rua? | default "" | if ($in | is-not-empty) { $"rua=($in)" } else { null }) let ruf_part = ($d.ruf? | default "" | if ($in | is-not-empty) { $"ruf=($in)" } else { null }) let parts = [ "v=DMARC1", $"p=($d.policy)", $rua_part, $ruf_part, $"adkim=($d.adkim? | default 's')", $"aspf=($d.aspf? | default 's')", $"pct=($d.pct? | default 100)", ] | compact $parts | str join "; " } def render_autoconfig_endpoint [domain: string, target?: string]: nothing -> string { if ($target | default "" | is-empty) { return "" } [ $" - dnsName: autoconfig.($domain)", $" recordType: CNAME", $" targets: [\"($target)\"]", $" recordTTL: 300", ] | str join "\n" } def main [component_json: string]: nothing -> nothing { let d = ($component_json | from json) let relay_host = ($d | get -o relay | default {} | get -o host | default "") let relay_port = ($d | get -o relay | default {} | get -o port | default 587) let dns = ($d.dns_records? | default null) let dns_endpoints_block = if ($dns | is-not-empty) { [ (render_mx_endpoints $dns.domain $dns.mx), $" - dnsName: ($dns.domain)", $" recordType: TXT", $" targets: [\"($dns.spf)\"]", $" recordTTL: 300", $" - dnsName: _dmarc.($dns.domain)", $" recordType: TXT", $" targets: [\"(render_dmarc_value $dns.dmarc)\"]", $" recordTTL: 300", (render_autoconfig_endpoint $dns.domain $dns.autoconfig?), ] | where {|s| ($s | str trim | is-not-empty) } | str join "\n" } else { "" } { relay_host_postfix: (if ($relay_host | is-not-empty) { $"[($relay_host)]:($relay_port)" } else { "" }), tls_cert_path: "/etc/ssl/mail/tls.crt", tls_key_path: "/etc/ssl/mail/tls.key", tls_mount_path: "/etc/ssl/mail", dns_domain: (if ($dns | is-not-empty) { $dns.domain? | default "" } else { "" }), dns_dkim_selector: (if ($dns | is-not-empty) { $dns.dkim_selector? | default "mail" } else { "mail" }), dns_endpoints_block: $dns_endpoints_block, } | to json --raw | print }