Repository navigation
Open
Labels
0.kind: enhancementAdd something new or improve an existing system.
Description
Project description
https://bun.sh/ is a great project, however we still don't have a build system for it in nixpkgs.
This issue is to discuss how to solve this.
Discussion is continued from an initial PR #221981
Metadata
how do build a bun project today with a nix expression (this is rough, and so can't be accepted as it into nixpkgs, but should help you get something running until we figure everything out).
Currently I'm using the following to build a bun project.
node_modules = with pkgs; stdenv.mkDerivation {
pname = "document-search-node_modules";
version = "0.0.1";
impureEnvVars = lib.fetchers.proxyImpureEnvVars
++ [ "GIT_PROXY_COMMAND" "SOCKS_SERVER" ];
src = ./.;
nativeBuildInputs = [ bun ];
buildInputs = [ nodejs-slim_latest ];
dontConfigure = true;
buildPhase = ''
bun install --no-progress --frozen-lockfile
'';
installPhase = ''
mkdir -p $out/node_modules
cp -R ./node_modules $out
'';
outputHash = if stdenv.isLinux then "sha256-vNPLtQVDLeR+TenFM0SIQ7rminT3fOUN98obaibeEW4=" else "sha256-nkPbmg/kG8Id8nGYz2x0drarDD/1qsCPL4Dgg21tmNw=";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
};
document-search = with pkgs; stdenv.mkDerivation {
pname = "document-search";
version = "0.0.1";
src = ./.;
nativeBuildInputs = [ makeBinaryWrapper ];
buildInputs = [ bun ];
configurePhase = ''
runHook preConfigure
# node modules need to be copied to substitute for reference
# substitution step cannot be done before otherwise
# nix complains about unallowed reference in FOD
cp -R ${node_modules}/node_modules .
# bun installs .bin package with a usr bin env ref to node
# replace any ref for bin that are used
substituteInPlace node_modules/.bin/vite \
--replace "/usr/bin/env node" "${nodejs-slim_latest}/bin/node"
runHook postConfigure
'';
env.UNSTRUCTURED_API_KEY = "REPLACE_ME_UNSTRUCTURED_API_KEY";
env.HUGGINGFACE_API_TOKEN = "REPLACE_ME_HUGGINGFACE_API_TOKEN";
buildPhase = ''
runHook preBuild
bun run build
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/bin
ln -s ${node_modules}/node_modules $out
cp -R ./build/* $out
makeBinaryWrapper ${bun}/bin/bun $out/bin/document-search \
# bun is referenced naked in the package.json generated script
--prefix PATH : ${lib.makeBinPath [ bun ]} \
--add-flags "run --prefer-offline --no-install --cwd $out start"
runHook postInstall
'';
};
keep in mind this is just an example (it's a sveltekit project using vite, so your mileage may vary).
Challenges ahead.
- I'm not 100% sure the
bun --installcommand is fully reproducible. we should potentially confirm that with upstream (even with a lockfile). - Building the node_modules directory, should we rely on bun, or should we
bun install --yarnand try to parse the yarn.lock v1 file to contruct the node_modules directory. (in the above example, I'm just using bun, but I'm not sure this can be relied on).
Reacted by dawn, Jack5079, elohmeier, Bruno Bigras, Guanran Wang, Donovan Glover, Michael Thomas, medv, tsandrini, April and 27 more
Reacted by medv, tsandrini, alex, Johannes Schickling, Dmitrii Stepanidenko, sokai and Changsheng Wu
Metadata
Metadata
Assignees
Labels
0.kind: enhancementAdd something new or improve an existing system.
Activity
happysalada commentedon Oct 16, 2023
I've been using Fixed Output Derviation for almost a month now with bun, and it looks like the output of bun install is stable.
I'm planning to wait until after the 23.11 release before making a PR for buildBunModules with FOD. I think we have to test it at a bigger scale next to surface potential bugs. People on bun have been super responsive so far, so if we find something, there is a good chance there is a solution.
What I still don't know about is the stability of the sha-256 for depedencies across platforms. I think it will be different on darwin and linux, just because the output of the node_modules is probably different. This will have to be verified when there is a PR.
cdmistman commentedon Oct 16, 2023
do you know if this affects any yarn2nix projects?
cdmistman commentedon Oct 16, 2023
also, were you able to verify that
bun --installis fully reproducible?happysalada commentedon Oct 16, 2023
I dont think yarn2nix projects are affected (since what i said is bun spevific, i have never checked the internals of yarn2nix)
I couldnt verify that its fully reproducible everytime, i just havent found a problem yet. I believe asking more people to test might surface some problems. For my package on darwin i havent had a problem yet.
happysalada commentedon Oct 31, 2023
I'm monitoring issues in the bun repository and there are still a lof of issues open about how bun doesn't generate the correct nodes_modules.
I'll keep an eye on this, and when it's more stable, I think the solution in this thread is not out of the question.
nyabinary commentedon Jan 12, 2024
Any updates?
happysalada commentedon Jan 25, 2024
I'm making some tests with a small package
#283643
this should be abstractable into some functions later for convenience, I'm going to try to make sure that bun installation from lockfile is actually reproducible.
The one thing that is going to be painful for anyone, is that every platform will have a different sha256 hash for the dependencies, that won't make any updates easy unfortunately.
nyabinary commentedon Feb 1, 2024
Nice, this would make Dorion packaging much easier (#265771)
Alexnortung commentedon Jun 16, 2024
I have been trying to bundle my own bun app.
I think it might be worth to use the
--compileand--minifyflag in the build step, so that you do not necessarily need to install all the source code.For my app, I have the following build and install phas
Is this a good idea or is it better to use bun as a
nativeBuildInput, such that it can be reused across other packages? If that is the case I still think--minifycould be worth it.But yeah, thanks for making this, it really helped me package my app :)
Alexnortung commentedon Jun 16, 2024
I also want to mention that you could use the version and package name from the
package.jsonfile as default:happysalada commentedon Jun 16, 2024
Thanks for sharing this, i like both of your suggestions !
Alexnortung commentedon Jun 16, 2024
Alright, I compared the output of using
--compileand not using it. And it seems to be much better to use it without--compile. Here is the final build and install phaseI know that this is only for compiling a single entrypoint without a custom build script. But I think it is worth sharing anyway.
eveeifyeve commentedon Aug 21, 2024
I feel like a hook for this would be better #335534 has an issue that should add the hook to it.
tsandrini commentedon Aug 25, 2024
Hi, thanks for the already done work, I wanted to share some of my experience while packaging a static Astro site built with bun via nix. Here is the snippet that I ended up with
And some notes/issues that I faced
node_modules, however, runningpatchShebangsproducederror: illegal path references in fixed-output derivationand since without the patched shebangs the build didn't work I was forced to copy (instead of symlinking it as per usual) thenode_modulesderivation and modify permissions + shebangs afterwards. This feels a bit suboptimal and partly defeats the purpose of FODs in the first place.node_modules(specificallynode_modules/.astroandnode_modules/.vite) during the build, which means that thenode_modulesdirectory needs to be writeable as well. Afaik there are no environment variables or flags to override this behaviour, the only thing you can do is to setcacheDir = 'some-other-dir';fromastro.config.mjsandvite.config.tswhich won't be guaranteed for most projects (since the default is withinnode_modulesas I stated earlier)astroexecutable.bun ./node_modules/.bin/astro ...,bunx astroandbun x astrodidn't work at all. I played around with$PATHand$NODE_PATHand ended up with the combination above + thebun astro ...command format + modifying thenode_modules/.binfolder permissions, however, I am not going to pretend that I really understand why it didn't work to begin with.$HOME, so it might be a good idea to setHOME=$TMPDIReveeifyeve commentedon Sep 8, 2024
?
honnip commentedon Nov 4, 2024
Bun creates a cache to
node_modules/.cache/1234asdf.npmand it is completely non deterministic:Installing with
--no-cacheflag will solve:jakehamilton commentedon Jan 23, 2025
Bun 1.2 now uses a text lock file, we may be able to parse this and handle dependencies similar to how we do for package-lock.json in the npm builder.
eveeifyeve commentedon Jan 23, 2025
HI thanks for the reply I am going to try to implement as much of this issue #335534 which I think the hooks way is much better than a builder, today.
dmitriiStepanidenko commentedon Sep 6, 2025
@honnip sorry for disturbance. Can you replicate non deterministic ".cache" behavior under recent bun in nixpkgs-unstable? I try this on other package and bun don't even create ".cache" folder in node_modules ... Am I doing something wrong or something changed?Yes, it's mandatory to use "--no-cache" with bun install. And i don't understand what kind of black magic was pulled, but in my case "--no-cache" works only with "writableTmpDirAsHomeHook" in "nativeBuildInputs". I assume, that under the hood bun using /tmp, but it's too strange to be true.
ccusage package
Cu3PO42 commentedon Oct 27, 2026
Since Bun 1.4.4 recently added
bun pm fetchwhich makes this much easier, I decided to take a stab at building abuildBunPackagethat tries to be as close tobuildNpmPackageas reasonably possible. It needs neither generated Nix files nor IFD. For now it's available in a separate repo.I've tested it on a few packages and it seems to be working as expected, however I wouldn't yet call this done.