Skip to content

[pkgdef 3/6] feat(pkgdef): wire package definitions into add, lock, install and env - #3002

Open
mikeland73 wants to merge 13 commits into
mikeland73/pkgdef-libfrom
mikeland73/pkgdef-wiring
Open

mikeland73 wants to merge 13 commits into
mikeland73/pkgdef-libfrom
mikeland73/pkgdef-wiring

Conversation

@mikeland73

@mikeland73 mikeland73 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Stack (merge in order):

  1. [pkgdef 1/6] docs(specs): add package definition spec and Go example #3000 — spec + Go example
  2. [pkgdef 2/6] feat(pkgdef): add package definition library #3001 — internal/pkgdef library
  3. [pkgdef 3/6] feat(pkgdef): wire package definitions into add, lock, install and env #3002 — wiring into add/lock/install/env (behind DEVBOX_FEATURE_PKG_DEFS) 👈 this PR
  4. [pkgdef 4/6] feat(pkgs): add the initial set of package definitions #3034 — initial package set (15 definitions)
  5. [pkgdef 5/6] feat(pkgdef): support the initial package set #3035 — format features for the initial package set
  6. [pkgdef 6/6] test(pkgs): install and switch versions of every shipped package #3036 — per-package install/switch tests + CI

Summary

Third PR of the package definitions stack. Teaches Devbox the pkg:<ref>@<version> package string, behind the DEVBOX_FEATURE_PKG_DEFS flag (on in dev builds). With the flag off, pkg: strings fail with an error telling the user to set the flag.

DEVBOX_FEATURE_PKG_DEFS=1 devbox add pkg:./[email protected]
devbox run -- go version   # go version go1.22.12 darwin/arm64, from go.dev
  • Recognizing the scheme. pkgtype.IsPkgDef recognizes pkg: and keeps it out of flake detection. In devpkg, such packages:
    • are treated as non-nix, so shellgen and the nix store paths skip them;
    • split name and version at the last @;
    • resolve through the lockfile, so devbox add surfaces the definition's own errors instead of falling back to nixpkgs.
  • Lockfile. devbox.lock gains definition_hash and per-system url/sha256 (additive; lockfile_version unchanged).
    • Remote definitions stay pinned to the hash except on devbox update. Update also records a changed hash when version and artifacts are unchanged. Every pkg: entry is eligible for update, including ones written without a version.
    • A definition without a checksum source gets its digest recorded by the first install (trust on first use). This happens only on the install path, never while computing the environment.
  • Install and PATH. installPackages installs package definitions next to runx packages, and computeEnv adds their bin dirs to PATH.
    • An unsupported system fails with an --exclude-platform hint.
    • Relative file:// URLs stay relative in the lockfile and resolve against the definition's directory when fetching, so lockfiles are portable.
  • Activation. Each resolved definition becomes a plugin config (virtenv pkgdef-<name>), so env, shell.init_hook and create_files apply through the plugin manager. Templates get InstallDir, Version, CacheDir, System, OS and Arch.
  • Robustness. A definition that cannot be loaded does not block config loading, so devbox update and devbox rm always work.
  • Reinstall on edit. Local definition files feed the config hash. Installs land in dirs keyed by the install fingerprint, so editing a definition's install recipe installs again. When a local definition no longer matches its definition_hash, the install step first re-locks the entry at the locked version, so url/sha256 follow the edited recipe.

The spec's status table is updated accordingly.

Audit fixes

