Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

In a pre-existing flake

To install bun2nix in an already existing bun project with a flake.nix, the following steps are recommended:

1. Source the bun2nix repo

Add the bun2nix flake to your inputs as follows:

bun2nix.url = "github:nix-community/bun2nix";
bun2nix.inputs.nixpkgs.follows = "nixpkgs";

1.5. (Optional) Use the binary cache

The bun2nix executable typically takes a while to compile, which is typical for many Rust programs, hence, hence, it may be a good idea to use the nix-community cache.

To add it include the following in your flake.nix.

nixConfig = {
    extra-substituters = [
      "https://cache.nixos.org"
      "https://nix-community.cachix.org"
    ];
    extra-trusted-public-keys = [
      "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
      "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
    ];
};

2. Get a bun2nix binary

bun2nix has a cross-platform web-assembly based NPM package available for usage. This should be the default choice as it allows those who do not have nix installed to work on your project and keep the generated bun.nix file up to date. Simply install and run it with:

bun install --dev bun2nix
bunx bun2nix

Add the binary to your environment with nix

Alternatively, add the native bun2nix CLI program into your developer environment by adding it to your dev-shell.

devShells.default = pkgs.mkShell {
  packages = with pkgs; [
    bun
    bun2nix.packages.${system}.default
  ];
};

NOTE: the system variable can be gotten in a variety of convenient ways - including flake-utils or nix-systems.

3. Use the binary in a bun postinstall script

To keep the generated bun.nix file produced by bun2nix up to date, add bun2nix as a postinstall script to run it after every bun operation that modifies the packages in some way.

Add the following to package.json:

"scripts": {
    "postinstall": "bun2nix -o bun.nix"
}

4. Build your package with Nix

Finally, a convenient package builder is exposed inside bun2nix - mkDerivation.

Add the following to flake.nix:

A nice way to do this might be with the overlay

my-package = pkgs.callPackage ./default.nix {
    inherit bun2nix.packages.${system}.default;
};

And place this in a file called default.nix

{ bun2nix, ... }:
bun2nix.mkDerivation {
  pname = "bun2nix-example";
  version = "1.0.0";

  src = ./.;

  bunNix = ./bun.nix;

  module = "index.ts";
}

A list of available options for mkDerivation can be seen at the building packages page, along with other useful things for building bun packages.