@@ -11,6 +11,10 @@ inputs:
1111 description : " Enable sccache with GitHub Actions cache backend"
1212 required : false
1313 default : " false"
14+ gc-max-store-size :
15+ description : " Max Nix store size before garbage collection (e.g. 5G, 10G). Only applies on WarpBuild runners."
16+ required : false
17+ default : " 8G"
1418runs :
1519 using : " composite"
1620 steps :
@@ -19,17 +23,99 @@ runs:
1923 github_access_token : ${{ inputs.github-token }}
2024 extra_nix_config : |
2125 accept-flake-config = true
22- - name : Nix store cache (WarpBuild)
26+ - name : Check Nix store cache (WarpBuild)
27+ id : nix-cache-check
28+ if : startsWith(runner.name, 'warp-')
29+ uses : WarpBuilds/cache/restore@v1
30+ with :
31+ path : |
32+ /nix/store
33+ /nix/var/nix/db
34+ /nix/var/nix/profiles
35+ key : nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}-${{ github.job }}-${{ github.run_id }}
36+ restore-keys : |
37+ nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}-${{ github.job }}-
38+ nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}-
39+ nix-store-${{ runner.os }}-${{ runner.arch }}-
40+ lookup-only : true
41+ - name : Prepare Nix directories for cache restore
42+ if : startsWith(runner.name, 'warp-') && steps.nix-cache-check.outputs.cache-matched-key != ''
43+ run : |
44+ # Clear store so cache restore doesn't conflict with paths the
45+ # Nix installer already created. The cached archive includes
46+ # Nix itself, so it will be fully restored from cache.
47+ sudo rm -rf /nix/store /nix/var/nix/db /nix/var/nix/profiles
48+ # Recreate directories owned by runner user so tar can
49+ # write during restore and read during save
50+ sudo mkdir -p /nix/store /nix/var/nix/db /nix/var/nix/profiles
51+ sudo chown $USER /nix/store /nix/var/nix/db /nix/var/nix/profiles
52+ shell : bash
53+ - name : Restore Nix store cache (WarpBuild)
2354 if : startsWith(runner.name, 'warp-')
2455 uses : WarpBuilds/cache@v1
2556 with :
2657 path : |
2758 /nix/store
2859 /nix/var/nix/db
2960 /nix/var/nix/profiles
30- key : nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}
61+ key : nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}-${{ github.job }}-${{ github.run_id }}
3162 restore-keys : |
63+ nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}-${{ github.job }}-
64+ nix-store-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('flake.lock') }}-
3265 nix-store-${{ runner.os }}-${{ runner.arch }}-
66+ - name : Fix Nix directory ownership for cache save
67+ if : startsWith(runner.name, 'warp-') && steps.nix-cache-check.outputs.cache-matched-key == ''
68+ run : |
69+ # On cache miss the store is still root-owned from the installer.
70+ # Fix ownership so the post-step cache save can read all files.
71+ sudo chown -R $USER /nix/store /nix/var/nix/db /nix/var/nix/profiles
72+ shell : bash
73+ - name : Register post-step Nix store GC
74+ if : startsWith(runner.name, 'warp-')
75+ uses : webiny/action-post-run@3.1.0
76+ with :
77+ run : bash ${{ github.workspace }}/.github/scripts/nix-store-gc.sh ${{ inputs.gc-max-store-size }}
78+ - name : Write Nix GC script
79+ if : startsWith(runner.name, 'warp-')
80+ run : |
81+ mkdir -p "${{ github.workspace }}/.github/scripts"
82+ cat > "${{ github.workspace }}/.github/scripts/nix-store-gc.sh" << 'NIXGCEOF'
83+ #!/usr/bin/env bash
84+ set -euo pipefail
85+ parse_size() {
86+ local val="${1%[GgMmKk]*}"
87+ local unit="${1: -1}"
88+ case "$unit" in
89+ G|g) echo $(( val * 1073741824 )) ;;
90+ M|m) echo $(( val * 1048576 )) ;;
91+ K|k) echo $(( val * 1024 )) ;;
92+ *) echo "$1" ;;
93+ esac
94+ }
95+ THRESHOLD="${1:-8G}"
96+ THRESHOLD_BYTES=$(parse_size "$THRESHOLD")
97+ if du -sb /nix/store >/dev/null 2>&1; then
98+ STORE_BYTES=$(du -sb /nix/store | cut -f1)
99+ else
100+ STORE_BYTES=$(( $(du -sk /nix/store | cut -f1) * 1024 ))
101+ fi
102+ STORE_MB=$(( STORE_BYTES / 1048576 ))
103+ echo "Nix store size: ${STORE_MB}MB, threshold: $THRESHOLD"
104+ if [ "$STORE_BYTES" -gt "$THRESHOLD_BYTES" ]; then
105+ echo "Store exceeds threshold, running garbage collection..."
106+ nix store gc 2>&1 || true
107+ if du -sb /nix/store >/dev/null 2>&1; then
108+ NEW_BYTES=$(du -sb /nix/store | cut -f1)
109+ else
110+ NEW_BYTES=$(( $(du -sk /nix/store | cut -f1) * 1024 ))
111+ fi
112+ echo "Store size after GC: $(( NEW_BYTES / 1048576 ))MB"
113+ else
114+ echo "Store is within limit, skipping GC"
115+ fi
116+ NIXGCEOF
117+ chmod +x "${{ github.workspace }}/.github/scripts/nix-store-gc.sh"
118+ shell : bash
33119 - uses : cachix/cachix-action@v16
34120 if : ${{ inputs.cachix-auth-token != '' }}
35121 with :
0 commit comments