These come from the cross-stack audit recorded in the spec.

  • Activation templates.
    • Each string is rendered with pkgdef's template engine and funcs before marshalling. Before, {{ if eq .OS "darwin" }} broke on JSON escaping, and trimPrefix and friends were undefined.
    • Load and template errors are now warnings instead of debug logs that silently dropped the env.
  • devbox info and devbox search with pkg: never contact the search service. info loads the definition and prints its name, version, description, homepage and platforms. It goes through packageInfo, so it also works outside a project (main's project-less devbox info).
  • System detection without nix. nix.SystemOrHost() falls back to GOOS/GOARCH. Before, a machine without nix silently lost the definition's env and failed installs.
  • PATH order. Definition bin dirs now come before the nix profile and the inherited PATH. They used to be appended last, losing to nix packages and system binaries.
  • Keyed install dirs. Install dirs come from the locked artifact ([pkgdef 2/6] feat(pkgdef): add package definition library #3001). The fingerprint-mismatch reinstall, which deleted a dir another project's shell might be using, is gone.

Review fixes

From Greptile's review of this PR:

  • Install identity. Install dirs are keyed by the resolved file:// path, so two definitions in different folders using the same relative URL never share an install. The lockfile keeps the relative URL.
  • First command after hand-editing devbox.json. The config is reloaded once the install locks a new pkg: entry, so the first devbox run gets its env, init hook and created files.
  • Activation. disable_plugin is honored. A second definition with the same name is skipped with a warning instead of writing into the same pkgdef-<name> dir. Editing a local definition's create_files source changes the config hash.
  • devbox rm removes the definition's pkgdef-<name> dir instead of a path built from the raw pkg: string.
  • Lockfile. The existing entry is read under the lockfile mutex. pkg-def entries set last_modified (kept when re-resolving yields the same entry), so multi-project lockfile sync no longer skips them.

Test plan

  • testscripts/pkgdef/pkgdef.test.txt covers:
    • add with a version prefix, lockfile contents, binary on PATH
    • env/init hook/create_files templating, version switch, unknown version error, remove
    • PATH order, eq/replace templates, info, search, updating an unversioned entry, flag-off errors
  • Unit tests in internal/pkgdef, internal/devpkg, internal/devbox (the update merge records a changed definition hash), internal/plugin (activation, disabled plugin, duplicate names, create_files hash), internal/lock (last_modified, re-locking an edited definition), internal/boxcli (search) and internal/nix
  • Manual: the real pkgs/go/go.pkg.json on macOS arm64 installs Go 1.22.12 from go.dev with all four platform checksums locked. which go and go env GOROOT point into the cache.
  • go vet and gofmt clean. golangci-lint was clean before the review fixes and has not been re-run since.

https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH


Generated by Claude Code

Teach Devbox the "pkg:<ref>@<version>" package string behind the
DEVBOX_FEATURE_PKG_DEFS flag (on in dev builds). See
specs/package-definitions.md.

- pkgtype.IsPkgDef recognizes the scheme and keeps it out of flake
  detection; devpkg treats such packages as non-nix so shellgen and the
  nix store paths skip them, and ValidateExists resolves them through
  the lockfile so `devbox add` surfaces the definition's own errors.
- devbox.lock gains definition_hash and per-system url/sha256. Remote
  definitions are pinned to the hash except on `devbox update`; a
  definition without a checksum source gets its digest recorded by the
  first install (trust on first use) and kept across updates.
- installPackages installs package definitions next to runx packages
  into $XDG_CACHE_HOME/devbox/pkgs and computeEnv appends their bin
  directories to PATH. A definition that does not support the current
  system fails with an --exclude-platform hint.
- Each resolved package definition becomes a plugin config so its env,
  shell.init_hook and create_files apply through the plugin manager,
  with InstallDir, Version, CacheDir, System, OS and Arch template
  variables. Local definition files feed the config hash so edits
  trigger a reinstall.
- Relative file:// URLs in a local definition resolve against its
  directory, which keeps the new testscript self-contained.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mikeland73
mikeland73 force-pushed the mikeland73/pkgdef-wiring branch from ccbb54d to 332643a Compare October 26, 2026 00:57
# Conflicts:
#	internal/devpkg/package.go
@mikeland73 mikeland73 closed this Nov 17, 2026
@mikeland73 mikeland73 reopened this Nov 17, 2026
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge with the stack, with a non-blocking checksum sync issue.

Fix All in Claude CodeFindings

  1. P2 Sync misses recorded checksums ▶
Fix with agent prompt
### Issue 1
internal/lock/pkgdef.go:131-133
`SetArtifactForPackage` saves a newly learned checksum without changing `LastModified`. If two projects start with the same lock entry and one installs the package, `SyncLockfiles` skips the other project because their timestamps still match. The second project keeps trusting its next download instead of using the checksum already recorded by the first.

Update the timestamp when recording a changed artifact, or make sync compare the artifact fields too.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds feature-flagged pkg: packages across resolution, installation, environment setup, plugins, and package inspection.

  • Devbox recognizes pkg: references as package definitions.
  • Package definitions install locked artifacts and add their binaries to PATH.
  • Package definitions can set project environment values and shell hooks.
  • devbox info shows details from package definitions locally.

Previous threads were supplied without comment numbers, so previousFindings is empty. Existing findings were not reposted.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A["pkg: package"] --> B["Resolve definition and version"]
  B --> C["Lock hash and artifacts"]
  C --> D["Install into keyed cache directory"]
  C --> E["Load plugin settings"]
  D --> F["Add binaries to PATH"]
  E --> G["Apply env, hooks, and created files"]
Loading

Reviews (4) · Last reviewed commit: "Merge branch 'mikeland73/pkgdef-lib' int..." · Reviewed by Greptile

Comment thread internal/plugin/pkgdef.go Resolved
Comment thread internal/devbox/pkgdef.go Resolved
Comment thread internal/pkgdef/install.go Resolved
Comment thread internal/plugin/pkgdef.go Resolved
Comment thread internal/plugin/pkgdef.go Resolved
Comment thread internal/plugin/pkgdef.go Resolved
Comment thread internal/boxcli/multi/sync.go Resolved
# Conflicts:
#	internal/devbox/packages.go
#	internal/devbox/update_test.go
@mikeland73 mikeland73 changed the title feat(pkgdef): wire package definitions into add, lock, install and env [pkgdef 3/6] feat(pkgdef): wire package definitions into add, lock, install and env Nov 23, 2026
…em and PATH

Fixes the PR 3 findings from the audit in specs/package-definitions.md:

- Activation templates: render every string in a definition's env, shell
  and create_files with the package definition template functions (now
  exported as pkgdef.TemplateFuncs) and the plugin variables before
  marshalling, instead of templating the JSON-escaped plugin.json. Errors
  loading a definition's activation are shown as a warning (once per
  message) instead of being logged at debug level; config load still
  succeeds so `devbox rm`/`update` keep working.
- `devbox update` always re-resolves pkg: entries, versioned or not.
- With DEVBOX_FEATURE_PKG_DEFS off, pkg: packages fail early (add,
  lockfile Resolve/FetchResolvedPackage) with a hint to set the flag,
  instead of a slow nixpkgs search or "unsupported flake reference".
- `devbox info pkg:...` loads the definition and resolves the version
  locally; `devbox search pkg:...` points to `devbox info`. Neither
  contacts the search service.
- Package definitions use nix.SystemOrHost, which falls back to
  GOOS/GOARCH when nix isn't installed.
- PATH: package definition bin dirs now come first, next to the nix
  profile bin dir and ahead of the stdenv and inherited PATH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH
# Conflicts:
#	internal/pkgdef/checksum.go
#	internal/pkgdef/definition.go
#	internal/pkgdef/install.go
#	internal/pkgdef/versions.go

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

Comment thread internal/pkgdef/install.go Resolved
Comment thread internal/plugin/pkgdef.go Resolved

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

Comment thread internal/lock/pkgdef.go Resolved
- Key installs by the resolved file:// URL so relative artifacts from
  different definition directories don't share an install.
- Re-lock a local definition's entry (keeping its version) when the file
  changed, so an edited install recipe doesn't install the old artifact.
- Reload the config after locking "pkg:" entries added by editing
  devbox.json, so the first command gets their env, init hook and files.
- Honor disable_plugin for package definitions.
- Skip (with a warning) a definition whose name collides with another's,
  instead of letting both write into pkgdef-<name>.
- Include local definitions' create_files sources in the config hash.
- Remove a package definition's plugin dir (pkgdef-<name>) on devbox rm.
- Read the existing lock entry under the lockfile mutex.
- Set last_modified on package definition lock entries so multi-project
  lockfile sync picks them up (kept unchanged when nothing changed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92pwczBpH2NQmF4hzg5
Resolve conflicts with the project-less 'devbox info' refactor from main:
the package-definition check moves into packageInfo so definitions are
never sent to the search service with or without a project, and
pkgDefInfo takes the project dir, locker and stderr instead of *Devbox.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92mV11RTGiKeSuiHtuV
…ck entries

- PR 3 spec text: flag-off pkg: strings fail with an error (they are not
  unrecognized); local definition edits re-lock the entry; pkg-def entries
  set last_modified; same-name definitions, disable_plugin and create_files
  edits in Activation; info/search never contact the search service.
- Unit tests for last_modified on pkg-def lock entries and RelockPkgDef.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92pwczBpH2NQmF4hzg5
@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

Comment thread internal/lock/pkgdef.go
Comment on lines +131 to +133
locked.Systems[system].URL = url
locked.Systems[system].Sha256 = sha256
return f.Save()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Sync misses recorded checksums

SetArtifactForPackage saves a newly learned checksum without changing LastModified. If two projects start with the same lock entry and one installs the package, SyncLockfiles skips the other project because their timestamps still match. The second project keeps trusting its next download instead of using the checksum already recorded by the first.

Update the timestamp when recording a changed artifact, or make sync compare the artifact fields too.

Knowledge Base Used: Dependency lock resolution

Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/lock/pkgdef.go
Line: 131-133

Comment:
**Sync misses recorded checksums**

`SetArtifactForPackage` saves a newly learned checksum without changing `LastModified`. If two projects start with the same lock entry and one installs the package, `SyncLockfiles` skips the other project because their timestamps still match. The second project keeps trusting its next download instead of using the checksum already recorded by the first.

Update the timestamp when recording a changed artifact, or make sync compare the artifact fields too.

**Knowledge Base Used:** [Dependency lock resolution](https://app.greptile.com/-/switch-tenant?tenant=mike-s-org&next=%2Fcustom-context%2Fknowledge-base%2Fjetify-com%2Fdevbox%2F-%2Fdocs%2Fdependency-lock-resolution.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants