# agent-sandbox.nix Declarative sandboxing for AI agents on Linux and macOS. Stop your agents in YOLO mode from deleting your $HOME, force pushing to main, or publishing your ssh keys on reddit. The sandbox works with any CLI-based AI agent. The repository provides templates for common agent CLIs. The sandbox uses [bubblewrap](https://github.com/containers/bubblewrap) on Linux and sandbox-exec on macOS. See [Security](#security) for the threat model and the known limits. ## What the sandbox allows - **Project directory**: read/write access to the directory you launch the agent from, or to a fixed directory you set with `workspaceDir`. - **Declared state**: read/write access to anything you list in `rwDirs` / `rwFiles`, or read-only access through `roDirs` / `roFiles`. - **Allowed packages**: the binaries you list in `allowedPackages` are on the agent's PATH, together with `bash` and `cacert`. - **Network filtering**: open by default. Optionally filtered to `allowedDomains`. Ports are closed by default, but may be optionally exposed or connected to. - **Environment**: environment restricted to declared environment variables. - **Git**: git commands, including when launched within a worktree. - **Nix**: disabled by default. You can let the agent run nix commands. Everything else is denied. Only changes to the project directory and declared rwDirs/files are persisted - the agent's home directory and anything else it writes are discarded upon exit. ## Contents * [Quick start](#quick-start) * [Templates](#templates) * [Agent notes](#agent-notes) * [Arguments](#arguments) * [NixOS, Nix Darwin, or Home Manager](#nixos-nix-darwin-or-home-manager) * [Network restrictions](#network-restrictions) * [Domain and internet access](#domain-and-internet-access) * [Path rules](#path-rules) * [Host ports](#host-ports) * [Published ports](#published-ports) * [UNIX-domain sockets](#unix-domain-sockets) * [Authentication](#authentication) * [Environment variable tokens (recommended)](#environment-variable-tokens-recommended) * [Credential files via `rwDirs`](#credential-files-via-rwdirs) * [Git](#git) * [Setting your git identity](#setting-your-git-identity) * [What the sandbox exposes](#what-the-sandbox-exposes) * [Remote access (push / pull / fetch)](#remote-access-push--pull--fetch) * [Read-only paths in the git directory](#read-only-paths-in-the-git-directory) * [Using Nix inside the sandbox](#using-nix-inside-the-sandbox) * [Using headless browsers inside the sandbox](#using-headless-browsers-inside-the-sandbox) * [Sandbox configuration](#sandbox-configuration) * [Driver configuration](#driver-configuration) * [Troubleshooting](#troubleshooting) * [Session directories](#session-directories) * [Probe the sandbox interactively](#probe-the-sandbox-interactively) * [Deriving a network allowlist](#deriving-a-network-allowlist) * [macOS: system denial log](#macos-system-denial-log) * [macOS: gh and other Go tools](#macos-gh-and-other-go-tools) * [Security](#security) * [What it protects against](#what-it-protects-against) * [What it doesn't protect against](#what-it-doesnt-protect-against) * [Linux vs macOS](#linux-vs-macos) * [Is this the right tool for me?](#is-this-the-right-tool-for-me) * [Limitations](#limitations) * [Similar projects](#similar-projects) ## Quick start To get started quickly, use a flake template. If you do not use flakes, [`shells/claude.shell.nix`](shells/claude.shell.nix) is the same setup written as a plain `shell.nix`. The rest of [`shells/`](shells/) holds worked examples for narrower setups, linked from the sections they illustrate. ### Templates The following flake templates are provided for quick setup: | Template | Agent | Wrapped binary | | --- | --- | --- | | `claude` | Claude Code | `claude-sandboxed` | | `codex` | Codex | `codex-sandboxed` | | `copilot` | GitHub Copilot CLI | `copilot-sandboxed` | | `gemini` | Gemini | `gemini-sandboxed` | | `opencode` | OpenCode | `opencode-sandboxed` | | `pi` | Pi | `pi-sandboxed` | To initialize a template in your project directory: ```bash nix flake init -t github:archie-judd/agent-sandbox.nix#