Skip to content

buildBunModules: init - #221981

Closed
jcouyang wants to merge 4 commits into
NixOS:masterfrom
jcouyang:bun
Closed

jcouyang wants to merge 4 commits into
NixOS:masterfrom
jcouyang:bun

Conversation

@jcouyang

@jcouyang jcouyang commented Mar 19, 2023 •

Copy link
Copy Markdown
Member
Description of changes

Added buildBunModules to help build Bun project with nix

Here is a very simple example of how to package a hono service in bun:

pkgs.buildBunModules {
  pname = "bun-hono-example";
  version = "1.0.87";
  vendorSha256 = "sha256-ne/9TOoH69/DAtbR0vzEMWy2yTc3F2mRJsOU7ANEGLs=";
  src = ./.;
}

Obviously pname, version and src are mandatory, where vendorSha256 is the calculated hash of node_modules, left this empty if don't wish to verify vendor packages' integrity by nix, e.g. if you have bun.lockb already, which verify and lock all packages.

Other optional attributes you can provide are:

  • prod: default to true, only install production dependencies using bun install --production, otherwise install all dependencies.
  • bunlock: default to src + "./bun.lockb", please specify if you have bunlock in other location.

Run the test:

nix-build -A tests.bun
./result/bin/bun-hono-example start
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@jcouyang
jcouyang marked this pull request as draft March 19, 2023 08:46
@ofborg ofborg Bot added 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. labels Mar 19, 2023
@jcouyang
jcouyang marked this pull request as ready for review March 19, 2023 09:52
@jcouyang
jcouyang requested review from Mic92 and zowoq as code owners March 19, 2023 09:52
@RaitoBezarius

Copy link
Copy Markdown
Member

Adding @winterqt and @lilyinstarlight as they have (legendary) experience with Node.js ecosystem and Bun is probably sharing some of the Node.js heritage in their build system.

@happysalada

Copy link
Copy Markdown
Contributor

hey, this is looking quite good!
would you mind switching vendorSha256 to vendorHash , just to keep consistent with the renaming happening ?
Thanks a lot for taking this forward.
(I realise it's been a long time since you've gotten feedback on this, I hope you're still interested in moving this forward).

@ghost

ghost commented Sep 8, 2023

Copy link
Copy Markdown

This looks good

@winterqt winterqt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We more or less can't rely on the content of node_modules directory itself.

Take a look at how buildNpmPackage is implemented -- we implement a custom fetcher that creates a known-deterministic tree of the required dependencies.

I'm not sure how feasible this will be with bun, though, but I'm afraid this can't be merged in its current state.

@winterqt

winterqt commented Sep 9, 2023

Copy link
Copy Markdown
Member

where vendorSha256 is the calculated hash of node_modules, left this empty if don't wish to verify vendor packages' integrity by nix, e.g. if you have bun.lockb already, which verify and lock all packages.

Also, this isn't how FODs work at all, and the implementation doesn't provide support for something that reads the lockfile directly.

@happysalada

Copy link
Copy Markdown
Contributor

The lockb file is a binary file, so I see the chances of parsing it as relatively low.

@winterqt you are saying that the contents of the node_modules directory are not deterministic, so we can't rely on it, the vendorHash would potentially change. Did I understand right ?
The first version of buildNpmDeps was relying on npm ci right ? (maybe I remember wrong). If bun produces a deterministic node_modules directory, then we should be fine using vendorHash mechanism ? I could be missing something here, just trying to figure out a way around parsing the lockfile.

@Lord-Valen

Lord-Valen commented Sep 11, 2023 •

Copy link
Copy Markdown
Contributor

It's possible to build a yarn lockfile alongside bun.lockb via cli flag or bunfig.toml. https://bun.sh/docs/install/lockfile

I'd rather we didn't have to resort to such things and could handle the default case (handle lockb files). Fortunately, running bun on a bun.lockb returns an autogenerated yarn lockfile.

~/bun-test via 🍞 v0.6.2 via  v18.17.1
❯ bun bun.lockb
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# bun ./bun.lockb --hash: B17D8754EFAAD092-9e922113a65956d7-A0C41F8A3F86CE81-04470ad34b5699cc


bun-types@^0.5.0:
  version "0.5.8"
  resolved "https://registry.npmjs.org/bun-types/-/bun-types-0.5.8.tgz"
  integrity sha512-VHwD0MAHo3wraYAeqTWH2NDmXOdGfC3wWWOnZvK93ytI6yq/LkgsCjDudWNmN7MlfPvJb2zoLMnkzhjxNwLsLw==

They suggest using this for git diffing.

@happysalada

Copy link
Copy Markdown
Contributor

That could be the way forward for now.
I havent seen any code to parse the yarn lock file, but hopefully it should be close enough to the paclage-lock code.

@cdmistman

Copy link
Copy Markdown
Contributor

Fortunately, running bun on a bun.lockb returns an autogenerated yarn lockfile.

the lockfile itself is executable as well, though probably should stick with bun bun.lockb

I havent seen any code to parse the yarn lock file, but hopefully it should be close enough to the paclage-lock code.

you're probably blocked on a yarn2nix refresh, there are a lot of open issues around it...

@happysalada

Copy link
Copy Markdown
Contributor

I was thinking about a similar method than what is used here.
https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/servers/web-apps/discourse/default.nix#L203
use fetchYarnDeps for the dependencies and then try to install with yarn and offline mirror.
maybe we need to test with a couple of packages manually (by using these manual steps) before we can make the right abstraction.

@cdmistman

Copy link
Copy Markdown
Contributor

use fetchYarnDeps for the dependencies and then try to install with yarn and offline mirror

for clarity: do you mean you intend to remove bun from the installation process? so use it to get the locked deps but then only use yarn?

@Lord-Valen

Copy link
Copy Markdown
Contributor

I think we should close this PR and open an issue for discussion. I think it's fairly clear that this PR won't be seeing any more work, especially since we haven't decided on how this should be implemented.

@happysalada

Copy link
Copy Markdown
Contributor

let's use this issue if this works for you #255890
I've got something that works on local to get people moving forward today (this probably won't be good enough to be accepted in nixpkgs, but this provides an example until then. I've tried to add comments, but feel free to ask questions if anything seems weird).

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

Labels

8.has: package (new) This PR adds a new package 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants