Skip to content

Commit 1cc79e1

Browse files
jfrocheyvan-sraka
authored andcommitted
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 21338c8 commit 1cc79e1

3 files changed

Lines changed: 78 additions & 17 deletions

File tree

.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 & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ permissions:
1414
contents: write
1515
packages: write
1616

17-
concurrency:
18-
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
19-
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
20-
2117
jobs:
2218
nix-eval:
2319
uses: ./.github/workflows/nix-eval.yml
@@ -40,17 +36,31 @@ jobs:
4036
- name: Checkout Repo
4137
if: ${{ matrix.attr != '' }}
4238
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
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
4345
- name: Install nix (ephemeral)
4446
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
4547
uses: ./.github/actions/nix-install-ephemeral
4648
with:
4749
push-to-cache: 'true'
50+
enable-sccache-sandbox-path: 'true'
4851
env:
4952
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
5053
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
5154
- name: Install nix (self-hosted)
5255
if: ${{ matrix.attr != '' && matrix.runs_on.group == 'self-hosted-runners-nix' }}
5356
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
5464
- name: nix build
5565
if: ${{ matrix.attr != '' }}
5666
shell: bash
@@ -71,17 +81,31 @@ jobs:
7181
- name: Checkout Repo
7282
if: ${{ matrix.attr != '' }}
7383
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
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
7490
- name: Install nix (ephemeral)
7591
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
7692
uses: ./.github/actions/nix-install-ephemeral
7793
with:
7894
push-to-cache: 'true'
95+
enable-sccache-sandbox-path: 'true'
7996
env:
8097
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
8198
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
8299
- name: Install nix (self-hosted)
83100
if: ${{ matrix.attr != '' && matrix.runs_on.group == 'self-hosted-runners-nix' }}
84101
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
85109
- name: nix build
86110
if: ${{ matrix.attr != '' }}
87111
shell: bash
@@ -148,14 +172,28 @@ jobs:
148172
- name: Checkout Repo
149173
if: ${{ matrix.attr != '' }}
150174
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
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
151181
- name: Install nix
152182
if: ${{ matrix.attr != '' }}
153183
uses: ./.github/actions/nix-install-ephemeral
154184
with:
185+
enable-sccache-sandbox-path: 'true'
155186
push-to-cache: 'true'
156187
env:
157188
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
158189
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
159197
- name: nix build
160198
if: ${{ matrix.attr != '' }}
161199
shell: bash
@@ -176,14 +214,28 @@ jobs:
176214
- name: Checkout Repo
177215
if: ${{ matrix.attr != '' }}
178216
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
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
179223
- name: Install nix
180224
if: ${{ matrix.attr != '' }}
181225
uses: ./.github/actions/nix-install-ephemeral
182226
with:
227+
enable-sccache-sandbox-path: 'true'
183228
push-to-cache: 'true'
184229
env:
185230
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
186231
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
187239
- name: nix build
188240
if: ${{ matrix.attr != '' }}
189241
shell: bash
@@ -217,15 +269,3 @@ jobs:
217269
(needs.nix-build-packages-x86_64-linux.result == 'skipped' || needs.nix-build-packages-x86_64-linux.result == 'success') &&
218270
(needs.nix-build-checks-x86_64-linux.result == 'skipped' || needs.nix-build-checks-x86_64-linux.result == 'success')
219271
uses: ./.github/workflows/test.yml
220-
221-
docker-image-test:
222-
needs: [nix-eval, nix-build-packages-aarch64-linux, nix-build-checks-aarch64-linux]
223-
if: |
224-
!cancelled() &&
225-
needs.nix-eval.result == 'success' &&
226-
(needs.nix-build-packages-aarch64-linux.result == 'skipped' || needs.nix-build-packages-aarch64-linux.result == 'success') &&
227-
(needs.nix-build-checks-aarch64-linux.result == 'skipped' || needs.nix-build-checks-aarch64-linux.result == 'success')
228-
uses: ./.github/workflows/docker-image-test.yml
229-
secrets:
230-
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
231-
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
stdenv,
3535
writeShellScriptBin,
3636
defaultBindgenHook,
37+
sccache,
3738
}:
3839

3940
# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
@@ -55,7 +56,7 @@
5556
postgresql,
5657
# enable override to generate bindings using bindgenHook.
5758
# Some older versions of cargo-pgrx use a bindgenHook that is not compatible with the
58-
# current clang version present in stdenv
59+
# current clang version present in stdenv
5960
bindgenHook ? defaultBindgenHook,
6061
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
6162
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
@@ -157,12 +158,22 @@ let
157158
postgresql
158159
pkg-config
159160
bindgenHook
161+
sccache
160162
]
161163
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
162164

163165
buildPhase = ''
164166
runHook preBuild
165167
168+
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="500G"
173+
else
174+
echo "sccache: cache directory not available, skipping"
175+
fi
176+
166177
echo "Executing cargo-pgrx buildPhase"
167178
${preBuildAndTest}
168179
${maybeEnterBuildAndTestSubdir}
@@ -183,6 +194,11 @@ let
183194
--features "${builtins.concatStringsSep " " buildFeatures}" \
184195
--out-dir "$out"
185196
197+
if [[ -n "''${RUSTC_WRAPPER:-}" ]]; then
198+
echo "sccache stats:"
199+
${sccache}/bin/sccache --show-stats
200+
fi
201+
186202
${maybeLeaveBuildAndTestSubdir}
187203
188204
runHook postBuild

0 commit comments

Comments
 (0)