Files
devvm/internal/vm/dockerlayer.go
T
w3cjandClaude Opus 5.5 91a429108f feat(images): a built-in nix layer with Nix, direnv and nix-direnv, pinned
nix is a layer devvm ships, as docker is: devvm nix, devvm node+nix, devvm
build nix, or added to an existing VM in place. A folder with flake.nix,
shell.nix or devenv.nix at its top level, or a default.nix that makes a
shell (mkShell), gets it after its toolchains, so package.json and flake.nix
give node+nix.

- assets/layers/nix.sh: Nix 2.35.4's release tarball (releases.nixos.org),
  its version and SHA-256 per arch at the top, checked against the
  tarball's .sha256 files and nixos.org/nix/install, fetched with
  devvm-fetch; 2.35.3 fixes GHSA-hf6w-2rg9-v84g (store ownership in the
  scripted installer) and CVE-2026-110450 (store corruption through the
  daemon's repair flag). The tarball's own install --daemon --yes
  --no-channel-add, with experimental-features = nix-command flakes,
  trusted-users = root, sandbox = true and sandbox-fallback = false. The
  installer runs without proxy variables (it writes any it finds into a
  systemd override for nix-daemon, which would put a build's proxy into
  the image) and with NIX_SSL_CERT_FILE at the system's bundle. The layer
  checks the store is root's, adds Nix's default profile to the end of
  sudo's secure_path (/etc/sudoers.d/devvm-nix, checked with visudo), and
  appends Nix's profiles to BASH_ENV's PATH. direnv 2.38.2 (per arch) and
  nix-direnv 3.2.0's direnvrc, pinned the same way; direnv's hook in
  ~/.bashrc.d/direnv.sh, which loads before mise.sh so direnv's prompt hook
  runs after mise's and a dev shell's PATH comes first; a direnv lib file
  puts each project's layout under ~/.cache/direnv/layouts, so nix-direnv
  writes no .direnv into the project folder. Without systemd the script
  refuses in one line.
- assets/layers/nix.policy: cache.nixos.org, channels.nixos.org,
  releases.nixos.org (GitHub is in base.policy).
- assets/guest/sync.sh: nix-daemon gets a systemd drop-in with the VM's
  proxy (http_proxy, https_proxy, no_proxy) under network policy, removed
  when it's off, the daemon restarted when it changed.
- image.NixLayer in Builtins; project.Detect's nix marker (a default.nix is
  read only as a regular file, at most 64 KiB); backend.FeatureNixLayer
  (nix-layer), needed by a VM of an image with the layer and by building
  one: Lima native, smolvm and Docker none, so both refuse in one line
  (backend_lacks); dockerLayers.choice returns every layer feature of the
  image, so docker+nix on Docker needs nix-layer too.
