From 04ab27d16c26ac6f934cb24e8703f025e8b97bb4 Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:52:22 +0000 Subject: [PATCH] immich: add photos-local.masu.rs DNS record and Caddy route --- docs/CHANGELOG.md | 6 ++ flake.nix | 1 + .../services/cloudflare/cloudflare.nix | 79 ++++++++++++++++++- .../nmasur/presets/services/immich/immich.nix | 15 +++- 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a487127e..e8261819 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -341,3 +341,9 @@ was auto-closed. The volume indicator is not essential; the preset and package are left in place (`pkgs/applications/misc/volnoti`) if it is 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`. + diff --git a/flake.nix b/flake.nix index 84698435..3cbba87e 100644 --- a/flake.nix +++ b/flake.nix @@ -164,6 +164,7 @@ notifications = "ntfy.${baseName}"; paperless = "paper.${baseName}"; photos = "photos.${baseName}"; + photosLocal = "photos-local.${baseName}"; postgresql = "pg.${baseName}"; prometheus = "prom.${baseName}"; recipes = "cooking.${baseName}"; diff --git a/platforms/nixos/modules/nmasur/presets/services/cloudflare/cloudflare.nix b/platforms/nixos/modules/nmasur/presets/services/cloudflare/cloudflare.nix index 40ceb7c0..35dbb507 100644 --- a/platforms/nixos/modules/nmasur/presets/services/cloudflare/cloudflare.nix +++ b/platforms/nixos/modules/nmasur/presets/services/cloudflare/cloudflare.nix @@ -58,6 +58,12 @@ in description = "Domains to use for dyndns without CDN proxying."; 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 { @@ -123,7 +129,8 @@ in after = [ "cloudflare-api-prefixed-secret.service" "letsencrypt-key-secret.service" - ]; + ] + ++ lib.optional config.services.bind.enable "bind.service"; requires = [ "cloudflare-api-prefixed-secret.service" "letsencrypt-key-secret.service" @@ -173,6 +180,76 @@ in # Enable the home-made service that we created for non-proxied records 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 secrets.cloudflare-dns-api-prefixed = { source = ./cloudflare-api.age; diff --git a/platforms/nixos/modules/nmasur/presets/services/immich/immich.nix b/platforms/nixos/modules/nmasur/presets/services/immich/immich.nix index 358bb928..c274dfdb 100644 --- a/platforms/nixos/modules/nmasur/presets/services/immich/immich.nix +++ b/platforms/nixos/modules/nmasur/presets/services/immich/immich.nix @@ -57,7 +57,14 @@ in nmasur.presets.services.caddy.routes = [ { - match = [ { host = [ hostnames.photos ]; } ]; + match = [ + { + host = [ + hostnames.photos + hostnames.photosLocal + ]; + } + ]; handle = [ { handler = "reverse_proxy"; @@ -69,9 +76,13 @@ in # Configure Cloudflare DNS to point to this machine services.cloudflare-dyndns.domains = [ hostnames.photos ]; + nmasur.presets.services.cloudflare.dnsRecords.${hostnames.photosLocal} = "192.168.1.218"; # 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 services.restic.backups.default.paths = [ "/data/images" ];