Guides

AI + Nix: Run Anything, Install Nothing

Use Nix to give AI agents full host access to any software — including graphical applications — without permanently installing anything. Run ad-hoc, garbage collect when done.

9 min read Nix AI nix run nix shell
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" /> Warning

Warning: The techniques in this guide apply with or without AI — AI is just the most common entry point today. Nix does not sandbox execution: ad-hoc programs run with your user permissions.
-> AI is a tool, not an authority — you are responsible for every action on your system and the resulting consequences.
-> Review commands before running them.

The problem: AI needs software, hosts need hygiene

AI coding agents are increasingly capable. They can write code, run commands, debug issues, and orchestrate complex workflows. But they hit a wall the moment they need a tool that is not installed: a linter for an unfamiliar language, a database client, an image editor, a network diagnostic tool.

The traditional options are bad:

  • Install it permanently — pollutes the host with packages the user never asked for and may never need again.
  • Run it in a container — isolates the tool from the host, breaking access to local files, GPU, display server, and other resources the AI often needs.
  • Ask the user to install it — breaks the flow and defeats the purpose of autonomous agents.

Nix offers a fourth option: run anything ad-hoc, directly on the host, and garbage collect it later. The AI gets full host access. The host stays clean.

How ad-hoc execution works

Nix can run any of its 120,000+ packages without installing them in the traditional sense. The binaries land in /nix/store — an immutable, content-addressed directory — and are available immediately. No apt-get, no brew, no sudo, no permanent changes to PATH.

nix run: execute and exit

nix run fetches a package, builds or downloads it, and runs its default binary in a single command:

terminal
$ nix run nixpkgs#cowsay -- "Hello from Nix"
 ________________
< Hello from Nix >
 ----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

The binary is cached in /nix/store. Nothing is added to your profile or PATH. Run it again and it starts instantly from cache.

nix shell: get a temporary environment

nix shell drops you into a shell (or runs a command) with one or more packages available:

terminal
$ nix shell nixpkgs#ffmpeg nixpkgs#imagemagick
$ ffmpeg -version
ffmpeg version 7.1 ...
$ convert --version
Version: ImageMagick 7.1.1 ...
$ exit
$ ffmpeg -version
zsh: command not found: ffmpeg

The packages exist only for the duration of the shell session. After exit, they are gone from PATH — though still cached in the store for instant reuse.

Graphical applications work too

Nix packages include desktop applications with full access to the host display server, GPU, audio, and filesystem:

terminal
$ nix run nixpkgs#firefox
$ nix run nixpkgs#gimp
$ nix run nixpkgs#blender
$ nix run nixpkgs#libreoffice

These are not sandboxed or containerized. They run as native processes on the host with the same permissions as the user. They can open local files, access the clipboard, use hardware acceleration — everything a normally installed application can do.

Garbage collection: reclaim disk space on demand

Every nix run and nix shell invocation caches its packages in /nix/store. Over time, this cache grows. Nix provides precise garbage collection to reclaim space:

terminal
# Remove packages not referenced by any profile or GC root
$ nix-collect-garbage

# Remove everything older than 30 days
$ nix-collect-garbage --delete-older-than 30d

# Remove all old generations, then collect
$ nix-collect-garbage -d

Garbage collection is safe. Nix traces references from your active profiles and GC roots. Anything not reachable is removed. Anything still needed stays. There is no risk of breaking running software.

Check how much space the store uses:

terminal
$ nix store info
$ du -sh /nix/store

Why this matters for AI

AI agents that can invoke shell commands gain a practical superpower with Nix: they can use any software without asking permission to install it and without leaving a mess behind.

What this looks like in practice

An AI coding agent working on a project might need to:

  1. Lint unfamiliar code — The project has a Haskell file. The agent does not need Haskell installed:
terminal
$ nix run nixpkgs#hlint -- src/Parser.hs
  1. Process images — A task requires resizing screenshots. No need to permanently install ImageMagick:
terminal
$ nix shell nixpkgs#imagemagick -c convert input.png -resize 50% output.png
  1. Inspect a database — The agent needs to check a PostgreSQL schema:
terminal
$ nix shell nixpkgs#postgresql -c psql -h localhost -U dev -d myapp -c '\dt'
  1. Generate diagrams — Documentation needs architecture diagrams from Graphviz DOT files:
terminal
$ nix shell nixpkgs#graphviz -c dot -Tpng architecture.dot -o architecture.png
  1. Open a GUI application — The agent needs to verify a PDF renders correctly:
terminal
$ nix run nixpkgs#evince -- report.pdf
  1. Run a language runtime — A quick Python script is the fastest way to solve a data transformation:
terminal
$ nix shell nixpkgs#python3 -c python3 transform.py

In every case, the tool appears instantly (or after a one-time download and build), runs with full host access, and leaves nothing behind once garbage collected.

The container trap

Containers are the default answer to “run something without installing it,” but they introduce friction that undermines AI agents:

ConcernContainerNix ad-hoc
Host filesystem accessRequires explicit mountsFull access by default
Display server (GUI)Complex X11/Wayland forwardingNative, zero config
GPU accelerationNeeds runtime flags and driversWorks transparently
Startup overheadImage pull + container creationInstant from cache
Compose with other toolsIsolated environmentSame shell, same PATH
Cleanupdocker system prunenix-collect-garbage