- scripts/bump.sh: nix (the version nixos.org's installer names, the
  sums Nix publishes; it fails when the installer names no tarball, as
  Nix's Rust installer will), direnv, nix-direnv.
- Docs: using/images (built-in layers, detection), using/toolchains (Nix
  projects), safety/network (the layer's hosts; source builds' hosts
  blocked), internals/images (the nix layer), internals/network (the
  daemons' drop-ins), internals/backends (nix-layer), internals/design
  (the nix layer beside Instead of Nix), backends, customizing,
  coding-agents, contributing (the e2e section); the CLI reference;
  AGENTS.md's assets/ and e2e lines.
- Tests: Detect (markers, default.nix with and without a shell, a linked
  one), layer and environment lists, layer new nix reserved, nix-layer in
  ImageSpec and ImageFeatures, caps tests, up_checks.txtar (node+nix
  detected under an agent), docker.txtar (nix refused for a VM and a
  build). e2e nix section: the layer built behind a company proxy leaves
  no proxy settings and no password on its image's disk; a node+nix flake
  VM with network policy: pins, a root-owned store, sandbox without
  fallback, trusted-users root and an untrusted substituter ignored, the
  daemon's drop-in, nix run nixpkgs#hello, sudo nix and nix over ssh, the
  flake's node ahead of the shared node prompt after prompt through
  direnv, no .direnv in the folder, a blocked fixed-output fetch named by
  devvm-why, and the drop-in gone after network off.

Review (security-audit skill, guidance mode, against the review task's
list): the tarball and direnv's files are pinned in devvm's source and
checked before tar or install runs; nothing from the host goes into the
image (no proxy override, e2e greps the disk); the drop-in holds only the
VM's own proxy URL, devvm's value, and goes when policy is off;
trusted-users stays root and an untrusted user's substituters are
ignored (the VM's user has sudo, so that guards an honest VM and the
allowlist is what holds); secure_path gains only the root-owned default
profile, last; /nix is on the VM's own disk and nothing mounts a host's
or another VM's store. Found and fixed in review: Nix's default
sandbox-fallback would build unsandboxed without a word where the kernel
can't sandbox, so it's off. No confirmed findings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:22:02 -07:00

92 lines
3.2 KiB
Go

package vm
import (
"context"
"github.com/w3cj/devvm/internal/backend"
"github.com/w3cj/devvm/internal/image"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/netpolicy"
"github.com/w3cj/devvm/internal/state"
)
// dockerLayers is how VMs run the docker layer on a backend that makes
// VMs for it (backend.DockerLayerVM), new ones and those it's added to in
// place alike: the runtime (KeyDockerLayer), and setting it on a VM that
// exists. Such a VM gets no network policy.
type dockerLayers struct{ *Env }
// choice is the runtime a VM with the docker layer runs it under on a
// backend that makes VMs for it (dl), and the features its layers (img)
// need (docker-layer, refused by Require while unreviewed, and any
// other's); the backend's refusal where it can't run it.
func (e dockerLayers) choice(dl backend.DockerLayerVM, vm, img string) (string, []backend.Feature, error) {
rt, err := dl.DockerLayer(vm)
if err != nil {
return "", nil, err
}
return rt, backend.ImageFeatures(img), nil
}
// say says which runtime runs a VM's docker layer; nothing for "".
func (e dockerLayers) say(b backend.Backend, mode, vm string) {
if mode != "" {
e.Info(msg.DockerLayerRuntime(b.Name(), mode, vm))
}
}
// inPlace checks the docker layer added to a VM in place (when added has
// it) on a backend that makes VMs for it: its runtime (choice), or the
// backend's refusal; refused too for a VM with network policy, before
// anything changes. It returns the runtime ("" otherwise) and what adding
// the layers needs of the backend.
func (e dockerLayers) inPlace(b backend.Backend, st *state.VM, added string) (string, []backend.Feature, error) {
dl, made := b.(backend.DockerLayerVM)
if !made || !image.HasLayer(added, image.DockerLayer) {
return "", backend.ImageFeatures(added), nil
}
mode, need, err := e.choice(dl, st.Name(), added)
if err == nil && netpolicy.On(st) {
err = msg.NetDockerLayer(b.Name(), st.Name())
}
return mode, need, err
}
// set makes a stopped VM one for the docker layer under runtime mode,
// recorded (KeyDockerLayer), its backend's container made again at its
// next start; nothing for "".
func (e dockerLayers) set(ctx context.Context, b backend.Backend, st *state.VM, mode string) error {
dl, made := b.(backend.DockerLayerVM)
if mode == "" || !made {
return nil
}
if err := dl.SetDockerLayer(ctx, st.Name(), true); err != nil {
return err
}
if err := st.Set(KeyDockerLayer, mode); err != nil {
return err
}
e.say(b, mode, st.Name())
return nil
}
// unset makes a VM that set made one for the docker layer under mode one
// as it was, once the layer's script failed: stopped, its backend's
// container made again without it, its state's KeyDockerLayer emptied.
// Nothing for "". It runs on when ctx is cancelled (an interrupted
// script), which would leave the VM without network policy for good.
func (e dockerLayers) unset(ctx context.Context, b backend.Backend, st *state.VM, mode string) error {
dl, made := b.(backend.DockerLayerVM)
if mode == "" || !made {
return nil
}
ctx = context.WithoutCancel(ctx)
if err := b.Stop(ctx, st.Name()); err != nil {
return err
}
if err := dl.SetDockerLayer(ctx, st.Name(), false); err != nil {
return err
}
return st.Set(KeyDockerLayer, "")
}