bun2nix

repository·master·Indexed 41 days ago

https://github.com/nix-community/bun2nix

A 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.

Tokens
16.5K
Snippets
69
Records
91
Agent score
50%

What's inside bun2nix

  1. What is Bun2Nix?

    master
    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.
  2. Overview of the Bun2Nix Catalog Template

    master

    The 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 how bun2nix handles both the default catalog and named catalogs (e.g., catalog:tooling).

    Project Structure

    • packages/app: A simple application that consumes the workspace library using the catalog: protocol.
    • packages/lib: A library package that uses the default catalog to pin its npm dependencies.
  3. Understand the Bun2Nix sample project structure

    master

    The sample project demonstrates how bun2nix integrates 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 by bun2nix from bun.lock.
    • package.json: The standard JavaScript manifest, which includes a postinstall script that invokes bun2nix to ensure the Nix expressions are up to date.
  4. Understand the Bun2Nix sample file structure

    master

    The sample project demonstrates how bun2nix integrates 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 by bun2nix from bun.lock.
    • package.json: A standard JavaScript manifest that includes a postinstall script configured to trigger bun2nix.
  5. Understand the purpose of the bun2nix templates directory

    master

    The programs/bun2nix/templates/ directory contains rinja templates used internally by bun2nix to 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 bun2nix integration, use the nix/templates/ folder instead.

  6. How `fetchBunDeps` operates internally

    master

    The fetchBunDeps function follows a four-step process to create a Bun-compatible cache:

    1. Download: Packages are downloaded using a Nix fetching function (typically pkgs.fetchurl). The hashes are derived from the bun.lock file.
    2. Extraction & Patching: Tarballs are extracted and permissions are normalized. References to node or bun binaries are fixed up.
    3. 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 of Wyhash and 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
    4. Cache Enforcement: The bun2nix hook sets the $BUN_INSTALL_CACHE_DIR environment variable to $out/share/bun-cache, forcing Bun to use the Nix-provided cache.
  7. Choosing between the WASM CLI and the native CLI

    master

    The bun2nix tool 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 git or tarball dependencies.
  8. Understand the bun2nix project structure

    master

    When 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 of bun2nix within 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 by bun2nix that contains the Nix expression derived from your bun.lock.
    • package.json: A standard JavaScript package.json that includes a postinstall script configured to trigger bun2nix.
  9. How private registry authentication works in bun2nix

    master

    The authentication workflow follows these steps:

    1. Generation Phase: When bun2nix generates the bun.nix file, it extracts the full tarball URLs from bun.lock for any packages hosted on non-default registries.
    2. Evaluation Phase: When Nix evaluates fetchBunDeps, it parses the credentials from the files provided via bunfigPath or npmrcPath.
    3. Request Phase: fetchBunDeps creates an authenticated fetchurl wrapper that automatically injects the appropriate Authorization headers for matching registry hosts.

    Note: Packages from the default registry (registry.npmjs.org) do not require these credentials.

  10. Access bun2nix building functions via passthru

    master

    The bun2nix package 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 the bun2nix package itself via its passthru attribute.

    # Example pattern for accessing functions
    let bun2nix = import <nixpkgs> { ... };
    let bun2nixFunctions = bun2nix.passthru;
    # Now you can use functions like bun2nixFunctions.mkDerivation
  11. How Bun workspace catalogs are handled

    master

    Projects using Bun's catalog: protocol are supported out of the box.

    Because bun install re-resolves catalog: specifiers against the registry even with a populated cache, the hook runs bunResolveCatalogRefs immediately before bun install. This process:

    1. Reads the exact pinned version for each package from bun.lock.
    2. Rewrites every catalog: reference (in bun.lock's workspaces section and each workspace package.json) to that exact version, or to workspace:* for workspace packages.

    This works for both the default catalog and named catalogs (e.g., catalog:<name>).