immich: add photos-local.masu.rs DNS record and Caddy route

This commit is contained in:
Noah Masur committed 2026-11-16 01:39:23 +00:00
1 parent 23aee86f4b
commit 04ab27d16c
4 files changed
+98 -3

No files matched your search

+6
View File
@@ -341,3 +341,9 @@
was auto-closed. The volume indicator is not essential; the preset and was auto-closed. The volume indicator is not essential; the preset and
package are left in place (`pkgs/applications/misc/volnoti`) if it is package are left in place (`pkgs/applications/misc/volnoti`) if it is
re-enabled or replaced later. re-enabled or replaced later.
## 2026-11-16
- Added `photosLocal = "photos-local.masu.rs"` hostname in `flake.nix` and configured Immich (`platforms/nixos/modules/nmasur/presets/services/immich/immich.nix`) to serve both `photos.masu.rs` and `photos-local.masu.rs` through Caddy with automatic ACME DNS-01 TLS certificates.
- Added `nmasur.presets.services.cloudflare.dnsRecords` option and `cloudflare-dns-records` systemd service in `platforms/nixos/modules/nmasur/presets/services/cloudflare/cloudflare.nix` to manage static non-proxied Cloudflare A records, pointing `photos-local.masu.rs` to `192.168.1.218`.
+1
View File
@@ -164,6 +164,7 @@
notifications = "ntfy.${baseName}"; notifications = "ntfy.${baseName}";
paperless = "paper.${baseName}"; paperless = "paper.${baseName}";
photos = "photos.${baseName}"; photos = "photos.${baseName}";
photosLocal = "photos-local.${baseName}";
postgresql = "pg.${baseName}"; postgresql = "pg.${baseName}";
prometheus = "prom.${baseName}"; prometheus = "prom.${baseName}";
recipes = "cooking.${baseName}"; recipes = "cooking.${baseName}";
@@ -58,6 +58,12 @@ in
description = "Domains to use for dyndns without CDN proxying."; description = "Domains to use for dyndns without CDN proxying.";
default = [ ]; default = [ ];
}; };
dnsRecords = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
description = "Static DNS A records (mapping domain to IPv4 address) without CDN proxying.";
default = { };
};
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
@@ -123,7 +129,8 @@ in
after = [ after = [
"cloudflare-api-prefixed-secret.service" "cloudflare-api-prefixed-secret.service"
"letsencrypt-key-secret.service" "letsencrypt-key-secret.service"
]; ]
++ lib.optional config.services.bind.enable "bind.service";
requires = [ requires = [
"cloudflare-api-prefixed-secret.service" "cloudflare-api-prefixed-secret.service"
"letsencrypt-key-secret.service" "letsencrypt-key-secret.service"
@@ -173,6 +180,76 @@ in
# Enable the home-made service that we created for non-proxied records # Enable the home-made service that we created for non-proxied records
services.cloudflare-dyndns-noproxy.enable = true; services.cloudflare-dyndns-noproxy.enable = true;
# Create or update static non-proxied DNS records in Cloudflare
systemd.services.cloudflare-dns-records = lib.mkIf (cfg.dnsRecords != { }) {
description = "Cloudflare Static DNS Records";
after = [
"network-online.target"
"cloudflare-api-secret.service"
]
++ lib.optional config.services.bind.enable "bind.service";
wants = [ "network-online.target" ];
requires = [ "cloudflare-api-secret.service" ];
wantedBy = [ "multi-user.target" ];
path = [
pkgs.curl
pkgs.jq
];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
DynamicUser = true;
LoadCredential = [
"apiToken:${config.secrets.cloudflare-api.dest}"
];
};
script = ''
set -euo pipefail
CLOUDFLARE_API_TOKEN="$(tr -d '\r\n' < "''${CREDENTIALS_DIRECTORY}/apiToken")"
API_URL="https://api.cloudflare.com/client/v4"
CURL_ARGS=(-fsSL --retry 5 --retry-all-errors --retry-delay 2 --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" --header "Content-Type: application/json")
ZONES_JSON="$(curl "''${CURL_ARGS[@]}" "$API_URL/zones")"
${lib.concatStringsSep "\n" (
lib.mapAttrsToList (domain: ip: ''
ZONE_ID="$(echo "$ZONES_JSON" | jq -r --arg domain ${lib.escapeShellArg domain} '
.result[] | .name as $zone | select($domain == $zone or ($domain | endswith("." + $zone))) | .id
' | head -n1)"
if [ -z "$ZONE_ID" ]; then
echo "Error: Could not find Cloudflare zone for ${domain}" >&2
exit 1
fi
RECORDS_JSON="$(curl "''${CURL_ARGS[@]}" "$API_URL/zones/$ZONE_ID/dns_records?type=A&name=${domain}")"
RECORD_ID="$(echo "$RECORDS_JSON" | jq -r '.result[0].id // empty')"
CURRENT_IP="$(echo "$RECORDS_JSON" | jq -r '.result[0].content // empty')"
CURRENT_PROXIED="$(echo "$RECORDS_JSON" | jq -r '.result[0].proxied | tostring')"
PAYLOAD="$(jq -nc \
--arg name ${lib.escapeShellArg domain} \
--arg content ${lib.escapeShellArg ip} \
'{type: "A", name: $name, content: $content, ttl: 1, proxied: false}')"
if [ -z "$RECORD_ID" ]; then
echo "Creating Cloudflare A record for ${domain} -> ${ip}"
curl "''${CURL_ARGS[@]}" -X POST --data "$PAYLOAD" "$API_URL/zones/$ZONE_ID/dns_records" >/dev/null
elif [ "$CURRENT_IP" != ${lib.escapeShellArg ip} ] || [ "$CURRENT_PROXIED" != "false" ]; then
echo "Updating Cloudflare A record for ${domain} -> ${ip}"
curl "''${CURL_ARGS[@]}" -X PUT --data "$PAYLOAD" "$API_URL/zones/$ZONE_ID/dns_records/$RECORD_ID" >/dev/null
else
echo "Cloudflare A record for ${domain} is already up to date (${ip})"
fi
'') cfg.dnsRecords
)}
'';
};
# Create certs when not using proxy # Create certs when not using proxy
secrets.cloudflare-dns-api-prefixed = { secrets.cloudflare-dns-api-prefixed = {
source = ./cloudflare-api.age; source = ./cloudflare-api.age;
@@ -57,7 +57,14 @@ in
nmasur.presets.services.caddy.routes = [ nmasur.presets.services.caddy.routes = [
{ {
match = [ { host = [ hostnames.photos ]; } ]; match = [
{
host = [
hostnames.photos
hostnames.photosLocal
];
}
];
handle = [ handle = [
{ {
handler = "reverse_proxy"; handler = "reverse_proxy";
@@ -69,9 +76,13 @@ in
# Configure Cloudflare DNS to point to this machine # Configure Cloudflare DNS to point to this machine
services.cloudflare-dyndns.domains = [ hostnames.photos ]; services.cloudflare-dyndns.domains = [ hostnames.photos ];
nmasur.presets.services.cloudflare.dnsRecords.${hostnames.photosLocal} = "192.168.1.218";
# Point localhost to the local domain # Point localhost to the local domain
networking.hosts."127.0.0.1" = [ hostnames.photos ]; networking.hosts."127.0.0.1" = [
hostnames.photos
hostnames.photosLocal
];
# Backups # Backups
services.restic.backups.default.paths = [ "/data/images" ]; services.restic.backups.default.paths = [ "/data/images" ];