Containers are excellent for deployment isolation. But for an AI agent that needs to use software on a developer’s workstation, Nix’s ad-hoc execution is a better fit. The agent operates as an extension of the user, not inside a sealed box.

Security model

<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" /> Warning

Warning: Nix ad-hoc execution runs programs with the current user’s permissions — the same as any other program the user runs. This may have serious security implications: AI agents can read project files, write output, and access local services.

The Nix store itself is read-only and content-addressed. Packages cannot be tampered with after they are built. Every store path includes a cryptographic hash of all inputs. If you fetch the same package twice, you get the same bytes.

Setting up Nix for ad-hoc use

If Nix is not yet installed, a single command sets it up. This installs the Determina Nix package manager with reasonable defaults.

terminal
$ curl -sSf -L https://getnix.io/install | sh -s -- install

Verify it works:

terminal
$ nix run nixpkgs#hello
Hello, world!

That is all the setup needed. No package lists to maintain, no profiles to manage. Any of the 120,000+ packages in nixpkgs is now one command away.

Combining ad-hoc packages in a pipeline

Nix shell sessions can compose multiple packages for complex operations:

terminal
$ nix shell nixpkgs#ffmpeg nixpkgs#whisper-ctranslate2 -c bash -c '
  ffmpeg -i meeting.mp4 -vn -ar 16000 -ac 1 audio.wav
  whisper audio.wav --model medium --output_format txt
'

This extracts audio from a video and transcribes it using Whisper — neither tool needs to be installed. An AI agent can construct pipelines like this dynamically, pulling in whatever tools the task requires.

Example: Claude Code with Nix

Claude Code is Anthropic’s CLI agent that runs shell commands on your machine. By default it can only use tools already installed on the host. With Nix available, Claude gains access to the entire nixpkgs package set — without installing anything permanently.

Instructing Claude to use Nix

Add a CLAUDE.md file to your project (or your home directory for global instructions) telling Claude that Nix is available and how to use it:

text
# Tools

Nix is installed on this system. When you need a tool that is not
available, use `nix run nixpkgs#<package>` or `nix shell nixpkgs#<package>`
to run it ad-hoc. Do not install packages permanently.

Search for packages with `nix search nixpkgs <term>`.

That is all it takes. Claude reads CLAUDE.md at the start of every session and will use nix run / nix shell whenever it needs software that is not on the PATH.

What a session looks like

You ask Claude to generate a diagram from a Graphviz DOT file in your project. Claude does not have Graphviz installed, but Nix is available:

text
You: Generate a PNG from docs/architecture.dot

Claude: I'll render the DOT file to PNG using Graphviz via Nix.

  $ nix shell nixpkgs#graphviz -c dot -Tpng docs/architecture.dot -o docs/architecture.png

  Created docs/architecture.png (2048x1536, 184 KB).

Or you ask Claude to optimize images. It reaches for multiple tools at once:

text
You: Compress all PNGs in static/ losslessly

Claude: I'll use OptiPNG via Nix to compress them in place.

  $ nix shell nixpkgs#optipng -c bash -c 'for f in static/*.png; do optipng -o5 "$f"; done'

  static/og.png: 142 KB → 118 KB (17% reduction)
  static/hero.png: 89 KB → 74 KB (17% reduction)
  static/logo.png: 12 KB → 11 KB (8% reduction)

Claude can also open graphical applications when it needs visual verification:

text
You: Check if the PDF report renders correctly

Claude: I'll open the PDF with Evince via Nix so you can inspect it.

  $ nix run nixpkgs#evince -- output/report.pdf

In each case, Claude finds and uses the right tool without any prior installation. The tools are cached in /nix/store for instant reuse and cleaned up by garbage collection when no longer needed.

Automatic cleanup with scheduled garbage collection

For hosts where AI agents run frequently, automate garbage collection so the store does not grow unbounded:

terminal
# systemd timer (Linux) — collect garbage weekly
$ cat > ~/.config/systemd/user/nix-gc.service << 'EOF'
[Unit]
Description=Nix garbage collection

[Service]
Type=oneshot
ExecStart=/nix/var/nix/profiles/default/bin/nix-collect-garbage --delete-older-than 7d
EOF

$ cat > ~/.config/systemd/user/nix-gc.timer << 'EOF'
[Unit]
Description=Weekly Nix garbage collection

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target
EOF

$ systemctl --user enable --now nix-gc.timer

On NixOS, this is a one-liner in your system configuration:

nix
nix.gc = {
  automatic = true;
  dates = "weekly";
  options = "--delete-older-than 7d";
};

Quick reference

CommandWhat it does
nix run nixpkgs#<pkg>Run a package’s default binary
nix run nixpkgs#<pkg> -- <args>Run with arguments
nix shell nixpkgs#<pkg1> nixpkgs#<pkg2>Open a shell with multiple packages
nix shell nixpkgs#<pkg> -c <cmd>Run a single command with the package available
nix-collect-garbageRemove unreferenced store paths
nix-collect-garbage -dDelete all old generations, then collect
nix-collect-garbage --delete-older-than 30dRemove store paths older than 30 days
nix store infoShow store statistics
nix search nixpkgs <term>Search available packages
nix path-info -shr nixpkgs#<pkg>Show package size including dependencies