# Nix packages

A box can run real [nixpkgs](https://github.com/NixOS/nixpkgs) programs: `jq`, `ripgrep`,
`sqlite`, `python3`, `git`, `curl` and the rest, the same binaries Nix users run, built by Hydra
and fetched from cache.nixos.org. They come from the `lifo-pkg-nix` package, which gives a box
`nix run`, `nix shell` and `nix-shell -p`. It's opt-in and in preview: nothing loads or downloads
until someone types `nix`, and the box itself stays what it always is, TypeScript with no kernel.

```bash
nix run nixpkgs#hello
nix shell nixpkgs#jq -c jq '[.orders[].total] | add' orders.json
nix run nixpkgs#ripgrep -- -n TODO notes
nix shell nixpkgs#miller -c mlr --icsv --opprint sort -nr units sales.csv
nix shell nixpkgs#sqlite -c sqlite3 shop.db < schema.sql
nix-shell -p cowsay --run 'cowsay hello from nixpkgs'
nix shell nixpkgs#python3                     # a shell with python3 on the path; exit to leave
```

The same package gives a box Alpine and Debian, with `apk` and `apt`, in the same Linux guest:
see [Linux packages](https://lifo.sh/docs/build/linux.md).

Every program reads and writes the box's files: the working directory, absolute paths, `HOME`
and `/tmp` are the box's own, so `sqlite3` above leaves `shop.db` in the box for the next
command (or the file explorer) to see. Try them in the playground's
[Linux packages](https://github.com/lifo-sh/lifo/blob/main/docs/build/playground?example=linux) example.

## From your code

Register the commands on a box, then run them like any other:

```js
import { Sandbox } from '@lifo-sh/core';
import nix, { nixShell } from 'lifo-pkg-nix';

const box = await Sandbox.create();
box.commands.register('nix', nix);
box.commands.register('nix-shell', nixShell);

await box.fs.writeFile('/home/user/data.json', '{"n": [1, 2, 3]}');
const { stdout } = await box.commands.run("nix shell nixpkgs#jq -c jq '.n | add' data.json");
console.log(stdout); // 6
```

## How it works

There's no Nix evaluator in a box and no kernel to run Linux binaries, so the package brings
what's needed and keeps it out of the box:

- **A name becomes a store path.** `nixpkgs#jq` resolves through the
  [nixpkgs-multiverse](https://nixmultiverse.com) index (the one [trynix](https://trynix.dev)
  uses) to the newest aarch64-linux build Hydra made. `nixpkgs#jq@1.7.1` picks a version, and
  `/nix/store/...` paths work too.
- **The closure is downloaded and checked.** Each path's narinfo comes from cache.nixos.org and
  its signature is checked against the `cache.nixos.org-1` key; each archive is checked against
  its signed size and hash before anything runs.
- **A Linux guest runs it.** [unkernel](https://github.com/pithings/unkernel), the Linux kernel
  compiled to WebAssembly, boots on first use (about 0.1 s) with the store at `/nix`. Each run
  mounts the box's files into the guest and starts the program with the box as its root.

On a Linux machine that has Nix installed (a Docker container, for instance), `nix` runs the
host's own Nix with the same arguments instead, natively, with nothing emulated.

## Where it runs

| Where | How | Notes |
| --- | --- | --- |
| Browsers with JSPI: Chrome and Edge 137+, Firefox 153+, Safari 27+ | The WebAssembly guest | The kernel (about 12 MB) loads from jsDelivr on first use. The guest reaches the internet only through a [Wisp](https://github.com/MercuryWorkshop/wisp-protocol) relay (`LIFO_NIX_WISP`); lifo.sh's playground passes its own. Tested in Chrome. |
| Node.js 24+ | The WebAssembly guest, in a worker thread | Node 24 has JSPI behind a flag, which the package turns on. The guest uses the host's sockets. Downloads are kept in `~/.cache/lifo-nix`. |
| Linux with Nix installed, such as a Docker container | The host's Nix, natively | Detected on the PATH (`nix`, or `nix-portable`, which works but is slow to start). Any system Nix supports, x86_64 included. `LIFO_NIX_BACKEND=guest` forces the guest. Tested in `node:24-bookworm-slim` with single-user Nix 2.36. |
| Linux without Nix, such as a plain Node.js container | The WebAssembly guest | Same as Node.js. Tested in `node:24-bookworm-slim`, including https from the guest. |
| Lifo Cloud | Not yet | A cloud box is a workerd isolate. The guest has run in a workerd isolate with a cloud box's settings, so this is on the way; it isn't wired into cloud boxes yet. |
| Cloudflare Workers, Vercel Edge | No | Cloudflare gives an isolate 128 MB, which the guest's memory, kernel and packages don't fit in, and only instantiates precompiled WebAssembly, while the guest compiles code as it runs. Vercel Edge doesn't allow compiling WebAssembly at run time at all. Run `nix` from Node.js or a container instead. |

## Downloads

The first run of a package downloads it and everything it needs; later runs come from the cache.
Most command-line tools share glibc (7.5 MiB), so a typical tool is 8 to 13 MiB the first time;
`git` is about 20 MiB, `nodejs` 31 MiB and `python3` 71 MiB. Measured on a laptop in Node.js:
`hello` takes about 1 s the first time and under 0.1 s once the guest is up; in Chrome, the first
`nix` of a visit also loads the kernel (about 3 s in all), and a second visit starts in under 1 s.

## Limits

- **Prebuilt packages only.** There's no evaluation, so no `nix build`, `nix develop`, flakes
  other than nixpkgs, or `shell.nix`. A package Hydra didn't build for aarch64-linux doesn't run
  in the guest (the native path isn't limited this way).
- **aarch64 in the guest.** unkernel emulates an aarch64 CPU, so CPU-heavy work runs several
  times slower than native, and x86_64-only packages don't run there.
- **Inside the guest**, programs run as root and see the box's files as root's, and `nix shell -c`
  can run the selected packages' programs and busybox's, not Lifo's own commands.
- **Binary output** on stdout is decoded as text; write binary output to a file instead.
- **Memory.** Downloaded packages stay in memory for the session (`python3` takes about 230 MB).

The package's [README](https://github.com/lifo-sh/lifo/tree/main/packages/lifo-pkg-nix) lists its
settings (`LIFO_NIX_BACKEND`, `LIFO_NIX_FETCH=lazy`, another binary cache and its keys) and
licenses: the package is MIT; unkernel's kernel, loaded at run time, is GPL-2.0.
