Skip to content

Commit 2786eaf

Browse files
committed
feat(darwin): enable sccache for darwin builds with daemon restart
Enable sccache for darwin builds with persistent cache via stickydisk mounted per postgres version. Stop existing sccache daemon before builds to prevent stale TMPDIR state from previous builds causing permission errors. Override TMPDIR to /tmp on darwin so sccache daemon uses shared temp directory accessible to all nixbld users. Ensure cache directory has 2777 permissions via darwin activation script. Add writable check before enabling sccache. Add tmate debug session on failures with cleanup of existing sessions. Guard ephemeral runner sccache with directory checks.
1 parent 683d135 commit 2786eaf

File tree

4 files changed

+107
-29
lines changed

4 files changed

+107
-29
lines changed

.github/workflows/nix-build.yml

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ jobs:
5959
if: ${{ matrix.attr != '' && matrix.postgresql_version && matrix.runs_on.group != 'self-hosted-runners-nix' }}
6060
run: |
6161
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
62-
sudo chown -R 872415232 /nix/var/cache/sccache
63-
sudo chmod -R 2777 /nix/var/cache/sccache
62+
if [ -d /nix/var/cache/sccache ]; then sudo chown -R 872415232 /nix/var/cache/sccache; fi
63+
if [ -d /nix/var/cache/sccache ]; then sudo chmod -R 2777 /nix/var/cache/sccache; fi
6464
- name: nix build
6565
if: ${{ matrix.attr != '' }}
6666
shell: bash
@@ -104,8 +104,8 @@ jobs:
104104
if: ${{ matrix.attr != '' && matrix.postgresql_version && matrix.runs_on.group != 'self-hosted-runners-nix' }}
105105
run: |
106106
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
107-
sudo chown -R 872415232 /nix/var/cache/sccache
108-
sudo chmod -R 2777 /nix/var/cache/sccache
107+
if [ -d /nix/var/cache/sccache ]; then sudo chown -R 872415232 /nix/var/cache/sccache; fi
108+
if [ -d /nix/var/cache/sccache ]; then sudo chmod -R 2777 /nix/var/cache/sccache; fi
109109
- name: nix build
110110
if: ${{ matrix.attr != '' }}
111111
shell: bash
@@ -126,13 +126,43 @@ jobs:
126126
- name: Checkout Repo
127127
if: ${{ matrix.attr != '' }}
128128
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
129+
- name: Mount sccache disk
130+
if: ${{ matrix.attr != '' && matrix.postgresql_version }}
131+
uses: useblacksmith/stickydisk@v1
132+
with:
133+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
134+
path: /nix/var/cache/sccache
129135
- name: Install nix
130136
if: ${{ matrix.attr != '' }}
131137
uses: ./.github/actions/nix-install-self-hosted
138+
- name: Configure sccache cache directory
139+
if: ${{ matrix.attr != '' && matrix.postgresql_version }}
140+
run: |
141+
mkdir -p /nix/var/cache/sccache || true
142+
# Stop any existing sccache daemon to avoid stale TMPDIR state
143+
if command -v sccache &> /dev/null; then
144+
sccache --stop-server 2>/dev/null || true
145+
fi
132146
- name: nix build
133147
if: ${{ matrix.attr != '' }}
134148
shell: bash
135149
run: nix build --accept-flake-config -L .#${{ matrix.attr }}
150+
- name: Setup tmate session on failure
151+
if: ${{ failure() && matrix.attr != '' && matrix.postgresql_version }}
152+
timeout-minutes: 30
153+
shell: bash
154+
run: |
155+
nix profile install nixpkgs#tmate
156+
export PATH="$HOME/.nix-profile/bin:$PATH"
157+
mkdir -p ~/.tmate
158+
killall tmate 2>/dev/null || true
159+
rm -f /tmp/tmate.sock
160+
rm -f /tmp/tmate.sock
161+
tmate -S /tmp/tmate.sock new-session -d
162+
tmate -S /tmp/tmate.sock wait tmate-ready
163+
tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}'
164+
echo "Session active for 30 minutes. Cancel workflow to exit."
165+
sleep 1800
136166
137167
nix-build-checks-aarch64-darwin:
138168
name: >-
@@ -149,9 +179,23 @@ jobs:
149179
- name: Checkout Repo
150180
if: ${{ matrix.attr != '' }}
151181
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
182+
- name: Mount sccache disk
183+
if: ${{ matrix.attr != '' && matrix.postgresql_version }}
184+
uses: useblacksmith/stickydisk@v1
185+
with:
186+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
187+
path: /nix/var/cache/sccache
152188
- name: Install nix
153189
if: ${{ matrix.attr != '' }}
154190
uses: ./.github/actions/nix-install-self-hosted
191+
- name: Configure sccache cache directory
192+
if: ${{ matrix.attr != '' && matrix.postgresql_version }}
193+
run: |
194+
mkdir -p /nix/var/cache/sccache || true
195+
# Stop any existing sccache daemon to avoid stale TMPDIR state
196+
if command -v sccache &> /dev/null; then
197+
sccache --stop-server 2>/dev/null || true
198+
fi
155199
- name: nix build
156200
if: ${{ matrix.attr != '' }}
157201
shell: bash
@@ -192,12 +236,28 @@ jobs:
192236
if: ${{ matrix.attr != '' && matrix.postgresql_version && matrix.runs_on.group != 'self-hosted-runners-nix' }}
193237
run: |
194238
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
195-
sudo chown -R 872415232 /nix/var/cache/sccache
196-
sudo chmod -R 2777 /nix/var/cache/sccache
239+
if [ -d /nix/var/cache/sccache ]; then sudo chown -R 872415232 /nix/var/cache/sccache; fi
240+
if [ -d /nix/var/cache/sccache ]; then sudo chmod -R 2777 /nix/var/cache/sccache; fi
197241
- name: nix build
198242
if: ${{ matrix.attr != '' }}
199243
shell: bash
200244
run: nix build --accept-flake-config -L .#${{ matrix.attr }}
245+
- name: Setup tmate session on failure
246+
if: ${{ failure() && matrix.attr != '' && matrix.postgresql_version }}
247+
timeout-minutes: 30
248+
shell: bash
249+
run: |
250+
nix profile install nixpkgs#tmate
251+
export PATH="$HOME/.nix-profile/bin:$PATH"
252+
mkdir -p ~/.tmate
253+
killall tmate 2>/dev/null || true
254+
rm -f /tmp/tmate.sock
255+
rm -f /tmp/tmate.sock
256+
tmate -S /tmp/tmate.sock new-session -d
257+
tmate -S /tmp/tmate.sock wait tmate-ready
258+
tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}'
259+
echo "Session active for 30 minutes. Cancel workflow to exit."
260+
sleep 1800
201261
202262
nix-build-checks-x86_64-linux:
203263
name: >-
@@ -234,8 +294,8 @@ jobs:
234294
if: ${{ matrix.attr != '' && matrix.postgresql_version && matrix.runs_on.group != 'self-hosted-runners-nix' }}
235295
run: |
236296
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
237-
sudo chown -R 872415232 /nix/var/cache/sccache
238-
sudo chmod -R 2777 /nix/var/cache/sccache
297+
if [ -d /nix/var/cache/sccache ]; then sudo chown -R 872415232 /nix/var/cache/sccache; fi
298+
if [ -d /nix/var/cache/sccache ]; then sudo chmod -R 2777 /nix/var/cache/sccache; fi
239299
- name: nix build
240300
if: ${{ matrix.attr != '' }}
241301
shell: bash

