bun2nix
repository·master·Indexed 41 days ago
https://github.com/nix-community/bun2nixA Rust-based utility that converts Bun (v1.2+) lock files into Nix expressions, enabling reproducible package builds within the Nix ecosystem. It provides specialized building functions via passthru, including mkDerivation, hook, fetchBunDeps, writeBunApplication, and writeBunScriptBin.
What's inside bun2nix
- Bun2Nix is a fast, Rust-based tool designed to convert Bun (v1.2+) package lock files into Nix expressions. These expressions can then be consumed within a reproducible Nix derivation to produce packages, bridging the gap between Bun's package management and Nix's reproducible build environment.
Overview of the Bun2Nix Catalog Template
masterThe Bun2Nix Catalog Template demonstrates how to use a Bun workspace project with the
catalog:protocol to centralize dependency versions. This template is specifically designed to show howbun2nixhandles both the default catalog and named catalogs (e.g.,catalog:tooling).Project Structure
packages/app: A simple application that consumes the workspace library using thecatalog:protocol.packages/lib: A library package that uses the default catalog to pin its npm dependencies.
Understand the Bun2Nix sample project structure
masterThe sample project demonstrates how
bun2nixintegrates with a standard Bun project. The following files are key to the integration:flake.nix: Provides the Nix Flake setup for the project.default.nix: Contains the specific Nix build instructions for the Bun package.bun.lock: The source of truth for Bun dependencies.bun.nix: The Nix expression generated bybun2nixfrombun.lock.package.json: The standard JavaScript manifest, which includes apostinstallscript that invokesbun2nixto ensure the Nix expressions are up to date.
Understand the Bun2Nix sample file structure
masterThe sample project demonstrates how
bun2nixintegrates with a standard Bun/Nix project through several key files:flake.nix: Defines the Nix Flake setup for the project.default.nix: Contains the specific build instructions for the Bun package.bun.lock: The source of truth for Bun dependencies.bun.nix: The output file generated bybun2nixfrombun.lock.package.json: A standard JavaScript manifest that includes apostinstallscript configured to triggerbun2nix.
Understand the purpose of the bun2nix templates directory
masterThe
programs/bun2nix/templates/directory contains rinja templates used internally bybun2nixto construct the output Nix files.Note: This directory is for template definitions used by the tool itself. If you are looking for project templates to bootstrap your own Bun project with
bun2nixintegration, use thenix/templates/folder instead.How `fetchBunDeps` operates internally
masterThe
fetchBunDepsfunction follows a four-step process to create a Bun-compatible cache:- Download: Packages are downloaded using a Nix fetching function (typically
pkgs.fetchurl). The hashes are derived from thebun.lockfile. - Extraction & Patching: Tarballs are extracted and permissions are normalized. References to
nodeorbunbinaries are fixed up. - Cache Entry Creation: The
bun2nix.cache-entry-creator(a Zig utility) transforms the patched directories into the specific format Bun expects in its global cache. It handles package name patching using a specific version ofWyhashand ensures sub-directories (like@types) are correctly structured.- Example Input:
@types/bun@1.2.4 - Example Output:
$out/share/bun-cache/@types/bun@1.2.4@@@1
- Example Input:
- Cache Enforcement: The
bun2nixhook sets the$BUN_INSTALL_CACHE_DIRenvironment variable to$out/share/bun-cache, forcing Bun to use the Nix-provided cache.
- Download: Packages are downloaded using a Nix fetching function (typically
Choosing between the WASM CLI and the native CLI
masterThe
bun2nixtool is available in two flavors: WASM and Native. Choosing the right one depends on your environment and dependency types.Use the WASM CLI if:
- You are working in environments where Nix is not installed (e.g., Windows users on a team).
- You are not using exotic dependency types like tarballs or git repositories.
Use the Native CLI if:
- Your team is already using Nix (this reduces overhead as it avoids running in a JavaScript runtime).
- You are using dependency types that require a prefetch, such as
gitortarballdependencies.
Understand the bun2nix project structure
masterWhen using
bun2nix, your project will typically include the following key files:flake.nix: Contains the basic project setup for a Nix flake, enabling the use ofbun2nixwithin the Nix ecosystem.default.nix: Contains the specific build instructions for the Bun package.bun.lock: The standard Bun lockfile used as the source of truth for dependencies.bun.nix: A file generated bybun2nixthat contains the Nix expression derived from yourbun.lock.package.json: A standard JavaScriptpackage.jsonthat includes apostinstallscript configured to triggerbun2nix.
How private registry authentication works in bun2nix
masterThe authentication workflow follows these steps:
- Generation Phase: When
bun2nixgenerates thebun.nixfile, it extracts the full tarball URLs frombun.lockfor any packages hosted on non-default registries. - Evaluation Phase: When Nix evaluates
fetchBunDeps, it parses the credentials from the files provided viabunfigPathornpmrcPath. - Request Phase:
fetchBunDepscreates an authenticatedfetchurlwrapper that automatically injects the appropriateAuthorizationheaders for matching registry hosts.
Note: Packages from the default registry (
registry.npmjs.org) do not require these credentials.- Generation Phase: When
Access bun2nix building functions via passthru
masterThe
bun2nixpackage exposes several specialized functions for building Bun-related packages. These functions are not top-level attributes of the Nixpkgs set but are available as attributes on thebun2nixpackage itself via itspassthruattribute.# Example pattern for accessing functions let bun2nix = import <nixpkgs> { ... }; let bun2nixFunctions = bun2nix.passthru; # Now you can use functions like bun2nixFunctions.mkDerivationUse fetchBunDeps instead of mkBunNodeModules
masterIn V2,mkBunNodeModulesis deprecated and removed. To build a bun-compatible cache, usefetchBunDeps. This approach guarantees correct Bun installs by building the Bun cache rather than manually creating thenode_modules/directory.How Bun workspace catalogs are handled
masterProjects using Bun's
catalog:protocol are supported out of the box.Because
bun installre-resolvescatalog:specifiers against the registry even with a populated cache, the hook runsbunResolveCatalogRefsimmediately beforebun install. This process:- Reads the exact pinned version for each package from
bun.lock. - Rewrites every
catalog:reference (inbun.lock'sworkspacessection and each workspacepackage.json) to that exact version, or toworkspace:*for workspace packages.
This works for both the default catalog and named catalogs (e.g.,
catalog:<name>).- Reads the exact pinned version for each package from