Skip to content

Commit 0024ea8

Browse files
jfrocheyvan-sraka
authored andcommitted
feat(sccache): enable sccache for pgrx builds with per-PG-version cache keys
- Add sccache to buildPgrxExtension.nix nativeBuildInputs - Enable sccache conditionally when /nix/var/cache/sccache is available - Configure Nix with auto-allocate-uids and max-jobs=1 for consistent UID mapping - Add cache_key field to github-matrix output for stickydisk grouping - Fix postgresql_version extraction for packages with extra path levels - Use per-postgres-version cache keys to reduce stickydisk conflicts - Chown cache to UID 872415232 (auto-allocate-uids base) before builds
1 parent 9f3315d commit 0024ea8

5 files changed

Lines changed: 126 additions & 41 deletions

File tree

.github/actions/nix-install-ephemeral/action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ runs:
5353
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
5454
${{ inputs.push-to-cache == 'true' && 'post-build-hook = /etc/nix/upload-to-cache.sh' || '' }}
5555
${{ inputs.enable-sccache-sandbox-path == 'true' && 'extra-sandbox-paths = /nix/var/cache/sccache?' || '' }}
56-
max-jobs = 4
56+
max-jobs = 1
57+
auto-allocate-uids = true
58+
use-cgroups = true
59+
experimental-features = nix-command flakes cgroups auto-allocate-uids

.github/workflows/nix-build.yml

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
4141
uses: useblacksmith/stickydisk@v1
4242
with:
43-
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
43+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
4444
path: /nix/var/cache/sccache
4545
- name: Install nix (ephemeral)
4646
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
@@ -57,10 +57,9 @@ jobs:
5757
- name: Allow sccache cache write access
5858
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
5959
run: |
60-
sudo chgrp nixbld /nix/var/cache/sccache
61-
sudo chmod 777 /nix/var/cache/sccache
62-
sudo chmod g+s /nix/var/cache/sccache
63-
sudo setfacl -d -m u::rwX,g::rwX,o::rwX /nix/var/cache/sccache
60+
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
61+
sudo chown -R 872415232 /nix/var/cache/sccache
62+
sudo chmod -R 2777 /nix/var/cache/sccache
6463
- name: nix build
6564
if: ${{ matrix.attr != '' }}
6665
shell: bash
@@ -85,7 +84,7 @@ jobs:
8584
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
8685
uses: useblacksmith/stickydisk@v1
8786
with:
88-
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
87+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
8988
path: /nix/var/cache/sccache
9089
- name: Install nix (ephemeral)
9190
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
@@ -102,10 +101,9 @@ jobs:
102101
- name: Allow sccache cache write access
103102
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
104103
run: |
105-
sudo chgrp nixbld /nix/var/cache/sccache
106-
sudo chmod 777 /nix/var/cache/sccache
107-
sudo chmod g+s /nix/var/cache/sccache
108-
sudo setfacl -d -m u::rwX,g::rwX,o::rwX /nix/var/cache/sccache
104+
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
105+
sudo chown -R 872415232 /nix/var/cache/sccache
106+
sudo chmod -R 2777 /nix/var/cache/sccache
109107
- name: nix build
110108
if: ${{ matrix.attr != '' }}
111109
shell: bash
@@ -176,7 +174,7 @@ jobs:
176174
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
177175
uses: useblacksmith/stickydisk@v1
178176
with:
179-
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
177+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
180178
path: /nix/var/cache/sccache
181179
- name: Install nix
182180
if: ${{ matrix.attr != '' }}
@@ -190,10 +188,9 @@ jobs:
190188
- name: Allow sccache cache write access
191189
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
192190
run: |
193-
sudo chgrp nixbld /nix/var/cache/sccache
194-
sudo chmod 777 /nix/var/cache/sccache
195-
sudo chmod g+s /nix/var/cache/sccache
196-
sudo setfacl -d -m u::rwX,g::rwX,o::rwX /nix/var/cache/sccache
191+
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
192+
sudo chown -R 872415232 /nix/var/cache/sccache
193+
sudo chmod -R 2777 /nix/var/cache/sccache
197194
- name: nix build
198195
if: ${{ matrix.attr != '' }}
199196
shell: bash
@@ -218,7 +215,7 @@ jobs:
218215
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
219216
uses: useblacksmith/stickydisk@v1
220217
with:
221-
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
218+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cache_key }}
222219
path: /nix/var/cache/sccache
223220
- name: Install nix
224221
if: ${{ matrix.attr != '' }}
@@ -232,10 +229,9 @@ jobs:
232229
- name: Allow sccache cache write access
233230
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
234231
run: |
235-
sudo chgrp nixbld /nix/var/cache/sccache
236-
sudo chmod 777 /nix/var/cache/sccache
237-
sudo chmod g+s /nix/var/cache/sccache
238-
sudo setfacl -d -m u::rwX,g::rwX,o::rwX /nix/var/cache/sccache
232+
# With auto-allocate-uids, UID 872415232 (0x34000000) maps to nixbld inside sandbox
233+
sudo chown -R 872415232 /nix/var/cache/sccache
234+
sudo chmod -R 2777 /nix/var/cache/sccache
239235
- name: nix build
240236
if: ${{ matrix.attr != '' }}
241237
shell: bash

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ let
169169
echo "sccache: cache directory available, enabling"
170170
export RUSTC_WRAPPER="${sccache}/bin/sccache"
171171
export SCCACHE_DIR="/nix/var/cache/sccache"
172-
export SCCACHE_CACHE_SIZE="500G"
172+
export SCCACHE_CACHE_SIZE="50G"
173173
else
174174
echo "sccache: cache directory not available, skipping"
175175
fi

