Shemfile
A Shemfile uses Dockerfile syntax to build OCI images. WARM adds a build-time snapshot of a program, VM or model session.
just toolchain
shem build -t hello examples/hello # build into the local image store
shem run hello # run it
shem images # list the store
shem tag hello hello:1.0 # store it under another name too
shem inspect hello # its ID, settings and layers, as JSON
shem history hello # the instruction behind each layer
shem build -o hello.oci examples/hello # or write an OCI image layout directory
shem push hello ghcr.io/your-user/hello:1.0 # push to an OCI registry
shem pull ghcr.io/your-user/hello:1.0 # pull into the store, and run it by that name
docker save hello | shem load # or load an image docker build made
shem rmi hello # remove it from the store
shem prune # delete unpacked copies of images no longer stored
shem prune --all # and every copy, and the build cache
shem images --format takes json, or a Go template as docker images --format does, such as '{{.Repository}}:{{.Tag}} {{.Size}}', after table for aligned columns with headers. Its fields are .Repository, .Tag, .ID, .Digest, .Kind (program, vm or model), .Size and .Bytes, and its functions Docker’s json, upper, lower, title, truncate, pad, join, split and len. shem images -q prints the IDs alone, which shem rmi takes as well as names: an ID tagged more than once needs -f to remove every tag. tag, inspect and history take IDs too, or an OCI layout directory. shem history takes --format as well, with the fields .ID, .Digest, .CreatedBy, .Comment, .Size and .Bytes, and shem inspect -f a template over the JSON it prints, such as '{{.Config.Entrypoint}}'.
With docker build
To use shem’s BuildKit frontend, start the Dockerfile with its syntax directive:
# syntax=ghcr.io/jgalego/shem/dockerfile:latest
FROM hf.co/Qwen/Qwen3.5-0.8B
ENTRYPOINT ["shem:generate", "--system", "You are Shem, a golem made of clay."]
CMD ["Who are you?"]
WARM --until=prompt
The published frontend and runtime images are built for linux/amd64 and linux/arm64.
docker build --platform linux/riscv32 -t golem .
Run it with docker run through the containerd shim, or with docker save golem | shem load and shem run golem.
The frontend translates shem instructions into Docker build stages:
| Instruction | BuildKit behavior |
|---|---|
FROM hf.co/... | Import in shem-neural, then copy weights into layers |
ModelPack base or COPY --from source | Fetch the artifact and lay files out as its image twin, without sharing the original blobs |
WARM --until=prompt | Prime the model and copy session state into /.shem |
KERNEL | Set dev.shem.kernel and dev.shem.bootargs labels |
| Other stages | Ordinary Docker stages on the build platform |
The frontend and runtime images support linux/amd64 and linux/arm64; the resulting shem image uses linux/riscv32. RUN is not allowed in shem stages. Program and VM WARM require shem build, not docker build.
Build options:
| Option | Use |
|---|---|
--secret id=HF_TOKEN,env=HF_TOKEN | Access gated Hugging Face models |
--secret id=docker_config,src=$HOME/.docker/config.json | Authenticate to a registry |
--allow network.host | Reach a registry on localhost |
--build-arg SHEM_NEURAL_IMAGE=IMAGE | Select another runtime image |
Model downloads are cached between builds. Use docker push and docker save normally; shem run also accepts an extracted OCI layout from docker save.
Examples
The hello example cross-compiles in a Docker stage:
FROM shem-toolchain AS build
WORKDIR /src
COPY hello.c .
RUN rv32-cc -O2 -o hello hello.c
FROM scratch
COPY --from=build /src/hello /bin/hello
ENTRYPOINT ["/bin/hello"]
CMD ["from", "a", "shem", "image"]
The warm-shell example freezes BusyBox before its first read:
FROM shem-busybox AS busybox
FROM scratch
COPY --from=busybox / /
ENV PATH=/bin:/sbin:/usr/bin:/usr/sbin
ENTRYPOINT ["/bin/sh"]
WARM --until=syscall:read
Build it after creating shem-busybox:
just busybox-image
shem build -t warm-shell examples/warm-shell
$ printf 'echo hello from a warm shell\nuname -m\n' | shem run warm-shell
hello from a warm shell
riscv32
shem-toolchain and shem-busybox are Docker images built from toolchain/Dockerfile by just toolchain and just busybox-image.
Stages
The last stage produces the image. It must use one of the FROM sources below. Other stages supply files through COPY --from; Docker stages build only when needed. Local paths are relative to the build context, and registry bases are pulled on first use.
Instructions
| Instruction | In a shem stage |
|---|---|
FROM | scratch, a Hugging Face model, checkpoint directory or GGUF file (models.md), or a shem image or a ModelPack artifact (models.md), in a registry or a layout directory |
COPY --from=shem:rv32im src dest | Compiles and copies the RV32IM CPU for model tools |
COPY [--from=stage] src... dest | As in Docker: a directory’s contents are copied into dest; a file goes to dest, or into it when dest ends in / or there are several sources. Each COPY is one layer. Sources must stay inside the context. |
ENV, WORKDIR, ENTRYPOINT, CMD, ARG, LABEL | As in Docker, including shell and exec forms, and $NAME and ${NAME} substitution from ARG and ENV |
WARM | Save build-time state; see below |
KERNEL path [command line] | Select a kernel; see VM images |
RUN | Not allowed. Build in a Docker stage and COPY --from it. |
Set resource limits with shem run --mem and --max-steps, not Shemfile instructions.
WARM
WARM --until=<condition> [--max-steps=<n>]
Runs the entrypoint at build time until the condition holds, then freezes the container into a state layer. shem run resumes from that point instead of starting the process again.
Conditions:
syscall:<name>orsyscall:<number>: stop before servicing the first matching call. At run time it receives live input. Names includeread,write,openat,execve,wait4andclock_nanosleep.steps:<n>: stop onceninstructions have retired.ebreak: stop when the guest executesebreak, and resume after it. A program can use this to mark itself ready.
--max-steps fails the build if the condition is not reached in time (default: 4 billion instructions). A later WARM resumes from the previous one’s state.
The guest runs with an empty stdin; its output goes to the build’s stderr. The frozen container must be a single process with only stdin, stdout and stderr open: other open files live on the host and are not part of the snapshot yet.
A state layer holds:
machine.state: canonicalMachineState, with pc, registers, retired instruction count and memory.container.json: working directory, umask, memory map, signal state, LR/SC reservation, virtual clock and PRNG state.
Files the guest wrote during WARM become an ordinary filesystem layer, with whiteouts for deletions.
Program and VM snapshots are built on the reference vCPU. Their canonical state can resume on either vCPU. Model images instead use WARM --until=prompt to save a primed language-model session (models.md).
A warm program image takes no arguments or environment at run time, and runs with the memory it was frozen with.
VM images
KERNEL <path> [command line]
shem run boots this kernel from the image filesystem in VM mode, with the console on stdio. The default command line is earlycon=uart8250,mmio,0x10000000,1000000 console=ttyS0; default memory is 64 MiB, configurable with --mem.
In a VM image, WARM boots the kernel at build time and freezes the machine. Its conditions are:
output:TEXT: stop once the console’s output ends withTEXT, such as a shell’s prompt. Quote it to include spaces; in double quotes, JSON escapes such as\nwork.steps:<n>: stop onceninstructions have retired.
The state layer holds machine.state and vm.json, the host’s half of the hardware (see vm.md).
Image format
A shem image is a standard OCI image for linux/riscv32, stored as an OCI layout. Settings use these labels:
| Label | Value |
|---|---|
dev.shem.machine | Machine type |
dev.shem.memory | Warm image’s memory size |
dev.shem.kernel, dev.shem.bootargs | VM kernel path and command line |
dev.shem.model | Model directory |
A warm image’s last layer holds its state under /.shem. The config’s history records the Shemfile line behind each layer, and the instructions that made none between them, as shem history and docker history show it. The WARM that made the state layer comes last, commented state, even when later lines added layers before it.
Layouts in shem’s earlier format, with these media types, can still be read:
| Media type | Contents |
|---|---|
application/vnd.shem.config.v1+json | Entrypoint, cmd, env, workdir, labels, the machine (rv32im), a warm image’s memory size, and a VM image’s kernel and command line |
application/vnd.oci.image.layer.v1.tar+zstd | Guest filesystem layers, in the standard format |
application/vnd.shem.state.v1.tar+zstd | A WARM snapshot, always the last layer: a container’s (machine.state, container.json) or a VM’s (machine.state, vm.json) |
New layers use zstd-compressed tars. A booted Linux image is about 2 MB despite containing 64 MiB of RAM, mostly zeros. Uncompressed layers remain readable; inherited layers keep their original blobs and compression. A manifest can identify a related artifact through subject, as ModelPack twins do.
Images in the local store live under $SHEM_HOME/images/<name>/<tag>, by default ~/.local/share/shem/images. Program and VM runs unpack a fresh filesystem and remove it afterwards. Model images cache their unpacked filesystem.
A builder, a pull, a load, shem tag, shem rmi and shem prune take a lock on the store while they change it, so processes at once, builds of one tag among them, never leave half an image or remove one a build is storing; a built or pulled image is written whole beside the store and moved in. Reading takes no lock.
Registry transfers use ORAS and preserve manifest digests. Use --plain-http for a local registry without TLS.
The build cache
A model imported at a pinned revision, a commit (FROM hf.co/ORG/NAME:COMMIT), is imported once: later builds link the files the first one made, and a WARM --until=prompt that primes the same session on the same layers and driver options takes it from the cache too, so rebuilding such an image takes a fraction of a second where the first build took the import and the prime. A model at a branch, as FROM hf.co/ORG/NAME is, could be another model by now, so it is imported again, as are checkpoints and GGUF files in the build context. The cache sits beside the image store, links its files rather than copying them, and shem build --no-cache or SHEM_NO_CACHE=1 goes without it; shem prune --all empties it. A program’s or VM’s WARM is cached too: it runs the same for the same layers, configuration, limits and memory, since its clock and its host are virtual, so a build that has run it before takes its state, and the files it wrote, from the cache. COPY is not cached.
Reproducibility
Layers have sorted entries, timestamps of 0 and root ownership. Compression is deterministic; guest builds use a virtual clock, a seeded PRNG and empty stdin. With the same resolved base image, source files and build options, builds produce the same manifest digest, warm images included. Pin model revisions and Docker-stage inputs when repeatable builds are required.
Copy-on-write
- Shared layers. Images that hold the same layer keep one copy of it on the disk: a pulled, loaded or installed image’s blobs are hard links to the store’s own file for each (
$SHEM_HOME/blobs), and the unpacked copies hold a file of a mebibyte or more as a link to one in$SHEM_HOME/files, named for its content and mode. Two models with the same weights, or a model and the image built on it, take their space once, in the layouts and again in the copies unpacked to run them. A shared file is read-only in the unpacked copy, which a write would change for every image that holds it; a run’s overlay copies it up when a guest changes it, so a run on an overlay can still write to one. A file’s last link going, with the image or the copy that held it, takes it from the pool:shem rmiandshem prunedo. Where the store’s directories are not on one filesystem, or the system links no files, each image keeps its own, as before. - Filesystems. A program or VM run’s files are an overlay of the image’s, as far as the system lets a user mount one: the image is unpacked once, into a copy that
shem pruneremoves with the image, and each run mounts the kernel’s overlayfs over it, with an empty directory for what the guest creates, changes and deletes, removed when the run ends. Nothing is unpacked or copied for a run, and a guest cannot change the copy the next run starts from. The mount is made in theshemprocess’s own user and mount namespaces, so it needs Linux 5.11 or later and a user the kernel lets make them (Ubuntu 24.04 does not by default:sysctl kernel.apparmor_restrict_unprivileged_userns=0); elsewhere, and withSHEM_NO_OVERLAY=1, a run unpacks every layer into a directory of its own, as it did before.shem doctorsays which. A buildFROMan image already built (a program, a VM or a model image, as againstscratchor a Hugging Face checkpoint) is on one too: the base is that unpacked copy, and the build’sCOPYandWARMwrite above it, so a base of gigabytes is not unpacked again for each build; the image made is the same, digest for digest, as one built from unpacked layers. A Shemfile with Docker stages unpacks the base, since those run as programs of the build, in the namespaces the overlay is mounted in, which may not reach Docker.shem run --root DIR --root-overlay PROGRAMruns an executable on an overlay of a directory the same way, so that what it writes never reachesDIR(where the system cannot mount one, a warning says so and the changes go toDIR): the model server’s--toolsuse it, since the programs it runs would otherwise write to the image’s kept copy, which every later run reads. The containerd shim has nothing to add: containerd gives it an overlay of the image’s layers for each container, as for any runtime. - Reference memory.
forkcopies the guest’s memory. - Neural memory. The server shares 4 KiB pages between forks and restored snapshots, copying a page on its first write. The dense executor and GPU harness do not share pages (neural.md).