Skip to content

Commit 7cf3c52

Browse files
committed
feat(darwin): enable sccache for darwin builds with per-user isolation
Enable sccache for darwin builds with persistent cache via stickydisk mounted per postgres version. Use unique SCCACHE_SERVER_PORT per user ID to isolate sccache daemons between nixbld users, preventing temp directory permission errors. Stop existing daemon before builds to clear stale state. Override TMPDIR to /tmp on darwin for shared temp access. Ensure cache directory has 2777 permissions via darwin activation script. Add writable check before enabling sccache. Guard ephemeral runner sccache with directory checks.
1 parent 683d135 commit 7cf3c52

File tree

4 files changed

+78
-29
lines changed

4 files changed

+78
-29
lines changed

.github/workflows/nix-build.yml

Lines changed: 36 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,9 +126,23 @@ 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
@@ -149,9 +163,23 @@ jobs:
149163
- name: Checkout Repo
150164
if: ${{ matrix.attr != '' }}
151165
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
166+
- name: Mount sccache disk
167+
if: ${{ matrix.attr != '' && matrix.postgresql_version }}
168+
uses: useblacksmith/stickydisk@v1
169+
with:
170+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
171+
path: /nix/var/cache/sccache
152172
- name: Install nix
153173
if: ${{ matrix.attr != '' }}
154174
uses: ./.github/actions/nix-install-self-hosted
175+
- name: Configure sccache cache directory
176+
if: ${{ matrix.attr != '' && matrix.postgresql_version }}
177+
run: |
178+
mkdir -p /nix/var/cache/sccache || true
179+
# Stop any existing sccache daemon to avoid stale TMPDIR state
180+
if command -v sccache &> /dev/null; then
181+
sccache --stop-server 2>/dev/null || true
182+
fi
155183
- name: nix build
156184
if: ${{ matrix.attr != '' }}
157185
shell: bash
@@ -192,8 +220,8 @@ jobs:
192220
if: ${{ matrix.attr != '' && matrix.postgresql_version && matrix.runs_on.group != 'self-hosted-runners-nix' }}
193221
run: |
194222
# 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
223+
if [ -d /nix/var/cache/sccache ]; then sudo chown -R 872415232 /nix/var/cache/sccache; fi
224+
if [ -d /nix/var/cache/sccache ]; then sudo chmod -R 2777 /nix/var/cache/sccache; fi
197225
- name: nix build
198226
if: ${{ matrix.attr != '' }}
199227
shell: bash
@@ -234,8 +262,8 @@ jobs:
234262
if: ${{ matrix.attr != '' && matrix.postgresql_version && matrix.runs_on.group != 'self-hosted-runners-nix' }}
235263
run: |
236264
# 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
265+
if [ -d /nix/var/cache/sccache ]; then sudo chown -R 872415232 /nix/var/cache/sccache; fi
266+
if [ -d /nix/var/cache/sccache ]; then sudo chmod -R 2777 /nix/var/cache/sccache; fi
239267
- name: nix build
240268
if: ${{ matrix.attr != '' }}
241269
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: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,29 @@ 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+
# Use unique port per user to isolate sccache servers
185+
USER_ID=$(id -u)
186+
export SCCACHE_SERVER_PORT=$((4226 + USER_ID % 100))
187+
else
188+
echo "sccache: cache directory not accessible in sandbox, skipping"
189+
fi
173190
else
174191
echo "sccache: cache directory not available, skipping"
175192
fi

0 commit comments

Comments
 (0)