# Linux packages: Alpine, Debian and Nix

A box can run real Linux programs from three places, all opt-in and in preview:

- **Alpine** (3.24) with `apk`: `alpine` and `apk`.
- **Debian** (13) with `apt`: `debian`, `apt` and `apt-get`.
- **Nix** (nixpkgs): `nix run`, `nix shell` and `nix-shell -p`, described on [Nix packages](https://lifo.sh/docs/build/nix.md).

They come from the `lifo-pkg-nix` package and run in one Linux guest: the Linux kernel compiled
to WebAssembly ([unkernel](https://github.com/pithings/unkernel)), running unmodified aarch64
programs. Nothing loads or downloads until someone types one of these commands, and the box
itself stays what it always is, TypeScript with no kernel.

```bash
apk add jq
alpine jq '[.orders[].total] | add' orders.json
apk add miller                         # from Alpine's community repository, added when needed
alpine mlr --icsv --opprint sort -nr units sales.csv
alpine -c 'grep -rn TODO notes | sort > todo.txt'
alpine                                 # a shell in Alpine; exit to leave

apt install sqlite3
debian sqlite3 shop.db < schema.sql
debian -c 'cat /etc/debian_version; uname -m'
```

Every program reads and writes the box's files: `/home`, `/tmp` and the box's other top-level
folders are bound into the distro at the same paths, and each command starts in the box's
working directory. 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
[Alpine](https://github.com/lifo-sh/lifo/blob/main/docs/build/playground?example=alpine), [Debian](https://github.com/lifo-sh/lifo/blob/main/docs/build/playground?example=debian) and [Nix](https://github.com/lifo-sh/lifo/blob/main/docs/build/playground?example=nix) examples.

## How it fits together

```text diagram=linux-packages
 Your agent or app (a page, a Node.js API route, a CLI)
     |  box.commands.run('python3 report.py')        ^  stdout, stderr, exit code
     v                                               |
 +- Lifo box: TypeScript, about 0.3 MB, everywhere JavaScript runs --------+
 |  shell, files, processes, ports                                           |
 |  Node.js: JavaScript and TypeScript, npm   |   Python (Pyodide)           |
 +---------------------------------------------------------------------------+
     |  apk, apt, nix: only when typed               ^  output; files written land in the box
     v                                               |
 +- Linux guest (unkernel, WebAssembly) -+   or   +- The host's own Linux --------+
 |  Alpine, Debian, nixpkgs programs     |        |  Nix or Debian, natively      |
 |  the box's files at the same paths    |        |  (a Docker container)         |
 |  browser tab, Node.js 24+             |        |                               |
 +---------------------------------------+        +-------------------------------+
```

There are two layers, and most work never leaves the first.

**The box is always there.** It's Lifo itself: a shell, a filesystem, processes and ports, a
Node.js runtime for JavaScript and TypeScript, and Python through Pyodide, all written in
TypeScript and about 0.3 MB. It runs wherever JavaScript runs, and starts in milliseconds.

**The Linux guest is there when you ask for it.** Typing `apk`, `apt`, `alpine`, `debian` or `nix`
starts a real Linux kernel compiled to WebAssembly next to the box, with the box's files mounted
at the same paths, and runs real Alpine, Debian or nixpkgs programs in it. On a Linux host that
has Nix, or is Debian, those commands run natively instead, with nothing emulated.

## Why it's built this way

An agent's tools should cost what they're worth. Most of what an agent does is JavaScript and
Python: run a script, transform a file, call an API, test a function. In a Node.js API route,
an agent can create a box and do all of that with nothing else installed: no VM, no container,
no sandbox service to call, a few milliseconds to start, and as many boxes as the process has
memory for. That's the first layer, and it's why the box itself has no kernel.

Some work needs a real Linux: a development environment with its own toolchain, a compiler, a
database server, ffmpeg, an ELF binary that only ships for Linux, or any of the tens of thousands
of packages in Alpine, Debian and nixpkgs. Putting a kernel in every box would make every box pay
for that (megabytes to download, a boot before the first command) and would rule out the places
where only JavaScript runs. So the kernel is a tool the box calls, not what the box is made of:
it costs nothing until a command needs it, it shares the box's files so the two layers work on the
same project, and where a native Linux is at hand, it isn't used at all.

## The commands

| Command | Does |
| --- | --- |
| `alpine` / `debian` | An interactive shell in the distro, on the terminal (or a script read from stdin). |
| `alpine -c '<command line>'` | Runs a command line with the distro's shell (`sh` on Alpine, `bash` on Debian). |
| `alpine <program> [args]` | Runs one program. |
| `apk ...` | `apk` in Alpine. When `apk add` can't find a package in `main`, `community` is added and it tries again. |
| `apt ...`, `apt-get ...` | In Debian. Without a terminal, `-y` is added; when an install can't find or fetch a package, the package lists are refreshed and it tries again. |
| `alpine --info` | Where the image and the writable layer are, how much the layer holds. |
| `alpine --reset` | Throws away everything installed or changed in the distro (not the box's files). |

### Commands Lifo doesn't have

With `LIFO_LINUX` set, a command Lifo has no command for runs from that distro, and if it isn't
installed yet, the distro's package for it is installed first:

```bash
export LIFO_LINUX=alpine
jq -r '.orders[].customer' orders.json | sort | uniq -c
figlet Lifo                            # "figlet: not found, apk add cmd:figlet", then it runs
```

Lifo's own commands (`ls`, `grep`, `node`, `git`, ...) still come first; `alpine grep ...` runs
Alpine's. This works through a generic hook in the shell, `box.commands.fallback()`, that you
can use for your own commands too.

## From your code

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

const box = await Sandbox.create();
for (const [name, command] of Object.entries(linuxCommands)) box.commands.register(name, command);
box.commands.fallback(linuxFallback); // only acts when LIFO_LINUX is set

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

## What is kept, and where

Each distro is its image (read-only) under a writable layer, the way unkernel builds its root.
What `apk` and `apt` install goes into the layer and is kept:

| Where | The image | The layer |
| --- | --- | --- |
| Node.js | Downloaded once from unkernel's GitHub release, checked against its SHA-256, into `~/.cache/lifo-linux/images` | `~/.cache/lifo-linux/<distro>/layer.img`, an ext4 image, sparse (4 GiB at most, a few MiB on disk at first) |
| Browsers | Read in pieces over HTTP as programs need them, from mirror.unkernel.dev | IndexedDB, `kwdisk:lifo-linux-<distro>-v1` |

A layer belongs to the machine (Node.js) or the site (browsers), not to one box: install `jq`
once and every box there has it. Box files are never in the layer; they're bound in from the
box on each run. One process (or tab) uses a layer at a time; another one gets a layer in memory
for its session and a note saying so. `alpine --reset` deletes the layer.

## 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. `apk` and `apt` reach the internet only through a [Wisp](https://github.com/MercuryWorkshop/wisp-protocol) relay (`LIFO_NIX_WISP`); lifo.sh's playground passes its own. |
| Node.js 24+ | The WebAssembly guest, in a worker thread | The guest uses the host's sockets. |
| Linux that is Debian, such as a `node:24-bookworm-slim` Docker container | `debian`, `apt` and `apt-get` run the host's own programs natively | Debian 12 in that image, not 13, at native speed. They work in the box's working directory (copied in and back, or in place when the host maps it). `alpine` uses the WebAssembly guest, as in Node.js. Lifo Cloud's container boxes run a real shell rather than a Lifo box, so this doesn't apply there. |
| Cloudflare Workers, Vercel Edge | No | Cloudflare gives an isolate 128 MB 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 these commands from Node.js or a container. |

## Downloads and times

The images are 3.9 MiB (Alpine) and 74.2 MiB (Debian). Measured on a laptop (Apple silicon,
headless Chrome 149) on 2026-11-29:

| | Node.js | Chrome |
| --- | --- | --- |
| First `alpine` command, nothing cached | 1.1 s (the image downloads) | 3.6 to 3.9 s, loading the kernel included |
| First `debian` command, nothing cached | 5.3 s (the whole image downloads) | 3.3 s, loading the kernel included (the guest read 2.5 MiB of the image) |
| First command in a new process, or on a later visit | 0.3 s | 0.4 to 1.3 s (Alpine), 1.0 to 1.6 s (Debian) |
| A command once the distro is up | about 50 ms | 15 to 60 ms |
| `apk add jq` | 1.1 s | 0.9 to 1.1 s |
| `apt install sqlite3` | 5.9 s | 24 s (the guest read 59 MiB of the image over HTTP) |

`apk` starts with only Alpine's `main` repository (it's about four times faster to read than with
`community`); `apt` comes with package lists from when the image was built.

## Limits

- **aarch64, emulated.** unkernel runs an aarch64 CPU with a JIT, so CPU-heavy programs are
  several times slower than native.
- **Programs run as root** in the distro, with `HOME` the box's. Box files show as root's.
- **No servers you can reach from the box.** A program can listen inside the guest, but Lifo's
  own commands and previews can't connect to it yet.
- **Binary output** on stdout is decoded as text; write binary output to a file instead.
- **Sockets in `/tmp`.** The box's `/tmp` is bound in, and it can't hold Unix sockets.
- **The browser's network** needs a Wisp relay, and the guest sees every name resolve (a bad
  one fails when connecting).
- **Images.** Browsers read them from mirror.unkernel.dev (`LIFO_LINUX_IMAGES` names another
  copy of unkernel's release).

The package's [README](https://github.com/lifo-sh/lifo/tree/main/packages/lifo-pkg-nix) lists
every setting (`LIFO_LINUX`, `LIFO_LINUX_PERSIST=0`, `LIFO_LINUX_BACKEND`, ...). The package is
MIT; unkernel's kernel and the distro images, loaded at run time, are GPL-2.0 and the
distributions' own licenses.
