When you paste a snippet or refactor a build script, you’re holding a sharp knife. In 2026 that knife is sometimes AI-sharpened. This post is about putting a guard on it—without paying the container tax.
Rye now ships a lightweight sandbox based on Linux namespaces (aka “unshare”). It’s one flag, zero root, and integrates with Rye’s standard library so your script can detect and adapt to isolation.
TL;DR
rye --unshareruns your script inside fresh namespaces (fs/net/pid/uts) via re-exec.- No Docker/Podman: no images, daemons, or root; startup is fast.
- Project policy can require unshare; scripts can check it at runtime.
- Ideal for CI, running third-party code, and guardrailing day-to-day data tasks.
Why another sandbox? (The missing middle)
We’ve had two extremes:
- Nothing: run the script raw; hope it doesn’t write to your repo or phone home.
- Everything: containerize; build/pull images; juggle a daemon; accept overhead.
Rye’s unshare aims at the middle: strong enough to reduce blast radius, simple enough for a one-off script. It’s not a substitute for full container orchestration—but it removes excuses for unsafe defaults.
What --unshare actually does
Under the hood, Rye re-execs itself with Linux clone flags, then pivots into a small jail before your code runs:
- Filesystem: bind-mounts your current directory read-only as
/app, thenpivot_rootinto a tmpfs jail. Relative paths still work from/app. - Network: optional new network namespace (default: no network).
- PID: new PID namespace (hides host processes).
- UTS: new hostname namespace.
You can toggle dimensions with flags:
rye --unshare myscript.rye # full isolation
rye --unshare --unshare-net=false . # allow network, keep fs/pid/uts
rye --unshare --unshare-fs=false . # keep your full FS, isolate others
Or via policy in .ryesec (local, per-project):
unshare:
enabled: true
fs: true
net: true
pid: true
uts: true
Detect and adapt in code
Scripts can self-report their posture using Rye-itself builtins:
cc Rye-itself
cfg: Unshare-config? ; {active: true, fs: true, net: true, pid: true, uts: true}
if (cfg -> "active") {
print "Running sandboxed; no network expected."
} else {
print "Not sandboxed; consider: rye --unshare ."
}
Quick checks:
Is-unshare— inside sandbox?Is-unshare-fs/-net/-pid/-uts— per-namespace booleansRequire-unshared— fail fast if sandbox is required (great for CI)
cc Rye-itself
Require-unshared ; exits with error if not unshared
Real problems this solves
- Accidental writes: project dir is read-mostly in the jail; write to
/tmpdeliberately. - Hidden network: unsolicited HTTP calls or telemetry fail by default.
- Reproducibility: scripts see a curated filesystem; fewer environment surprises.
- Safer third-party code: reduce blast radius for examples and AI-generated snippets.
A 5-minute adopt plan (teams/CI)
- Commit a
.ryesecwith unshare enabled (above). - Add this to your CI boot:
cc Rye-itself
Require-unshared
- Document how to temporarily lift a single dimension locally, e.g.
--unshare-net=falsewhen a test legitimately fetches data.
Result: safer-by-default runs without new infra.
How is this different from containers or bwrap?
- Docker/Podman: powerful and portable, but heavy for casual scripts. You pay in images, daemons, permissions, and cognitive load.
- bubblewrap/firejail: excellent tools, but external. Rye integrates isolation with the language runtime and offers in-language posture checks.
Rye’s niche: “I need guardrails right now for this script,” not “I’m deploying a service.”
Caveats (be honest)
- Linux-only: gracefully degrade elsewhere (your script can branch on
Is-unshare). - RO remount is best-effort: on some kernels
/appmay remain writable; Rye warns. Treat/appas read-mostly and prefer/tmpfor outputs. - Isolation is decided at startup: no mid-run switch into namespaces.
- PID namespace: tooling that expects host PIDs will see the namespaced view.
Outside-the-box use-cases
- Disposable playpens: pair
--unsharewith temp projects; archive/tmpafter runs. - Canary runs: compare outputs unshared vs. normal to surface side-effects.
- Teaching: workshop repos ship with
.ryesec; learners can’t break their host. - Progressive hardening: combine unshare with Landlock; later add seccomp.
Minimal working demo
; Save as safe_fetch.rye
cc Rye-itself
if Is-unshare-net { print "Network disabled; skipping fetch." return }
cc batteries
url: "https://example.com"
http-get url |print
Run:
rye --unshare safe_fetch.rye # prints: Network disabled; skipping fetch.
rye --unshare --unshare-net=false safe_fetch.rye # performs the fetch
Why this fits Rye’s personality
Rye prizes readable scripts, value-level errors, and small, composable primitives. Unshare follows that: one flag to isolate, a few words to detect/require, and you’re still writing normal Rye.
TL;DR (again)
- Safer-by-default scripting with one flag, zero root.
- No container ceremony for everyday tasks.
- Detectable and enforceable in-language.
If you’ve ever hesitated to run a snippet, try rye --unshare first. It turns sharp knives into safer tools—without dulling them.