flake.lock

Lines changed: 19 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
nix-editor.url = "github:snowfallorg/nix-editor";
2121
nix-eval-jobs.inputs.flake-parts.follows = "flake-parts";
2222
nix-eval-jobs.inputs.treefmt-nix.follows = "treefmt-nix";
23-
nix-eval-jobs.url = "github:nix-community/nix-eval-jobs";
23+
nix-eval-jobs.url = "github:jfroche/nix-eval-jobs/fix-warnings";
2424
nix2container.inputs.nixpkgs.follows = "nixpkgs";
2525
nix2container.url = "github:nlewo/nix2container";
2626
# Pin to a specific nixpkgs version that has compatible v8 and curl versions

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,26 @@ let
164164

165165
buildPhase = ''
166166
runHook preBuild
167-
167+
echo "Platform: ${stdenv.system}"
168+
echo "isDarwin: ${lib.boolToString stdenv.isDarwin}"
168169
if [[ -d "/nix/var/cache/sccache" && -w "/nix/var/cache/sccache" ]]; then
169-
echo "sccache: cache directory available, enabling"
170-
export RUSTC_WRAPPER="${sccache}/bin/sccache"
171-
export SCCACHE_DIR="/nix/var/cache/sccache"
172-
export SCCACHE_CACHE_SIZE="50G"
170+
if touch "/nix/var/cache/sccache/.test" 2>/dev/null && rm -f "/nix/var/cache/sccache/.test" 2>/dev/null; then
171+
echo "sccache: cache directory available and writable, enabling"
172+
${lib.optionalString stdenv.isDarwin ''
173+
# Darwin: Use shared /tmp for TMPDIR to avoid sccache caching per-build temp paths
174+
export TMPDIR=/tmp
175+
export TEMP=/tmp
176+
export TEMPDIR=/tmp
177+
export TMP=/tmp
178+
''}
179+
export RUSTC_WRAPPER="${sccache}/bin/sccache"
180+
export SCCACHE_DIR="/nix/var/cache/sccache"
181+
export SCCACHE_CACHE_SIZE="50G"
182+
export SCCACHE_LOG=debug
183+
export SCCACHE_IDLE_TIMEOUT=0
184+
else
185+
echo "sccache: cache directory not accessible in sandbox, skipping"
186+
fi
173187
else
174188
echo "sccache: cache directory not available, skipping"
175189
fi

0 commit comments

Comments
 (0)