nix/packages/github-matrix/github_matrix.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class GitHubActionPackage(TypedDict):
5858
system: System
5959
runs_on: RunsOnConfig
6060
postgresql_version: NotRequired[str]
61+
cache_key: NotRequired[str]
6162

6263

6364
class NixEvalError(TypedDict):
@@ -213,8 +214,7 @@ def run_nix_eval_jobs(
213214

214215
def is_extension_pkg(pkg: NixEvalJobsOutput) -> bool:
215216
"""Check if the package is a postgresql extension package."""
216-
attrs = pkg["attr"].split(".")
217-
return attrs[-2] == "exts"
217+
return ".exts." in pkg["attr"]
218218

219219

220220
# thank you buildbot-nix https://github.com/nix-community/buildbot-nix/blob/985d069a2a45cf4a571a4346107671adc2bd2a16/buildbot_nix/buildbot_nix/build_trigger.py#L297
@@ -251,6 +251,31 @@ def is_kvm_pkg(pkg: NixEvalJobsOutput) -> bool:
251251
return "kvm" in pkg.get("requiredSystemFeatures", [])
252252

253253

254+
def clean_package_for_output(pkg: NixEvalJobsOutput) -> GitHubActionPackage:
255+
"""Convert nix-eval-jobs output to GitHub Actions matrix package"""
256+
runner = get_runner_for_package(pkg)
257+
if runner is None:
258+
raise ValueError(f"No runner configuration for system: {pkg['system']}")
259+
returned_pkg: GitHubActionPackage = {
260+
"attr": pkg["attr"],
261+
"name": pkg["name"],
262+
"system": pkg["system"],
263+
"runs_on": runner,
264+
}
265+
if is_extension_pkg(pkg):
266+
# Extract PostgreSQL version from attribute path
267+
# e.g., legacyPackages.aarch64-linux.psql_17.exts.wrappers-pkgs.0_5_6
268+
# or legacyPackages.aarch64-linux.psql_17.exts.pg_graphql
269+
attrs = pkg["attr"].split(".")
270+
exts_idx = attrs.index("exts")
271+
pg_version = attrs[exts_idx - 1].split("_")[-1]
272+
returned_pkg["postgresql_version"] = pg_version
273+
returned_pkg["cache_key"] = f"pg{pg_version}"
274+
else:
275+
returned_pkg["cache_key"] = "shared"
276+
return returned_pkg
277+
278+
254279
def get_runner_for_package(pkg: NixEvalJobsOutput) -> RunsOnConfig | None:
255280
"""Determine the appropriate GitHub Actions runner for a package.
256281
@@ -304,23 +329,6 @@ def main() -> None:
304329
packages, warnings_list, errors_list = run_nix_eval_jobs(cmd)
305330
gh_action_packages = sort_pkgs_by_closures(packages)
306331

307-
def clean_package_for_output(pkg: NixEvalJobsOutput) -> GitHubActionPackage:
308-
"""Convert nix-eval-jobs output to GitHub Actions matrix package"""
309-
runner = get_runner_for_package(pkg)
310-
if runner is None:
311-
raise ValueError(f"No runner configuration for system: {pkg['system']}")
312-
returned_pkg: GitHubActionPackage = {
313-
"attr": pkg["attr"],
314-
"name": pkg["name"],
315-
"system": pkg["system"],
316-
"runs_on": runner,
317-
}
318-
if is_extension_pkg(pkg):
319-
# Extract PostgreSQL version from attribute path
320-
attrs = pkg["attr"].split(".")
321-
returned_pkg["postgresql_version"] = attrs[-3].split("_")[-1]
322-
return returned_pkg
323-
324332
# Group packages by system and type (checks vs packages)
325333
packages_by_system: Dict[System, List[GitHubActionPackage]] = defaultdict(list)
326334
checks_by_system: Dict[System, List[GitHubActionPackage]] = defaultdict(list)

nix/packages/github-matrix/tests/test_github_matrix.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from github_matrix import (
66
NixEvalJobsOutput,
7+
clean_package_for_output,
78
get_runner_for_package,
89
is_extension_pkg,
910
is_kvm_pkg,
@@ -260,3 +261,80 @@ def test_dependency_order(self):
260261

261262
result = sort_pkgs_by_closures([pkg1, pkg2])
262263
assert result == [pkg1, pkg2]
264+
265+
266+
class TestCleanPackageForOutput:
267+
def test_extension_package_simple_path(self):
268+
"""Test extension package like pg_graphql with simple attr path"""
269+
pkg: NixEvalJobsOutput = {
270+
"attr": "legacyPackages.aarch64-linux.psql_17.exts.pg_graphql",
271+
"attrPath": [
272+
"legacyPackages",
273+
"aarch64-linux",
274+
"psql_17",
275+
"exts",
276+
"pg_graphql",
277+
],
278+
"cacheStatus": "notBuilt",
279+
"drvPath": "/nix/store/test.drv",
280+
"name": "pg_graphql",
281+
"system": "aarch64-linux",
282+
}
283+
result = clean_package_for_output(pkg)
284+
assert result["postgresql_version"] == "17"
285+
assert result["cache_key"] == "pg17"
286+
287+
def test_extension_package_versioned_path(self):
288+
"""Test extension package like wrappers with version suffix in attr path"""
289+
pkg: NixEvalJobsOutput = {
290+
"attr": "legacyPackages.aarch64-linux.psql_17.exts.wrappers-pkgs.0_5_6",
291+
"attrPath": [
292+
"legacyPackages",
293+
"aarch64-linux",
294+
"psql_17",
295+
"exts",
296+
"wrappers-pkgs",
297+
"0_5_6",
298+
],
299+
"cacheStatus": "notBuilt",
300+
"drvPath": "/nix/store/test.drv",
301+
"name": "wrappers-0.5.6",
302+
"system": "aarch64-linux",
303+
}
304+
result = clean_package_for_output(pkg)
305+
assert result["postgresql_version"] == "17"
306+
assert result["cache_key"] == "pg17"
307+
308+
def test_extension_package_pg15(self):
309+
"""Test extension package with PostgreSQL 15"""
310+
pkg: NixEvalJobsOutput = {
311+
"attr": "legacyPackages.x86_64-linux.psql_15.exts.pg_cron",
312+
"attrPath": [
313+
"legacyPackages",
314+
"x86_64-linux",
315+
"psql_15",
316+
"exts",
317+
"pg_cron",
318+
],
319+
"cacheStatus": "notBuilt",
320+
"drvPath": "/nix/store/test.drv",
321+
"name": "pg_cron",
322+
"system": "x86_64-linux",
323+
}
324+
result = clean_package_for_output(pkg)
325+
assert result["postgresql_version"] == "15"
326+
assert result["cache_key"] == "pg15"
327+
328+
def test_non_extension_package(self):
329+
"""Test non-extension package gets shared cache key"""
330+
pkg: NixEvalJobsOutput = {
331+
"attr": "legacyPackages.x86_64-linux.psql_15",
332+
"attrPath": ["legacyPackages", "x86_64-linux", "psql_15"],
333+
"cacheStatus": "notBuilt",
334+
"drvPath": "/nix/store/test.drv",
335+
"name": "postgresql-15.0",
336+
"system": "x86_64-linux",
337+
}
338+
result = clean_package_for_output(pkg)
339+
assert "postgresql_version" not in result
340+
assert result["cache_key"] == "shared"

0 commit comments

Comments
 (0)