Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
ci(desktop): cache macOS builds on Namespace (#7612)
Eric Hayesbcab127277ae
diff --git a/.github/workflows/build_desktop_on_tag.yml b/.github/workflows/build_desktop_on_tag.ymlindex c5edae764f..f74117879a 100644--- a/.github/workflows/build_desktop_on_tag.yml+++ b/.github/workflows/build_desktop_on_tag.yml@@ -184,7 +184,12 @@ jobs:needs:- resolve-refname: Build macOS DMG- runs-on: macos-15+ runs-on:+ - nscloud-macos-sequoia-arm64-6x14-with-cache+ - nscloud-cache-tag-macro-desktop-macos+ - nscloud-cache-size-100gb+ env:+ DESKTOP_NIX_CACHE_SIZE_GB: '100'steps:- name: Checkout Repouses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10@@ -199,6 +204,11 @@ jobs:exit 1fishell: bash+ - name: Mount macOS Nix binary cache+ uses: namespacelabs/nscloud-cache-action@15799a6b54e5765f85b2aac25b3f0df43ed571c0+ with:+ path: ~/.cache/macro-desktop-nix+ continue-on-error: true- name: Install Nixrun: |set -euo pipefail@@ -216,6 +226,40 @@ jobs:export NIX_REMOTE=daemonnix --versionshell: bash+ - id: nix-cache+ name: Configure macOS Nix binary cache+ run: |+ set -euo pipefail++ # The Apple linker/SDK live outside Nix. Do not reuse their outputs after the+ # runner image changes its macOS or Xcode version.+ toolchain_key=$(+ { xcodebuild -version && sw_vers -buildVersion; } |+ shasum -a 256 | awk '{print $1}'+ )+ cache_root="$HOME/.cache/macro-desktop-nix"+ cache_dir="$cache_root/$toolchain_key"+ mkdir -p "$cache_dir"+ cache_url="file://$cache_dir?compression=zstd&trusted=true&priority=10"++ # The mounted cache root is a symlink; the trailing slash measures its contents.+ du -sk "$cache_root/" | awk '{printf "Restored macOS Nix cache: %.2f GiB\n", $1 / 1048576}'++ # Initialize an empty cache before adding it as a substituter. Only this local,+ # repository-specific cache accepts unsigned paths; public caches still require+ # signatures. Keep the normal /nix store and its daemon-managed permissions.+ nix store info --store "$cache_url" --json+ {+ echo "DESKTOP_NIX_CACHE_ROOT=$cache_root"+ echo "DESKTOP_NIX_CACHE_DIR=$cache_dir"+ echo "DESKTOP_NIX_CACHE_URL=$cache_url"+ echo "NIX_CONFIG<<DESKTOP_NIX_CONFIG_EOF"+ printf '%s\n' "${NIX_CONFIG:-}"+ echo "extra-substituters = $cache_url"+ echo "DESKTOP_NIX_CONFIG_EOF"+ } >> "$GITHUB_ENV"+ shell: bash+ continue-on-error: true- name: Configure signing identityrun: |set -euo pipefail@@ -276,11 +320,156 @@ jobs:run: |set -euo pipefail+ # Record the derivations before building so completed dependencies can be saved+ # even if a later compilation or packaging step fails.+ nix path-info --derivation --impure \+ ".#packages.aarch64-darwin.tauri-desktop-apple-linker-smoke" \+ ".#packages.aarch64-darwin.tauri-desktop-dmg" \+ > "$RUNNER_TEMP/desktop-cache-roots"+nix build --impure --option sandbox false --print-build-logs \".#packages.aarch64-darwin.tauri-desktop-apple-linker-smoke"nix build --impure --option sandbox false --print-build-logs \".#packages.aarch64-darwin.tauri-desktop-dmg"shell: bash+ - name: Prune and save macOS Nix build dependencies+ if: '!cancelled() && steps.nix-cache.outcome == ''success'''+ run: |+ set -euo pipefail+ export LC_ALL=C++ roots_file="$RUNNER_TEMP/desktop-cache-roots"+ if [ ! -s "$roots_file" ]; then+ exit 0+ fi++ cache_root="${DESKTOP_NIX_CACHE_ROOT:?}"+ cache_dir="${DESKTOP_NIX_CACHE_DIR:?}"+ toolchain_key="${cache_dir#"$cache_root/"}"+ if [ "${#toolchain_key}" -ne 64 ] || [[ "$toolchain_key" == *[!0-9a-f]* ]] \+ || [ "$cache_dir" != "$cache_root/$toolchain_key" ] \+ || [ -L "$cache_dir" ] || [ -L "$cache_dir/nar" ]; then+ echo "Refusing to prune an unexpected macOS Nix cache directory: $cache_dir" >&2+ exit 1+ fi++ work_dir=$(mktemp -d "$RUNNER_TEMP/desktop-cache-prune.XXXXXX")+ trap 'rm -rf "$work_dir"' EXIT+ shopt -s nullglob dotglob++ report_size() {+ cache_size_kib=$(du -sk "$cache_root/" | awk '{print $1}')+ local message+ message=$(awk -v phase="$1" -v kib="$cache_size_kib" \+ 'BEGIN {printf "macOS Nix cache %s: %.2f GiB", phase, kib / 1048576}')+ echo "$message"+ if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then+ printf -- '- %s\n' "$message" >> "$GITHUB_STEP_SUMMARY"+ fi+ }+ report_size "before pruning"++ # A DMG's runtime closure omits build-only inputs such as Cargo artifacts and+ # the toolchain. Walk its derivation closure and include every existing output,+ # including successful dependencies of an otherwise failed build.+ while IFS= read -r derivation; do+ nix-store --query --requisites --include-outputs "$derivation"+ done < "$roots_file" |+ sort -u > "$work_dir/requisites"+ awk '!/\.drv$/' "$work_dir/requisites" > "$work_dir/paths"++ # Finish enumeration and validate metadata before deleting anything. An empty+ # closure or a failed query must leave the previous cache intact.+ if [ ! -s "$work_dir/paths" ]; then+ echo "No completed Nix outputs to cache; leaving the cache intact."+ exit 0+ fi+ # An early failure may leave current dependencies in the binary cache without+ # having restored them locally. Keep their planned outputs too, while exporting+ # only the realized paths above. Bound argv size for macOS's smaller limit.+ awk '/\.drv$/' "$work_dir/requisites" > "$work_dir/derivations"+ : > "$work_dir/planned-paths"+ if [ -s "$work_dir/derivations" ]; then+ xargs -n 128 nix-store --query --outputs < "$work_dir/derivations" > "$work_dir/planned-paths"+ fi+ awk -F/ '+ NF != 4 || $2 != "nix" || $3 != "store" {exit 1}+ {+ split($4, parts, "-")+ if (length(parts[1]) != 32 || parts[1] ~ /[^0123456789abcdfghijklmnpqrsvwxyz]/) exit 1+ print parts[1] ".narinfo"+ }+ ' "$work_dir/paths" "$work_dir/planned-paths" | sort -u > "$work_dir/wanted-narinfos"+ for metadata in "$cache_dir"/*.narinfo; do+ printf '%s\n' "${metadata##*/}"+ done | sort -u > "$work_dir/all-narinfos"+ comm -12 "$work_dir/all-narinfos" "$work_dir/wanted-narinfos" > "$work_dir/retained-narinfos"+ comm -23 "$work_dir/all-narinfos" "$work_dir/wanted-narinfos" > "$work_dir/remove-narinfos"+ : > "$work_dir/retained-nars"++ while IFS= read -r metadata; do+ nar_url=""+ store_path=""+ while read -r field value; do+ case "$field" in+ URL:) nar_url="$value" ;;+ StorePath:) store_path="$value" ;;+ esac+ done < "$cache_dir/$metadata"+ nar_file="${nar_url#nar/}"+ if [[ "$nar_url" != nar/* ]] || [[ "$nar_file" == *[!a-zA-Z0-9._-]* ]] \+ || [[ "$nar_file" == .* ]] || [ -z "$nar_file" ] \+ || [[ "$store_path" != /nix/store/"${metadata%.narinfo}"-* ]] \+ || [ -L "$cache_dir/$metadata" ]; then+ echo "Invalid retained Nix cache metadata: $metadata; leaving the cache intact." >&2+ exit 1+ fi+ if [ -f "$cache_dir/$nar_url" ] && [ ! -L "$cache_dir/$nar_url" ]; then+ printf '%s\n' "$nar_url" >> "$work_dir/retained-nars"+ else+ # Nix checks narinfo existence, not archive existence. Remove dangling+ # metadata so the subsequent copy repairs this path instead of skipping it.+ printf '%s\n' "$metadata" >> "$work_dir/remove-narinfos"+ fi+ done < "$work_dir/retained-narinfos"+ sort -u -o "$work_dir/retained-nars" "$work_dir/retained-nars"+ for archive in "$cache_dir"/nar/*; do+ printf 'nar/%s\n' "${archive##*/}"+ done | sort -u > "$work_dir/all-nars"+ # Different store paths may share one compressed archive. Retain the union of+ # their URLs so removing one obsolete narinfo cannot break a current path.+ comm -23 "$work_dir/all-nars" "$work_dir/retained-nars" > "$work_dir/remove-nars"++ while IFS= read -r metadata; do+ rm -f "$cache_dir/$metadata"+ done < "$work_dir/remove-narinfos"+ while IFS= read -r archive; do+ rm -f "$cache_dir/$archive"+ done < "$work_dir/remove-nars"+ for partition in "$cache_root"/*; do+ partition_key="${partition##*/}"+ if [ "${#partition_key}" -eq 64 ] && [[ "$partition_key" != *[!0-9a-f]* ]] \+ && [ "$partition" != "$cache_dir" ] && [ -d "$partition" ] && [ ! -L "$partition" ]; then+ rm -rf "$partition"+ fi+ done+ printf 'Pruned %s metadata entries and %s archives; retaining %s current store paths.\n' \+ "$(wc -l < "$work_dir/remove-narinfos" | tr -d ' ')" \+ "$(wc -l < "$work_dir/remove-nars" | tr -d ' ')" \+ "$(wc -l < "$work_dir/wanted-narinfos" | tr -d ' ')"+ report_size "after pruning"++ export_started=$SECONDS+ copy_status=0+ nix copy --stdin --to "$DESKTOP_NIX_CACHE_URL" < "$work_dir/paths" || copy_status=$?+ echo "Nix cache export took $((SECONDS - export_started)) seconds."+ report_size "after export"+ if (( cache_size_kib * 1024 >= ${DESKTOP_NIX_CACHE_SIZE_GB:-100} * 1000000000 )); then+ echo "::warning::The current Nix build closure exceeds the configured cache capacity even after pruning; Namespace may start the next build cold."+ fi+ exit "$copy_status"+ shell: bash+ continue-on-error: true- name: Collect DMG artifactrun: |set -euo pipefaildiff --git a/.github/workflows/build_dmg_on_tag.yml b/.github/workflows/build_dmg_on_tag.ymlindex 08e5322600..0d35b9456d 100644--- a/.github/workflows/build_dmg_on_tag.yml+++ b/.github/workflows/build_dmg_on_tag.yml@@ -11,7 +11,12 @@ name: Build macOS DMGjobs:build-dmg:name: Build macOS DMG- runs-on: macos-15+ runs-on:+ - nscloud-macos-sequoia-arm64-6x14-with-cache+ - nscloud-cache-tag-macro-desktop-macos+ - nscloud-cache-size-100gb+ env:+ DESKTOP_NIX_CACHE_SIZE_GB: '100'steps:- name: Checkout Repouses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10@@ -26,6 +31,11 @@ jobs:exit 1fishell: bash+ - name: Mount macOS Nix binary cache+ uses: namespacelabs/nscloud-cache-action@15799a6b54e5765f85b2aac25b3f0df43ed571c0+ with:+ path: ~/.cache/macro-desktop-nix+ continue-on-error: true- name: Install Nixrun: |set -euo pipefail@@ -43,6 +53,40 @@ jobs:export NIX_REMOTE=daemonnix --versionshell: bash+ - id: nix-cache+ name: Configure macOS Nix binary cache+ run: |+ set -euo pipefail++ # The Apple linker/SDK live outside Nix. Do not reuse their outputs after the+ # runner image changes its macOS or Xcode version.+ toolchain_key=$(+ { xcodebuild -version && sw_vers -buildVersion; } |+ shasum -a 256 | awk '{print $1}'+ )+ cache_root="$HOME/.cache/macro-desktop-nix"+ cache_dir="$cache_root/$toolchain_key"+ mkdir -p "$cache_dir"+ cache_url="file://$cache_dir?compression=zstd&trusted=true&priority=10"++ # The mounted cache root is a symlink; the trailing slash measures its contents.+ du -sk "$cache_root/" | awk '{printf "Restored macOS Nix cache: %.2f GiB\n", $1 / 1048576}'++ # Initialize an empty cache before adding it as a substituter. Only this local,+ # repository-specific cache accepts unsigned paths; public caches still require+ # signatures. Keep the normal /nix store and its daemon-managed permissions.+ nix store info --store "$cache_url" --json+ {+ echo "DESKTOP_NIX_CACHE_ROOT=$cache_root"+ echo "DESKTOP_NIX_CACHE_DIR=$cache_dir"+ echo "DESKTOP_NIX_CACHE_URL=$cache_url"+ echo "NIX_CONFIG<<DESKTOP_NIX_CONFIG_EOF"+ printf '%s\n' "${NIX_CONFIG:-}"+ echo "extra-substituters = $cache_url"+ echo "DESKTOP_NIX_CONFIG_EOF"+ } >> "$GITHUB_ENV"+ shell: bash+ continue-on-error: true- name: Configure signing identityrun: |set -euo pipefail@@ -103,11 +147,156 @@ jobs:run: |set -euo pipefail+ # Record the derivations before building so completed dependencies can be saved+ # even if a later compilation or packaging step fails.+ nix path-info --derivation --impure \+ ".#packages.aarch64-darwin.tauri-desktop-apple-linker-smoke" \+ ".#packages.aarch64-darwin.tauri-desktop-dmg" \+ > "$RUNNER_TEMP/desktop-cache-roots"+nix build --impure --option sandbox false --print-build-logs \".#packages.aarch64-darwin.tauri-desktop-apple-linker-smoke"nix build --impure --option sandbox false --print-build-logs \".#packages.aarch64-darwin.tauri-desktop-dmg"shell: bash+ - name: Prune and save macOS Nix build dependencies+ if: '!cancelled() && steps.nix-cache.outcome == ''success'''+ run: |+ set -euo pipefail+ export LC_ALL=C++ roots_file="$RUNNER_TEMP/desktop-cache-roots"+ if [ ! -s "$roots_file" ]; then+ exit 0+ fi++ cache_root="${DESKTOP_NIX_CACHE_ROOT:?}"+ cache_dir="${DESKTOP_NIX_CACHE_DIR:?}"+ toolchain_key="${cache_dir#"$cache_root/"}"+ if [ "${#toolchain_key}" -ne 64 ] || [[ "$toolchain_key" == *[!0-9a-f]* ]] \+ || [ "$cache_dir" != "$cache_root/$toolchain_key" ] \+ || [ -L "$cache_dir" ] || [ -L "$cache_dir/nar" ]; then+ echo "Refusing to prune an unexpected macOS Nix cache directory: $cache_dir" >&2+ exit 1+ fi++ work_dir=$(mktemp -d "$RUNNER_TEMP/desktop-cache-prune.XXXXXX")+ trap 'rm -rf "$work_dir"' EXIT+ shopt -s nullglob dotglob++ report_size() {+ cache_size_kib=$(du -sk "$cache_root/" | awk '{print $1}')+ local message+ message=$(awk -v phase="$1" -v kib="$cache_size_kib" \+ 'BEGIN {printf "macOS Nix cache %s: %.2f GiB", phase, kib / 1048576}')+ echo "$message"+ if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then+ printf -- '- %s\n' "$message" >> "$GITHUB_STEP_SUMMARY"+ fi+ }+ report_size "before pruning"++ # A DMG's runtime closure omits build-only inputs such as Cargo artifacts and+ # the toolchain. Walk its derivation closure and include every existing output,+ # including successful dependencies of an otherwise failed build.+ while IFS= read -r derivation; do+ nix-store --query --requisites --include-outputs "$derivation"+ done < "$roots_file" |+ sort -u > "$work_dir/requisites"+ awk '!/\.drv$/' "$work_dir/requisites" > "$work_dir/paths"++ # Finish enumeration and validate metadata before deleting anything. An empty+ # closure or a failed query must leave the previous cache intact.+ if [ ! -s "$work_dir/paths" ]; then+ echo "No completed Nix outputs to cache; leaving the cache intact."+ exit 0+ fi+ # An early failure may leave current dependencies in the binary cache without+ # having restored them locally. Keep their planned outputs too, while exporting+ # only the realized paths above. Bound argv size for macOS's smaller limit.+ awk '/\.drv$/' "$work_dir/requisites" > "$work_dir/derivations"+ : > "$work_dir/planned-paths"+ if [ -s "$work_dir/derivations" ]; then+ xargs -n 128 nix-store --query --outputs < "$work_dir/derivations" > "$work_dir/planned-paths"+ fi+ awk -F/ '+ NF != 4 || $2 != "nix" || $3 != "store" {exit 1}+ {+ split($4, parts, "-")+ if (length(parts[1]) != 32 || parts[1] ~ /[^0123456789abcdfghijklmnpqrsvwxyz]/) exit 1+ print parts[1] ".narinfo"+ }+ ' "$work_dir/paths" "$work_dir/planned-paths" | sort -u > "$work_dir/wanted-narinfos"+ for metadata in "$cache_dir"/*.narinfo; do+ printf '%s\n' "${metadata##*/}"+ done | sort -u > "$work_dir/all-narinfos"+ comm -12 "$work_dir/all-narinfos" "$work_dir/wanted-narinfos" > "$work_dir/retained-narinfos"+ comm -23 "$work_dir/all-narinfos" "$work_dir/wanted-narinfos" > "$work_dir/remove-narinfos"+ : > "$work_dir/retained-nars"++ while IFS= read -r metadata; do+ nar_url=""+ store_path=""+ while read -r field value; do+ case "$field" in+ URL:) nar_url="$value" ;;+ StorePath:) store_path="$value" ;;+ esac+ done < "$cache_dir/$metadata"+ nar_file="${nar_url#nar/}"+ if [[ "$nar_url" != nar/* ]] || [[ "$nar_file" == *[!a-zA-Z0-9._-]* ]] \+ || [[ "$nar_file" == .* ]] || [ -z "$nar_file" ] \+ || [[ "$store_path" != /nix/store/"${metadata%.narinfo}"-* ]] \+ || [ -L "$cache_dir/$metadata" ]; then+ echo "Invalid retained Nix cache metadata: $metadata; leaving the cache intact." >&2+ exit 1+ fi+ if [ -f "$cache_dir/$nar_url" ] && [ ! -L "$cache_dir/$nar_url" ]; then+ printf '%s\n' "$nar_url" >> "$work_dir/retained-nars"+ else+ # Nix checks narinfo existence, not archive existence. Remove dangling+ # metadata so the subsequent copy repairs this path instead of skipping it.+ printf '%s\n' "$metadata" >> "$work_dir/remove-narinfos"+ fi+ done < "$work_dir/retained-narinfos"+ sort -u -o "$work_dir/retained-nars" "$work_dir/retained-nars"+ for archive in "$cache_dir"/nar/*; do+ printf 'nar/%s\n' "${archive##*/}"+ done | sort -u > "$work_dir/all-nars"+ # Different store paths may share one compressed archive. Retain the union of+ # their URLs so removing one obsolete narinfo cannot break a current path.+ comm -23 "$work_dir/all-nars" "$work_dir/retained-nars" > "$work_dir/remove-nars"++ while IFS= read -r metadata; do+ rm -f "$cache_dir/$metadata"+ done < "$work_dir/remove-narinfos"+ while IFS= read -r archive; do+ rm -f "$cache_dir/$archive"+ done < "$work_dir/remove-nars"+ for partition in "$cache_root"/*; do+ partition_key="${partition##*/}"+ if [ "${#partition_key}" -eq 64 ] && [[ "$partition_key" != *[!0-9a-f]* ]] \+ && [ "$partition" != "$cache_dir" ] && [ -d "$partition" ] && [ ! -L "$partition" ]; then+ rm -rf "$partition"+ fi+ done+ printf 'Pruned %s metadata entries and %s archives; retaining %s current store paths.\n' \+ "$(wc -l < "$work_dir/remove-narinfos" | tr -d ' ')" \+ "$(wc -l < "$work_dir/remove-nars" | tr -d ' ')" \+ "$(wc -l < "$work_dir/wanted-narinfos" | tr -d ' ')"+ report_size "after pruning"++ export_started=$SECONDS+ copy_status=0+ nix copy --stdin --to "$DESKTOP_NIX_CACHE_URL" < "$work_dir/paths" || copy_status=$?+ echo "Nix cache export took $((SECONDS - export_started)) seconds."+ report_size "after export"+ if (( cache_size_kib * 1024 >= ${DESKTOP_NIX_CACHE_SIZE_GB:-100} * 1000000000 )); then+ echo "::warning::The current Nix build closure exceeds the configured cache capacity even after pruning; Namespace may start the next build cold."+ fi+ exit "$copy_status"+ shell: bash+ continue-on-error: true- name: Collect DMG artifactrun: |set -euo pipefaildiff --git a/nix/tauri-desktop.nix b/nix/tauri-desktop.nixindex 59e554e292..27b2d653b5 100644--- a/nix/tauri-desktop.nix+++ b/nix/tauri-desktop.nix@@ -173,41 +173,50 @@extraFileset = ../static_assets/schema.graphql;inherit frontend;- craneArgs.cargoVendorDir = tauriCargoVendorDir;- craneArgs.CARGO_TARGET_DIR = "apps/web/tauri/target";- # crane's common Cargo fileset only keeps the lockfile at cargoRoot.- # Restore the nested Tauri workspace lockfile where Cargo discovers it.- craneArgs.preConfigure = ''- export CARGO_TARGET_DIR="$PWD/apps/web/tauri/target"- install -Dm0644 ${../apps/web/tauri/Cargo.lock} apps/web/tauri/Cargo.lock- '';- craneArgs.postConfigure = ''- writable_vendor="$TMPDIR/cargo-vendor"- mkdir -p "$writable_vendor"- cp -aL ${tauriCargoVendorDir}/. "$writable_vendor/"- chmod -R u+w "$writable_vendor"- substituteInPlace .cargo-home/config.toml \- --replace-fail "${tauriCargoVendorDir}" "$writable_vendor"- '';- craneArgs.preBuild = ''- cp ${../apps/web/package.json} apps/web/package.json- rm -rf apps/web/dist- cp -r ${frontend} apps/web/dist-- # The dependency derivation runs from cargoRoot with an injected- # manifest path. Actual Tauri commands must run beside tauri.conf.json.- case "''${name:-}" in- *-deps-*) ;;- *) cd apps/web/tauri ;;- esac- '';+ craneArgs = {+ cargoVendorDir = tauriCargoVendorDir;+ CARGO_TARGET_DIR = "apps/web/tauri/target";+ # crane's common Cargo fileset only keeps the lockfile at cargoRoot.+ # Restore the nested Tauri workspace lockfile where Cargo discovers it.+ preConfigure = ''+ export CARGO_TARGET_DIR="$PWD/apps/web/tauri/target"+ install -Dm0644 ${../apps/web/tauri/Cargo.lock} apps/web/tauri/Cargo.lock+ '';+ postConfigure = ''+ writable_vendor="$TMPDIR/cargo-vendor"+ mkdir -p "$writable_vendor"+ cp -aL ${tauriCargoVendorDir}/. "$writable_vendor/"+ chmod -R u+w "$writable_vendor"+ substituteInPlace .cargo-home/config.toml \+ --replace-fail "${tauriCargoVendorDir}" "$writable_vendor"+ '';+ }+ // lib.optionalAttrs isAarch64Darwin {+ # Nix's linker crashes on the large final desktop binary. Use Apple's+ # linker for both dependencies and the app so Cargo can reuse them.+ CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER = tauriDesktopDmgAppleLinker;+ };+ };++ # Only final application builds need frontend assets. Including them in+ # craneArgs also invalidates buildDepsOnly whenever the frontend changes.+ tauriDesktopPreBuild = ''+ ${tauri.commonArgs.preBuild or ""}+ cp ${../apps/web/package.json} apps/web/package.json+ rm -rf apps/web/dist+ cp -r ${frontend} apps/web/dist+ cd apps/web/tauri+ '';++ tauriDesktopUnwrapped = tauri.app.overrideAttrs {+ preBuild = tauriDesktopPreBuild;};gioTlsModulePath = "${pkgs.glib-networking}/lib/gio/modules";wrappedTauriDesktop = pkgs.symlinkJoin {name = "macro-tauri-desktop-${appVersion}";- paths = [ tauri.app ];+ paths = [ tauriDesktopUnwrapped ];nativeBuildInputs = [ pkgs.makeWrapper ];postBuild = ''wrapProgram "$out/bin/app" \@@ -594,11 +603,6 @@pname = "macro-tauri-desktop-dmg";TAURI_CONFIG = tauriDesktopDmgConfig;APPLE_SIGNING_IDENTITY = tauriDesktopDmgSigningIdentity;- # Nix's cctools ld crashes with SIGTRAP while linking the large final- # desktop binary on GitHub's macOS 15 arm64 runners. This derivation- # is already an impure, unsandboxed native macOS build for signing;- # use the runner's Apple linker and SDK for Cargo links.- CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER = tauriDesktopDmgAppleLinker;nativeBuildInputs = tauri.commonArgs.nativeBuildInputs ++ [ pkgs.cargo-tauri ];preBuild = ''if [ -z "$APPLE_SIGNING_IDENTITY" ]; then@@ -606,7 +610,7 @@exit 1fiexport PATH="$PATH:/usr/bin:/bin:/usr/sbin:/sbin"- ${tauri.commonArgs.preBuild or ""}+ ${tauriDesktopPreBuild}'';buildPhaseCargoCommand = ''cargo tauri build --bundles app \@@ -771,7 +775,7 @@LD_LIBRARY_PATH = tauriRuntimeLibraryPath;LDAI_RUNTIME_FILE = tauriAppImageRuntime;preBuild = ''- ${tauri.commonArgs.preBuild or ""}+ ${tauriDesktopPreBuild}runtime_library_path="$(while IFS= read -r store_path; doif [ -d "$store_path/lib" ]; then@@ -889,7 +893,7 @@// lib.optionalAttrs isLinux {tauri-frontend = frontend;tauri-desktop = wrappedTauriDesktop;- tauri-desktop-unwrapped = tauri.app;+ tauri-desktop-unwrapped = tauriDesktopUnwrapped;tauri-desktop-cargo-artifacts = tauri.cargoArtifacts;}// lib.optionalAttrs isX86_64Linux {diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rsindex 21a866905a..85b5eee297 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs@@ -1,9 +1,14 @@//! `Build macOS DMG` — reusable workflow that builds the Tauri desktop DMG via//! Nix. Called from [`super::build_desktop_on_tag`].-use gh_workflow::{Event, Job, Run, Step, Workflow, WorkflowCall, WorkflowCallInput};+use gh_workflow::{Event, Expression, Job, Run, Step, Workflow, WorkflowCall, WorkflowCallInput};-use crate::workflows::{build_appimage_on_tag, steps, vars};+use crate::workflows::{build_appimage_on_tag, runners, steps, vars};++#[cfg(test)]+mod test;++const NIX_CACHE_SIZE_GB: u16 = 100;/// Build the reusable workflow.pub fn build_dmg() -> Workflow {@@ -30,13 +35,21 @@ pub fn build_dmg() -> Workflow {pub fn build_dmg_job(ref_expr: &str) -> Job {Job::default().name("Build macOS DMG")- .runs_on("macos-15")+ .runs_on(vec![+ format!("{}-with-cache", runners::Runner::MacOsArm),+ "nscloud-cache-tag-macro-desktop-macos".to_owned(),+ format!("nscloud-cache-size-{NIX_CACHE_SIZE_GB}gb"),+ ])+ .add_env(("DESKTOP_NIX_CACHE_SIZE_GB", NIX_CACHE_SIZE_GB.to_string())).add_step(steps::checkout_ref(ref_expr)).add_step(assert_arm64())+ .add_step(steps::mount_macos_nix_cache_volume()).add_step(steps::install_nix_macos())+ .add_step(configure_nix_cache()).add_step(configure_signing_identity()).add_step(steps::derive_artifact_metadata(ref_expr)).add_step(nix_build_dmg())+ .add_step(save_nix_cache()).add_step(collect_dmg()).add_step(validate_signed_dmg()).add_step(notarize_dmg())@@ -82,6 +95,24 @@ fn nix_build_dmg() -> Step<Run> {.shell("bash")}+fn configure_nix_cache() -> Step<Run> {+ Step::new("Configure macOS Nix binary cache")+ .run(include_str!("scripts/configure_macos_nix_cache.sh"))+ .id("nix-cache")+ .shell("bash")+ .continue_on_error(true)+}++fn save_nix_cache() -> Step<Run> {+ Step::new("Prune and save macOS Nix build dependencies")+ .run(include_str!("scripts/save_macos_nix_cache.sh"))+ .shell("bash")+ .if_condition(Expression::new(+ "!cancelled() && steps.nix-cache.outcome == 'success'",+ ))+ .continue_on_error(true)+}+fn collect_dmg() -> Step<Run> {Step::new("Collect DMG artifact").run(include_str!("scripts/collect_dmg.sh"))diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rsnew file mode 100644index 0000000000..d664d094d3--- /dev/null+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rs@@ -0,0 +1,363 @@+use std::collections::{BTreeMap, BTreeSet};+use std::fs;+use std::os::unix::fs::{PermissionsExt, symlink};+use std::path::{Path, PathBuf};+use std::process::{Command, Output};++use tempfile::TempDir;++const CURRENT_PATH: &str = "/nix/store/00000000000000000000000000000000-current";+const SHARED_PATH: &str = "/nix/store/11111111111111111111111111111111-shared";+const STALE_PATH: &str = "/nix/store/22222222222222222222222222222222-stale";+const BUILD_DERIVATION: &str = "/nix/store/33333333333333333333333333333333-desktop.drv";+const PLANNED_PATH: &str = "/nix/store/44444444444444444444444444444444-planned";++struct CacheFixture {+ temp: TempDir,+ cache_root: PathBuf,+ cache_dir: PathBuf,+}++impl CacheFixture {+ fn new(current_paths: &[&str]) -> Self {+ let temp = tempfile::tempdir().expect("temporary cache fixture");+ let cache_root = temp.path().join("cache");+ let cache_dir = cache_root.join("a".repeat(64));+ fs::create_dir_all(cache_dir.join("nar")).expect("current cache directory");+ fs::create_dir_all(temp.path().join("runner-temp")).expect("runner temporary directory");+ fs::create_dir_all(temp.path().join("bin")).expect("stub commands directory");+ fs::write(+ temp.path().join("runner-temp/desktop-cache-roots"),+ format!("{BUILD_DERIVATION}\n"),+ )+ .expect("build roots");+ fs::write(+ cache_dir.join("nix-cache-info"),+ "StoreDir: /nix/store\nWantMassQuery: 1\nPriority: 40\n",+ )+ .expect("binary cache metadata");++ // Include a duplicate and a derivation: only unique output paths belong+ // on nix copy's stdin, regardless of the closure query's ordering.+ let mut query_paths = current_paths.to_vec();+ query_paths.extend(current_paths.first().copied());+ query_paths.push(BUILD_DERIVATION);+ fs::write(+ temp.path().join("query-outputs"),+ format!("{}\n", query_paths.join("\n")),+ )+ .expect("fake closure query output");+ let expected_paths = current_paths.iter().copied().collect::<BTreeSet<_>>();+ fs::write(+ temp.path().join("expected-copy-input"),+ format!(+ "{}\n",+ expected_paths.into_iter().collect::<Vec<_>>().join("\n")+ ),+ )+ .expect("expected cache export paths");++ let fixture = Self {+ temp,+ cache_root,+ cache_dir,+ };+ fixture.set_planned_outputs(current_paths);+ fixture.write_command(+ "nix-store",+ r#"#!/usr/bin/env bash+set -euo pipefail+if [[ "$#" -eq 4 && "$1" == --query && "$2" == --requisites && "$3" == --include-outputs && "$4" == "$STUB_DERIVATION" ]]; then+ printf queried > "$STUB_QUERY_LOG"+ cat "$STUB_QUERY_OUTPUTS"+elif [[ "$#" -eq 3 && "$1" == --query && "$2" == --outputs && "$3" == "$STUB_DERIVATION" ]]; then+ printf queried > "$STUB_PLANNED_LOG"+ cat "$STUB_PLANNED_OUTPUTS"+ [[ ! -f "$STUB_PLANNED_FAILURE" ]]+else+ exit 2+fi+"#,+ );+ fixture.write_command(+ "nix",+ r#"#!/usr/bin/env bash+set -euo pipefail+[[ "$#" -eq 4 && "$1" == copy && "$2" == --stdin && "$3" == --to && "$4" == "$DESKTOP_NIX_CACHE_URL" ]]+[[ -z "$STUB_MISSING_NARINFO" || ! -e "$STUB_MISSING_NARINFO" ]]+cat > "$STUB_COPY_INPUT"+cmp "$STUB_EXPECTED_COPY_INPUT" "$STUB_COPY_INPUT"+printf copied > "$STUB_COPY_LOG"+"#,+ );+ fixture+ }++ fn set_planned_outputs(&self, paths: &[&str]) {+ let contents = if paths.is_empty() {+ String::new()+ } else {+ format!("{}\n", paths.join("\n"))+ };+ fs::write(self.temp.path().join("planned-outputs"), contents)+ .expect("planned derivation outputs");+ }++ fn write_command(&self, name: &str, contents: &str) {+ let path = self.temp.path().join("bin").join(name);+ fs::write(&path, contents).expect("stub command");+ fs::set_permissions(path, fs::Permissions::from_mode(0o755)).expect("executable stub");+ }++ fn narinfo_path(&self, store_path: &str) -> PathBuf {+ let name = store_path.rsplit('/').next().expect("store basename");+ let (hash, _) = name.split_once('-').expect("store hash");+ self.cache_dir.join(format!("{hash}.narinfo"))+ }++ fn write_narinfo(&self, store_path: &str, url: Option<&str>) -> PathBuf {+ let path = self.narinfo_path(store_path);+ let url_field = url.map(|url| format!("URL: {url}\n")).unwrap_or_default();+ let contents = format!(+ "StorePath: {store_path}\n{url_field}Compression: zstd\nFileHash: sha256:{}\nFileSize: 1\nNarHash: sha256:{}\nNarSize: 1\nReferences: \n",+ "0".repeat(52),+ "0".repeat(52),+ );+ fs::write(&path, contents).expect("cached narinfo");+ path+ }++ fn write_nar(&self, name: &str) -> PathBuf {+ let path = self.cache_dir.join("nar").join(name);+ fs::write(&path, name).expect("cached NAR");+ path+ }++ fn old_partition(&self) -> PathBuf {+ let path = self.cache_root.join("b".repeat(64));+ fs::create_dir_all(path.join("nar")).expect("old toolchain cache");+ fs::write(path.join("nar/old.nar.zst"), "old toolchain").expect("old cached NAR");+ path+ }++ fn run(&self, missing_narinfo: Option<&Path>) -> Output {+ let fixture_dir = self.temp.path();+ Command::new("bash")+ .args([+ "-c",+ "export PATH=\"$STUB_BIN:$PATH\"\nexec bash \"$STUB_SCRIPT\"",+ ])+ .env("STUB_BIN", fixture_dir.join("bin"))+ .env(+ "STUB_SCRIPT",+ Path::new(env!("CARGO_MANIFEST_DIR"))+ .join("src/workflows/scripts/save_macos_nix_cache.sh"),+ )+ .env("STUB_DERIVATION", BUILD_DERIVATION)+ .env("STUB_QUERY_OUTPUTS", fixture_dir.join("query-outputs"))+ .env("STUB_QUERY_LOG", fixture_dir.join("query-log"))+ .env("STUB_PLANNED_OUTPUTS", fixture_dir.join("planned-outputs"))+ .env("STUB_PLANNED_LOG", fixture_dir.join("planned-log"))+ .env(+ "STUB_PLANNED_FAILURE",+ fixture_dir.join("planned-query-fails"),+ )+ .env("STUB_COPY_INPUT", fixture_dir.join("copy-input"))+ .env("STUB_COPY_LOG", fixture_dir.join("copy-log"))+ .env(+ "STUB_EXPECTED_COPY_INPUT",+ fixture_dir.join("expected-copy-input"),+ )+ .env(+ "STUB_MISSING_NARINFO",+ missing_narinfo.unwrap_or_else(|| Path::new("")),+ )+ .env("RUNNER_TEMP", fixture_dir.join("runner-temp"))+ .env("GITHUB_STEP_SUMMARY", fixture_dir.join("step-summary"))+ .env("DESKTOP_NIX_CACHE_ROOT", &self.cache_root)+ .env("DESKTOP_NIX_CACHE_DIR", &self.cache_dir)+ .env(+ "DESKTOP_NIX_CACHE_URL",+ format!(+ "file://{}?compression=zstd&trusted=true&priority=10",+ self.cache_dir.display()+ ),+ )+ .output()+ .expect("run the actual cache save script")+ }++ fn assert_copy_completed(&self) {+ assert_eq!(+ fs::read(self.temp.path().join("copy-log")).expect("nix copy completed"),+ b"copied"+ );+ }+}++fn assert_success(output: &Output) {+ assert!(+ output.status.success(),+ "cache save failed: {:?}\nstdout: {}\nstderr: {}",+ output.status,+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr),+ );+}++fn snapshot(root: &Path) -> BTreeMap<PathBuf, Option<Vec<u8>>> {+ let mut entries = BTreeMap::new();+ for entry in fs::read_dir(root).expect("snapshot cache directory") {+ let path = entry.expect("cache entry").path();+ if path.is_dir() {+ entries.insert(path.clone(), None);+ entries.extend(snapshot(&path));+ } else {+ entries.insert(path.clone(), Some(fs::read(path).expect("cache file")));+ }+ }+ entries+}++#[test]+fn cache_pruning_preserves_current_shared_nars_and_removes_stale_content() {+ let fixture = CacheFixture::new(&[SHARED_PATH, CURRENT_PATH]);+ let current = fixture.write_narinfo(CURRENT_PATH, Some("nar/current.nar.zst"));+ let shared = fixture.write_narinfo(SHARED_PATH, Some("nar/shared.nar.zst"));+ let stale = fixture.write_narinfo(STALE_PATH, Some("nar/shared.nar.zst"));+ let current_nar = fixture.write_nar("current.nar.zst");+ let shared_nar = fixture.write_nar("shared.nar.zst");+ let orphan = fixture.write_nar("orphan.nar.zst");+ let old_partition = fixture.old_partition();+ // Namespace exposes cached directories through a symlink to its volume.+ let mounted_cache = fixture.temp.path().join("mounted-cache");+ fs::rename(&fixture.cache_root, &mounted_cache).expect("mount cache directory");+ symlink(&mounted_cache, &fixture.cache_root).expect("cache volume symlink");++ assert_success(&fixture.run(None));++ fixture.assert_copy_completed();+ for retained in [current, shared, current_nar, shared_nar] {+ assert!(+ retained.exists(),+ "current cache entry removed: {retained:?}"+ );+ }+ for removed in [stale, orphan, old_partition] {+ assert!(!removed.exists(), "stale cache entry retained: {removed:?}");+ }+ assert!(fixture.cache_dir.join("nix-cache-info").exists());+}++#[test]+fn cached_planned_outputs_are_retained_but_only_realized_outputs_are_exported() {+ let fixture = CacheFixture::new(&[CURRENT_PATH]);+ fixture.set_planned_outputs(&[CURRENT_PATH, PLANNED_PATH]);+ fixture.write_narinfo(CURRENT_PATH, Some("nar/current.nar.zst"));+ fixture.write_nar("current.nar.zst");+ let planned = fixture.write_narinfo(PLANNED_PATH, Some("nar/planned.nar.zst"));+ let planned_nar = fixture.write_nar("planned.nar.zst");+ let stale = fixture.write_narinfo(STALE_PATH, Some("nar/stale.nar.zst"));+ let stale_nar = fixture.write_nar("stale.nar.zst");++ assert_success(&fixture.run(None));++ fixture.assert_copy_completed();+ assert!(+ planned.exists(),+ "a needed, unrestored cache entry was pruned"+ );+ assert!(planned_nar.exists(), "a needed, unrestored NAR was pruned");+ assert!(!stale.exists());+ assert!(!stale_nar.exists());+ assert_eq!(+ fs::read_to_string(fixture.temp.path().join("copy-input")).expect("exported paths"),+ format!("{CURRENT_PATH}\n"),+ "nix copy cannot export an unrealized output"+ );+}++#[test]+fn failed_planned_output_queries_leave_the_cache_untouched() {+ for partial_outputs in [false, true] {+ let fixture = CacheFixture::new(&[CURRENT_PATH]);+ fixture.set_planned_outputs(if partial_outputs {+ &[CURRENT_PATH]+ } else {+ &[]+ });+ fs::write(fixture.temp.path().join("planned-query-fails"), "fail")+ .expect("make the planned output query fail");+ fixture.write_narinfo(CURRENT_PATH, Some("nar/current.nar.zst"));+ fixture.write_nar("current.nar.zst");+ fixture.write_narinfo(STALE_PATH, Some("nar/stale.nar.zst"));+ fixture.write_nar("stale.nar.zst");+ fixture.old_partition();+ let before = snapshot(&fixture.cache_root);++ let output = fixture.run(None);++ assert!(+ !output.status.success(),+ "failed query unexpectedly accepted"+ );+ assert!(fixture.temp.path().join("planned-log").exists());+ assert_eq!(snapshot(&fixture.cache_root), before);+ assert!(!fixture.temp.path().join("copy-log").exists());+ }+}++#[test]+fn malformed_retained_nar_urls_abort_without_mutating_the_cache() {+ for url in [+ None,+ Some("../outside.nar.zst"),+ Some("/tmp/outside.nar.zst"),+ Some("nar/../outside.nar.zst"),+ Some("https://example.invalid/outside.nar.zst"),+ ] {+ let fixture = CacheFixture::new(&[SHARED_PATH]);+ fixture.write_narinfo(SHARED_PATH, url);+ // Sort stale metadata before the invalid entry to catch pruning that+ // mutates files while validation is still in progress.+ fixture.write_narinfo(CURRENT_PATH, Some("nar/stale.nar.zst"));+ fixture.write_nar("stale.nar.zst");+ fixture.old_partition();+ let before = snapshot(&fixture.cache_root);++ let output = fixture.run(None);++ assert!(!output.status.success(), "unsafe URL accepted: {url:?}");+ assert_eq!(snapshot(&fixture.cache_root), before, "URL: {url:?}");+ assert!(!fixture.temp.path().join("copy-log").exists());+ }+}++#[test]+fn missing_nar_removes_its_narinfo_before_export_so_copy_can_repair_it() {+ let fixture = CacheFixture::new(&[CURRENT_PATH]);+ let narinfo = fixture.write_narinfo(CURRENT_PATH, Some("nar/missing.nar.zst"));++ assert_success(&fixture.run(Some(&narinfo)));++ fixture.assert_copy_completed();+ assert!(!narinfo.exists(), "incomplete cache entry prevents repair");+}++#[test]+fn missing_build_roots_leave_cache_contents_untouched() {+ let fixture = CacheFixture::new(&[CURRENT_PATH]);+ fixture.write_narinfo(CURRENT_PATH, Some("nar/current.nar.zst"));+ fixture.write_nar("current.nar.zst");+ fixture.old_partition();+ fs::remove_file(fixture.temp.path().join("runner-temp/desktop-cache-roots"))+ .expect("remove roots before running the script");+ let before = snapshot(&fixture.cache_root);++ assert_success(&fixture.run(None));++ assert_eq!(snapshot(&fixture.cache_root), before);+ assert!(!fixture.temp.path().join("query-log").exists());+ assert!(!fixture.temp.path().join("planned-log").exists());+ assert!(!fixture.temp.path().join("copy-log").exists());+}diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/build_dmg.sh b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/build_dmg.shindex 9d3d490520..ca46f3e50f 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/build_dmg.sh+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/build_dmg.sh@@ -1,5 +1,12 @@set -euo pipefail+# Record the derivations before building so completed dependencies can be saved+# even if a later compilation or packaging step fails.+nix path-info --derivation --impure \+ ".#packages.aarch64-darwin.tauri-desktop-apple-linker-smoke" \+ ".#packages.aarch64-darwin.tauri-desktop-dmg" \+ > "$RUNNER_TEMP/desktop-cache-roots"+nix build --impure --option sandbox false --print-build-logs \".#packages.aarch64-darwin.tauri-desktop-apple-linker-smoke"nix build --impure --option sandbox false --print-build-logs \diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/configure_macos_nix_cache.sh b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/configure_macos_nix_cache.shnew file mode 100644index 0000000000..ae852b579a--- /dev/null+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/configure_macos_nix_cache.sh@@ -0,0 +1,29 @@+set -euo pipefail++# The Apple linker/SDK live outside Nix. Do not reuse their outputs after the+# runner image changes its macOS or Xcode version.+toolchain_key=$(+ { xcodebuild -version && sw_vers -buildVersion; } |+ shasum -a 256 | awk '{print $1}'+)+cache_root="$HOME/.cache/macro-desktop-nix"+cache_dir="$cache_root/$toolchain_key"+mkdir -p "$cache_dir"+cache_url="file://$cache_dir?compression=zstd&trusted=true&priority=10"++# The mounted cache root is a symlink; the trailing slash measures its contents.+du -sk "$cache_root/" | awk '{printf "Restored macOS Nix cache: %.2f GiB\n", $1 / 1048576}'++# Initialize an empty cache before adding it as a substituter. Only this local,+# repository-specific cache accepts unsigned paths; public caches still require+# signatures. Keep the normal /nix store and its daemon-managed permissions.+nix store info --store "$cache_url" --json+{+ echo "DESKTOP_NIX_CACHE_ROOT=$cache_root"+ echo "DESKTOP_NIX_CACHE_DIR=$cache_dir"+ echo "DESKTOP_NIX_CACHE_URL=$cache_url"+ echo "NIX_CONFIG<<DESKTOP_NIX_CONFIG_EOF"+ printf '%s\n' "${NIX_CONFIG:-}"+ echo "extra-substituters = $cache_url"+ echo "DESKTOP_NIX_CONFIG_EOF"+} >> "$GITHUB_ENV"diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/save_macos_nix_cache.sh b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/save_macos_nix_cache.shnew file mode 100644index 0000000000..1293b5e9e7--- /dev/null+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/save_macos_nix_cache.sh@@ -0,0 +1,133 @@+set -euo pipefail+export LC_ALL=C++roots_file="$RUNNER_TEMP/desktop-cache-roots"+if [ ! -s "$roots_file" ]; then+ exit 0+fi++cache_root="${DESKTOP_NIX_CACHE_ROOT:?}"+cache_dir="${DESKTOP_NIX_CACHE_DIR:?}"+toolchain_key="${cache_dir#"$cache_root/"}"+if [ "${#toolchain_key}" -ne 64 ] || [[ "$toolchain_key" == *[!0-9a-f]* ]] \+ || [ "$cache_dir" != "$cache_root/$toolchain_key" ] \+ || [ -L "$cache_dir" ] || [ -L "$cache_dir/nar" ]; then+ echo "Refusing to prune an unexpected macOS Nix cache directory: $cache_dir" >&2+ exit 1+fi++work_dir=$(mktemp -d "$RUNNER_TEMP/desktop-cache-prune.XXXXXX")+trap 'rm -rf "$work_dir"' EXIT+shopt -s nullglob dotglob++report_size() {+ cache_size_kib=$(du -sk "$cache_root/" | awk '{print $1}')+ local message+ message=$(awk -v phase="$1" -v kib="$cache_size_kib" \+ 'BEGIN {printf "macOS Nix cache %s: %.2f GiB", phase, kib / 1048576}')+ echo "$message"+ if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then+ printf -- '- %s\n' "$message" >> "$GITHUB_STEP_SUMMARY"+ fi+}+report_size "before pruning"++# A DMG's runtime closure omits build-only inputs such as Cargo artifacts and+# the toolchain. Walk its derivation closure and include every existing output,+# including successful dependencies of an otherwise failed build.+while IFS= read -r derivation; do+ nix-store --query --requisites --include-outputs "$derivation"+done < "$roots_file" |+ sort -u > "$work_dir/requisites"+awk '!/\.drv$/' "$work_dir/requisites" > "$work_dir/paths"++# Finish enumeration and validate metadata before deleting anything. An empty+# closure or a failed query must leave the previous cache intact.+if [ ! -s "$work_dir/paths" ]; then+ echo "No completed Nix outputs to cache; leaving the cache intact."+ exit 0+fi+# An early failure may leave current dependencies in the binary cache without+# having restored them locally. Keep their planned outputs too, while exporting+# only the realized paths above. Bound argv size for macOS's smaller limit.+awk '/\.drv$/' "$work_dir/requisites" > "$work_dir/derivations"+: > "$work_dir/planned-paths"+if [ -s "$work_dir/derivations" ]; then+ xargs -n 128 nix-store --query --outputs < "$work_dir/derivations" > "$work_dir/planned-paths"+fi+awk -F/ '+ NF != 4 || $2 != "nix" || $3 != "store" {exit 1}+ {+ split($4, parts, "-")+ if (length(parts[1]) != 32 || parts[1] ~ /[^0123456789abcdfghijklmnpqrsvwxyz]/) exit 1+ print parts[1] ".narinfo"+ }+' "$work_dir/paths" "$work_dir/planned-paths" | sort -u > "$work_dir/wanted-narinfos"+for metadata in "$cache_dir"/*.narinfo; do+ printf '%s\n' "${metadata##*/}"+done | sort -u > "$work_dir/all-narinfos"+comm -12 "$work_dir/all-narinfos" "$work_dir/wanted-narinfos" > "$work_dir/retained-narinfos"+comm -23 "$work_dir/all-narinfos" "$work_dir/wanted-narinfos" > "$work_dir/remove-narinfos"+: > "$work_dir/retained-nars"++while IFS= read -r metadata; do+ nar_url=""+ store_path=""+ while read -r field value; do+ case "$field" in+ URL:) nar_url="$value" ;;+ StorePath:) store_path="$value" ;;+ esac+ done < "$cache_dir/$metadata"+ nar_file="${nar_url#nar/}"+ if [[ "$nar_url" != nar/* ]] || [[ "$nar_file" == *[!a-zA-Z0-9._-]* ]] \+ || [[ "$nar_file" == .* ]] || [ -z "$nar_file" ] \+ || [[ "$store_path" != /nix/store/"${metadata%.narinfo}"-* ]] \+ || [ -L "$cache_dir/$metadata" ]; then+ echo "Invalid retained Nix cache metadata: $metadata; leaving the cache intact." >&2+ exit 1+ fi+ if [ -f "$cache_dir/$nar_url" ] && [ ! -L "$cache_dir/$nar_url" ]; then+ printf '%s\n' "$nar_url" >> "$work_dir/retained-nars"+ else+ # Nix checks narinfo existence, not archive existence. Remove dangling+ # metadata so the subsequent copy repairs this path instead of skipping it.+ printf '%s\n' "$metadata" >> "$work_dir/remove-narinfos"+ fi+done < "$work_dir/retained-narinfos"+sort -u -o "$work_dir/retained-nars" "$work_dir/retained-nars"+for archive in "$cache_dir"/nar/*; do+ printf 'nar/%s\n' "${archive##*/}"+done | sort -u > "$work_dir/all-nars"+# Different store paths may share one compressed archive. Retain the union of+# their URLs so removing one obsolete narinfo cannot break a current path.+comm -23 "$work_dir/all-nars" "$work_dir/retained-nars" > "$work_dir/remove-nars"++while IFS= read -r metadata; do+ rm -f "$cache_dir/$metadata"+done < "$work_dir/remove-narinfos"+while IFS= read -r archive; do+ rm -f "$cache_dir/$archive"+done < "$work_dir/remove-nars"+for partition in "$cache_root"/*; do+ partition_key="${partition##*/}"+ if [ "${#partition_key}" -eq 64 ] && [[ "$partition_key" != *[!0-9a-f]* ]] \+ && [ "$partition" != "$cache_dir" ] && [ -d "$partition" ] && [ ! -L "$partition" ]; then+ rm -rf "$partition"+ fi+done+printf 'Pruned %s metadata entries and %s archives; retaining %s current store paths.\n' \+ "$(wc -l < "$work_dir/remove-narinfos" | tr -d ' ')" \+ "$(wc -l < "$work_dir/remove-nars" | tr -d ' ')" \+ "$(wc -l < "$work_dir/wanted-narinfos" | tr -d ' ')"+report_size "after pruning"++export_started=$SECONDS+copy_status=0+nix copy --stdin --to "$DESKTOP_NIX_CACHE_URL" < "$work_dir/paths" || copy_status=$?+echo "Nix cache export took $((SECONDS - export_started)) seconds."+report_size "after export"+if (( cache_size_kib * 1024 >= ${DESKTOP_NIX_CACHE_SIZE_GB:-100} * 1000000000 )); then+ echo "::warning::The current Nix build closure exceeds the configured cache capacity even after pruning; Namespace may start the next build cold."+fi+exit "$copy_status"diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/steps.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/steps.rsindex 71af6b04ee..7b8be3b368 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/steps.rs+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/steps.rs@@ -443,6 +443,13 @@ pub fn mount_nix_cache_volume() -> Step<Use> {nscloud_cache_action("Mount /nix cache volume").add_with(("cache", "nix"))}+/// macOS cache mounts are symlinks, which Nix does not support for `/nix`.+/// Persist a file binary cache instead, leaving the live store to the installer.+pub fn mount_macos_nix_cache_volume() -> Step<Use> {+ nscloud_cache_action("Mount macOS Nix binary cache")+ .add_with(("path", "~/.cache/macro-desktop-nix"))+}+/// Derive a safe tag name from the git ref for use in artifact names.pub fn derive_artifact_metadata(raw_ref_expr: &str) -> Step<Run> {Step::new("Derive artifact metadata")