Skip to content

Commit f186dc7

Browse files
feat(nix): build dprint from source in a bubblewrap sandbox (#1)
* feat(nix): build dprint from source in a bubblewrap sandbox Add a flake that builds the CLI from this tree and, on Linux, wraps it in a bubblewrap sandbox that exposes only /nix/store, a private tmpfs /tmp, a synthetic home, the project root and dprint's cache directory read-write. The host environment is cleared and PATH is rebuilt from /nix/store entries. Network is on by default for any plugin specifier; DPRINT_OFFLINE=1 turns it off and makes the cache read-only. Paths that are empty, /, the home directory or outside [A-Za-z0-9_/.-] are refused. A conformance script proves the sandbox in CI, and a main-branch workflow pushes dprint and dprint-unwrapped to the shared binary cache. * fix(nix): resolve the project root with the store's git, not the caller's PATH --------- Co-authored-by: systemfsoftware-bot <318394648+systemfsoftware-bot@users.noreply.github.com>
1 parent 3c91533 commit f186dc7

8 files changed

Lines changed: 820 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
name: Nix cache push
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
permissions:
8+
contents: read
9+
10+
env:
11+
NIX_CACHE_URL: https://nix.systemfsoftware.com
12+
NIX_CACHE_PUBLIC_KEY: nix-cache-1:wXA8I4DayNW9q6enUyQA/P/nT38pGSTLt374t3gsn7M=
13+
EXTRA_OUTPUTS: >-
14+
dprint
15+
dprint-unwrapped
16+
17+
jobs:
18+
push:
19+
name: push · ${{ matrix.system }}
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
include:
24+
- system: x86_64-linux
25+
runner: ubuntu-latest
26+
- system: aarch64-linux
27+
runner: ubuntu-24.04-arm
28+
runs-on: ${{ matrix.runner }}
29+
environment: nix-cache-push
30+
timeout-minutes: 90
31+
steps:
32+
- id: start
33+
name: Record the job start
34+
run: echo "at=$(date +%s)" >> "$GITHUB_OUTPUT"
35+
- name: The R2 push values are set
36+
env:
37+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
38+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
39+
NIX_CACHE_R2_ENDPOINT: ${{ secrets.NIX_CACHE_R2_ENDPOINT }}
40+
run: |
41+
set -euo pipefail
42+
missing=0
43+
for name in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY NIX_CACHE_R2_ENDPOINT; do
44+
if [ -z "${!name}" ]; then
45+
echo "::error title=nix-cache-push::${name} is not set in the nix-cache-push environment, so nothing was built or pushed"
46+
missing=1
47+
fi
48+
done
49+
exit "$missing"
50+
- uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa
51+
with:
52+
extra-conf: |
53+
access-tokens = github.com=${{ github.token }}
54+
extra-substituters = ${{ env.NIX_CACHE_URL }}
55+
extra-trusted-public-keys = ${{ env.NIX_CACHE_PUBLIC_KEY }}
56+
57+
- name: The signing key is the cache's key
58+
env:
59+
NIX_CACHE_SIGNING_KEY: ${{ secrets.NIX_CACHE_SIGNING_KEY }}
60+
run: |
61+
set -euo pipefail
62+
: "${NIX_CACHE_SIGNING_KEY:?NIX_CACHE_SIGNING_KEY is not set}"
63+
umask 077
64+
printf '%s' "$NIX_CACHE_SIGNING_KEY" > "$RUNNER_TEMP/signing-key"
65+
derived=$(nix --extra-experimental-features nix-command key convert-secret-to-public < "$RUNNER_TEMP/signing-key")
66+
if [ "$derived" != "$NIX_CACHE_PUBLIC_KEY" ]; then
67+
echo "::error title=nix-cache-key::NIX_CACHE_SIGNING_KEY does not derive the cache's public key; nothing was pushed"
68+
exit 1
69+
fi
70+
71+
- name: Build the push set from the pushed commit
72+
env:
73+
SYSTEM: ${{ matrix.system }}
74+
run: |
75+
set -euo pipefail
76+
pinned="github:${GITHUB_REPOSITORY}/${GITHUB_SHA}"
77+
read -ra extras <<< "$EXTRA_OUTPUTS"
78+
mapfile -t attrs < <(printf '%s\n' "${extras[@]}" | awk '!seen[$0]++')
79+
: > "$RUNNER_TEMP/out-paths"
80+
: > "$RUNNER_TEMP/build-failures"
81+
build() {
82+
if out=$(nix build --no-link --print-out-paths -L "${pinned}#$1"); then
83+
printf '%s\n' "$out" >> "$RUNNER_TEMP/out-paths"
84+
else
85+
echo "$1" >> "$RUNNER_TEMP/build-failures"
86+
echo "::error title=nix-build-failed::$1 did not build"
87+
fi
88+
}
89+
for attr in "${attrs[@]}"; do
90+
build "packages.${SYSTEM}.${attr}"
91+
done
92+
93+
- name: Push the closures and check the cache serves them
94+
env:
95+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
96+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
97+
NIX_CACHE_R2_ENDPOINT: ${{ secrets.NIX_CACHE_R2_ENDPOINT }}
98+
run: |
99+
set -euo pipefail
100+
echo "::add-mask::${NIX_CACHE_R2_ENDPOINT#https://}"
101+
mapfile -t outs < "$RUNNER_TEMP/out-paths"
102+
if [ "${#outs[@]}" -eq 0 ]; then
103+
echo "::error title=nix-cache-push::nothing built, so nothing was pushed"
104+
exit 1
105+
fi
106+
cat > "$RUNNER_TEMP/check-served.sh" << 'EOF'
107+
path=$1
108+
name=${path##*/}
109+
body=$(mktemp)
110+
code=$(curl -sS -m 30 --retry 2 -o "$body" -w "%{http_code}" "$NIX_CACHE_URL/${name%%-*}.narinfo" 2> /dev/null) || code=unreachable
111+
if [ "$code" != 200 ]; then
112+
echo "$path: HTTP $code" >> "$MISSES"
113+
elif ! grep -q "^Sig: nix-cache-1:" "$body"; then
114+
echo "$path: no signature from nix-cache-1" >> "$MISSES"
115+
fi
116+
rm -f "$body"
117+
EOF
118+
failed=0
119+
if nix copy --to "s3://systemfsoftware-nix-cache?endpoint=${NIX_CACHE_R2_ENDPOINT#https://}&region=auto&scheme=https&compression=zstd&multipart-upload=true&secret-key=${RUNNER_TEMP}/signing-key" "${outs[@]}" 2> "$RUNNER_TEMP/copy.err"; then
120+
export NIX_CACHE_URL MISSES="$RUNNER_TEMP/cache-misses"
121+
: > "$MISSES"
122+
nix-store --query --requisites "${outs[@]}" > "$RUNNER_TEMP/pushed-paths"
123+
xargs -P 16 -n 1 bash "$RUNNER_TEMP/check-served.sh" < "$RUNNER_TEMP/pushed-paths"
124+
if [ -s "$MISSES" ]; then
125+
failed=1
126+
head -n 10 "$MISSES" | while read -r miss; do
127+
echo "::error title=nix-cache-verify::${miss}"
128+
done
129+
{
130+
echo "The cache does not serve, or has not signed, these pushed paths:"
131+
sed 's/^/- /' "$MISSES"
132+
} >> "$GITHUB_STEP_SUMMARY"
133+
else
134+
echo "Every one of the $(wc -l < "$RUNNER_TEMP/pushed-paths") pushed paths is served with a nix-cache-1 signature." >> "$GITHUB_STEP_SUMMARY"
135+
fi
136+
else
137+
failed=1
138+
reason=$(sed -n '/error: ./{s/^[[:space:]]*//;p;q}' "$RUNNER_TEMP/copy.err" | cut -c1-300)
139+
echo "::error title=nix-cache-push::the push failed: ${reason:-nix copy failed}"
140+
echo "The push failed: ${reason:-nix copy failed}" >> "$GITHUB_STEP_SUMMARY"
141+
fi
142+
if [ -s "$RUNNER_TEMP/build-failures" ]; then
143+
failed=1
144+
{
145+
echo "These did not build, so they were not pushed:"
146+
sed 's/^/- /' "$RUNNER_TEMP/build-failures"
147+
} >> "$GITHUB_STEP_SUMMARY"
148+
fi
149+
exit "$failed"
150+
151+
- name: Remove the signing key
152+
if: always()
153+
run: rm -f "$RUNNER_TEMP/signing-key" "$RUNNER_TEMP/copy.err"
154+
155+
- name: Record the job's wall time
156+
if: always()
157+
env:
158+
START: ${{ steps.start.outputs.at }}
159+
SYSTEM: ${{ matrix.system }}
160+
run: |
161+
echo "nix cache push wall time (${SYSTEM}): $(( $(date +%s) - START ))s" >> "$GITHUB_STEP_SUMMARY"

‎.github/workflows/sandbox.yml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Sandbox conformance
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
14+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
15+
16+
jobs:
17+
conformance:
18+
name: conformance · ${{ matrix.system }}
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
include:
23+
- system: x86_64-linux
24+
runner: ubuntu-latest
25+
- system: aarch64-linux
26+
runner: ubuntu-24.04-arm
27+
runs-on: ${{ matrix.runner }}
28+
timeout-minutes: 90
29+
steps:
30+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
31+
with:
32+
persist-credentials: false
33+
- uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa
34+
with:
35+
extra-conf: |
36+
access-tokens = github.com=${{ github.token }}
37+
extra-substituters = https://nix.systemfsoftware.com
38+
extra-trusted-public-keys = nix-cache-1:wXA8I4DayNW9q6enUyQA/P/nT38pGSTLt374t3gsn7M=
39+
- name: Permit unprivileged user namespaces for bubblewrap
40+
run: |
41+
set -euo pipefail
42+
if [ -e /proc/sys/kernel/apparmor_restrict_unprivileged_userns ]; then
43+
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
44+
fi
45+
- name: Flake evaluates
46+
if: matrix.system == 'x86_64-linux'
47+
run: nix flake check
48+
- name: Run the conformance check
49+
run: ./scripts/sandbox-conformance.sh "${{ matrix.system }}"
50+
- name: Evaluate the Darwin output
51+
if: matrix.system == 'x86_64-linux'
52+
run: nix eval --raw .#packages.aarch64-darwin.dprint.drvPath > /dev/null

‎README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ Monorepo for dprint—a pluggable and configurable code formatting platform.
1212
- [Getting Started](https://dprint.dev/install)
1313
- [Playground](https://dprint.dev/playground)
1414

15+
## Nix flake
16+
17+
This repository ships a `flake.nix` that builds the CLI from this source and, on Linux, wraps it in a bubblewrap sandbox.
18+
19+
```sh
20+
nix run github:systemfsoftware/dprint
21+
nix build github:systemfsoftware/dprint#dprint
22+
```
23+
24+
Outputs:
25+
26+
- `dprint-unwrapped`: the source build of the CLI.
27+
- `dprint` (and `default`): on Linux, the same binary inside the sandbox; on Darwin, for now, the unwrapped binary.
28+
29+
On Linux the sandbox sees `/nix/store` read-only, a private tmpfs `/tmp`, a synthetic home directory, `/etc/resolv.conf`, `/etc/hosts` and `/etc/nsswitch.conf` when present, the project root read-write (the git toplevel when the working directory is inside a work tree, otherwise the working directory), and dprint's cache directory read-write (`$DPRINT_CACHE_DIR`, default `${XDG_CACHE_HOME:-$HOME/.cache}/dprint`). The host environment is cleared except for `TERM`, `LANG`, `LC_ALL`, `NO_COLOR`, `FORCE_COLOR` and proxy variables, and `PATH` is rebuilt from `/nix/store` entries only. Network is available by default so any `https:` or `npm:` plugin a `dprint.json` names resolves on a cold cache; set `DPRINT_OFFLINE=1` to run with no network and a read-only cache. A project or cache directory that is empty, `/`, the home directory, or contains characters outside `[A-Za-z0-9_/.-]` is refused rather than silently skipped.
30+
31+
There is no sandbox on Darwin yet: `dprint` is the unwrapped binary there. A macOS sandbox is a follow-up.
32+
33+
`scripts/sandbox-conformance.sh` proves the sandbox in CI, and `nix flake check` builds the probe that script runs.
34+
1535
## Plugins
1636

1737
See https://dprint.dev/plugins/

‎flake.lock‎

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)