Skip to content

Commit 549b89a

Browse files
committed
feat: enable sccache for cargo-pgrx builds in GitHub Actions
Speed up rust builds by enabling sccache caching for cargo-pgrx builds in GitHub Actions. We mount a persistent disk at /var/cache/sccache and configure sccache to use it.
1 parent f5be8f5 commit 549b89a

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ inputs:
55
description: 'Whether to push build outputs to the Nix binary cache'
66
required: false
77
default: 'false'
8+
enable-sccache-sandbox-path:
9+
description: 'Whether to expose /nix/var/cache/sccache in the Nix sandbox'
10+
required: false
11+
default: 'false'
812
aws-region:
913
description: 'AWS region for the Nix binary cache S3 bucket'
1014
required: false
@@ -48,4 +52,5 @@ runs:
4852
substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com
4953
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
5054
${{ inputs.push-to-cache == 'true' && 'post-build-hook = /etc/nix/upload-to-cache.sh' || '' }}
55+
${{ inputs.enable-sccache-sandbox-path == 'true' && 'extra-sandbox-paths = /nix/var/cache/sccache?' || '' }}
5156
max-jobs = 4

.github/workflows/nix-build.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,31 @@ jobs:
3636
- name: Checkout Repo
3737
if: ${{ matrix.attr != '' }}
3838
uses: actions/checkout@v4
39+
- name: Mount sccache disk
40+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
41+
uses: useblacksmith/stickydisk@v1
42+
with:
43+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
44+
path: /nix/var/cache/sccache
3945
- name: Install nix (ephemeral)
4046
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
4147
uses: ./.github/actions/nix-install-ephemeral
4248
with:
4349
push-to-cache: 'true'
50+
enable-sccache-sandbox-path: 'true'
4451
env:
4552
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
4653
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
4754
- name: Install nix (self-hosted)
4855
if: ${{ matrix.attr != '' && matrix.runs_on.group == 'self-hosted-runners-nix' }}
4956
uses: ./.github/actions/nix-install-self-hosted
57+
- name: Allow sccache cache write access
58+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
59+
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
5064
- name: nix build
5165
if: ${{ matrix.attr != '' }}
5266
shell: bash
@@ -67,17 +81,31 @@ jobs:
6781
- name: Checkout Repo
6882
if: ${{ matrix.attr != '' }}
6983
uses: actions/checkout@v4
84+
- name: Mount sccache disk
85+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
86+
uses: useblacksmith/stickydisk@v1
87+
with:
88+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
89+
path: /nix/var/cache/sccache
7090
- name: Install nix (ephemeral)
7191
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
7292
uses: ./.github/actions/nix-install-ephemeral
7393
with:
7494
push-to-cache: 'true'
95+
enable-sccache-sandbox-path: 'true'
7596
env:
7697
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
7798
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
7899
- name: Install nix (self-hosted)
79100
if: ${{ matrix.attr != '' && matrix.runs_on.group == 'self-hosted-runners-nix' }}
80101
uses: ./.github/actions/nix-install-self-hosted
102+
- name: Allow sccache cache write access
103+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
104+
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
81109
- name: nix build
82110
if: ${{ matrix.attr != '' }}
83111
shell: bash
@@ -144,14 +172,28 @@ jobs:
144172
- name: Checkout Repo
145173
if: ${{ matrix.attr != '' }}
146174
uses: actions/checkout@v4
175+
- name: Mount sccache disk
176+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
177+
uses: useblacksmith/stickydisk@v1
178+
with:
179+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
180+
path: /nix/var/cache/sccache
147181
- name: Install nix
148182
if: ${{ matrix.attr != '' }}
149183
uses: ./.github/actions/nix-install-ephemeral
150184
with:
185+
enable-sccache-sandbox-path: 'true'
151186
push-to-cache: 'true'
152187
env:
153188
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
154189
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
190+
- name: Allow sccache cache write access
191+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
192+
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
155197
- name: nix build
156198
if: ${{ matrix.attr != '' }}
157199
shell: bash
@@ -172,14 +214,28 @@ jobs:
172214
- name: Checkout Repo
173215
if: ${{ matrix.attr != '' }}
174216
uses: actions/checkout@v4
217+
- name: Mount sccache disk
218+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
219+
uses: useblacksmith/stickydisk@v1
220+
with:
221+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
222+
path: /nix/var/cache/sccache
175223
- name: Install nix
176224
if: ${{ matrix.attr != '' }}
177225
uses: ./.github/actions/nix-install-ephemeral
178226
with:
227+
enable-sccache-sandbox-path: 'true'
179228
push-to-cache: 'true'
180229
env:
181230
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
182231
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
232+
- name: Allow sccache cache write access
233+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
234+
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
183239
- name: nix build
184240
if: ${{ matrix.attr != '' }}
185241
shell: bash

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
darwin,
3636
writeShellScriptBin,
3737
defaultBindgenHook,
38+
sccache,
3839
}:
3940

4041
# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
@@ -56,7 +57,7 @@
5657
postgresql,
5758
# enable override to generate bindings using bindgenHook.
5859
# Some older versions of cargo-pgrx use a bindgenHook that is not compatible with the
59-
# current clang version present in stdenv
60+
# current clang version present in stdenv
6061
bindgenHook ? defaultBindgenHook,
6162
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
6263
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
@@ -135,12 +136,22 @@ let
135136
postgresql
136137
pkg-config
137138
bindgenHook
139+
sccache
138140
]
139141
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
140142

141143
buildPhase = ''
142144
runHook preBuild
143145
146+
if [[ -d "/nix/var/cache/sccache" && -w "/nix/var/cache/sccache" ]]; then
147+
echo "sccache: cache directory available, enabling"
148+
export RUSTC_WRAPPER="${sccache}/bin/sccache"
149+
export SCCACHE_DIR="/nix/var/cache/sccache"
150+
export SCCACHE_CACHE_SIZE="500G"
151+
else
152+
echo "sccache: cache directory not available, skipping"
153+
fi
154+
144155
echo "Executing cargo-pgrx buildPhase"
145156
${preBuildAndTest}
146157
${maybeEnterBuildAndTestSubdir}
@@ -154,6 +165,11 @@ let
154165
--features "${builtins.concatStringsSep " " buildFeatures}" \
155166
--out-dir "$out"
156167
168+
if [[ -n "''${RUSTC_WRAPPER:-}" ]]; then
169+
echo "sccache stats:"
170+
${sccache}/bin/sccache --show-stats
171+
fi
172+
157173
${maybeLeaveBuildAndTestSubdir}
158174
159175
runHook postBuild

0 commit comments

Comments
 (0)