Files
w3cjandClaude Opus 5.5 6ad4f6cb63 feat(images): your own layers get the scripts' prelude
devvm now puts assets/prelude.sh before a user's layer script, as it does
for its own scripts, so a layer gets $devvm_arch, $devvm_libc and
devvm_sha and can pin a download per architecture (and per C library on
Alpine) the way devvm's scripts do.

- image.WithPrelude puts the prelude before any script; OwnScript calls it.
- build.(*Env).LayerData reads the user's layer and puts the prelude
  before it; a deleted layer is still fs.ErrNotExist (Missing).
- The layer's step holds the prelude, so its key changes on its own:
  every image with a user's layer, and every image above one, is built
  again once. base and built-in-only images, and tool images, keep
  their keys; keyFormat is unchanged. The v0.1.0 fixtures have no user
  layers, so their keys don't move.
- The prelude runs first, so a layer that sets its own devvm_ name keeps
  it.
- layer new's starter mentions devvm_sha and $devvm_arch.
- Tests: TestWithPrelude, TestUserLayerGetsThePrelude (LayerData, the key
  differing from a prelude-less build's, a deleted layer still missing);
  prelude.txtar runs a layer as a build does on gnu and musl, and one
  that overrides devvm_arch and devvm_sha.
- e2e (images): the fixture layer prints $devvm_arch, $devvm_libc and
  devvm_sha of two fake pins, which the check compares with the VM's.
- Docs: Customizing's Mailpit example uses $devvm_arch and devvm_sha;
  images.md's key table, recipe and prelude sections; structure.md;
  CHANGELOG (Breaking).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 17:46:30 -07:00

100 lines
3.2 KiB
Go

package build
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/devvmhq/devvm/internal/image"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/paths"
)
// layerScript is the script a new layer starts with; NAME is its name.
const layerScript = `#!/bin/bash
# NAME: a devvm layer. devvm runs it once, with bash as the VM's user (who has sudo), in an image
# on top of base: devvm build NAME builds it, and a new VM gets it with --layers NAME. Editing it rebuilds that image,
# and the ones built on it, on next use.
# - Put PATH changes in ~/.profile.
# - Download with devvm-fetch URL SHA256 OUT: it fails when the checksum doesn't match. With
# TOOL_sha256_arm64 and TOOL_sha256_amd64 set, devvm_sha TOOL is this VM's; $devvm_arch is arm64 or amd64.
# - On the allowlist tier, NAME.policy next to this file lists the hosts VMs with this layer reach.
set -euo pipefail
# sudo apt-get install -y postgresql-client
`
// layerPolicy is the allowlist a new layer starts with.
const layerPolicy = `# Hosts a VM with the NAME layer can reach on the allowlist tier, one rule per line: example.com,
# *.example.com (any subdomain), host:port, an address or CIDR, !rule (deny).
`
// CheckLayerName refuses a name a new layer can't have: base's, a shared
// tool's or a built-in layer's, or one that isn't lowercase letters,
// digits and dashes.
func CheckLayerName(name string) error {
switch {
case name == image.Base || image.IsTool(name) || image.IsBuiltin(name):
return msg.LayerNameReserved(name)
case !image.LayerName(name):
return msg.LayerNameInvalid(name)
}
return nil
}
// NewLayer starts one of the user's layers: its script, and its allowlist
// unless that exists. It returns their paths. A name a layer can't have,
// or whose script exists, is refused.
func NewLayer(dirs paths.Dirs, name string) (script, policy string, err error) {
if err := CheckLayerName(name); err != nil {
return "", "", err
}
script = filepath.Join(dirs.Layers(), name+".sh")
policy = filepath.Join(dirs.Layers(), name+".policy")
if _, err := os.Lstat(script); err == nil {
return "", "", msg.LayerExists(script)
} else if !errors.Is(err, fs.ErrNotExist) {
return "", "", err
}
if err := os.MkdirAll(dirs.Layers(), 0o700); err != nil {
return "", "", err
}
if err := createNew(script, strings.ReplaceAll(layerScript, "NAME", name)); errors.Is(err, fs.ErrExist) {
return "", "", msg.LayerExists(script)
} else if err != nil {
return "", "", err
}
if err := createNew(policy, strings.ReplaceAll(layerPolicy, "NAME", name)); err != nil && !errors.Is(err, fs.ErrExist) {
return "", "", err
}
return script, policy, nil
}
// createNew writes a file that mustn't exist yet, whole or not at all: a
// temporary file, linked into place.
func createNew(path, data string) error {
f, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".tmp-*")
if err != nil {
return err
}
tmp := f.Name()
defer func() { _ = os.Remove(tmp) }()
_, err = f.WriteString(data)
if err == nil {
err = f.Sync()
}
if cerr := f.Close(); err == nil {
err = cerr
}
if err == nil {
err = os.Link(tmp, path)
}
if err != nil && !errors.Is(err, fs.ErrExist) {
return fmt.Errorf("writing %s: %w", path, err)
}
return err
}