oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.83k stars 2.73k forks source link

Use nix to provide single command dev environment setup and reproducible builds #5001

Open adrian-gierakowski opened 1 year ago

adrian-gierakowski commented 1 year ago

What is the problem this feature would solve?

I want bun to succeed as an open source project. For that to happen, it should be easy for anyone hack on bun’s code base and contribute their improvements back.

However the instructions for setting up dev tooling are not very user friendly.

Image a js project without a package.json nor a lock file, which listed it’s npm deps in a README, some with version numbers others without.

Setting up dev environment for bun should be as easy as running npm install. Same for building, testing etc. All deps needed for this should be pinned, just like we do for js projects with lock files.

How? Easy: just use nix. Drop a flake.nix (together with flake.lock) in your repo and anyone (on any linux or Mac machine) will be able to run:

  1. nix develop - to drop into a development shell with all deps needed to build bun available and ready to use
  2. nix build - to build the bun binary (or fetch it from cache if available)
  3. nix run - to run the bun binary (built from source if a pre-built binary for current state of the repo is not available in local or remote cache)
  4. nix flake check - to run all checks (tests, lint, format etc) you’d like devs to run before opening a PR

All of these transparently cached and perfectly reproducible.

You might even be able to use this to speed up your CI pipeline ;)

The only requirement for the users would be to install nix. And hence your development setup instructions could be reduce to 2 lines of bash.

You could even provide a pre-configure instance of nvim or vscode with all tooling (extensions etc) configured and ready to go.

Finally, for people who really like containers, you could have one built with just a few lines of nix. It would contain the exact same binaries (byte for byte) provided in the dev shell by nix develop.

What is the feature you are proposing to solve the problem?

Add a flake.nix defining all dev tools and scripts needed to contribute to bun’s development

What alternatives have you considered?

Build a docker container with dev environment based on one of the popular distros Linux distros. Meh

almmiko commented 7 months ago

I've used that nix-shell to build bun on NixOS. It's not a pure shell, so you may need to extend it.

{
  description = "bun development environment";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = {
    self,
    nixpkgs,
  }: let
    supportedSystems = ["aarch64-linux" "aarch64-darwin"]; // add your system
    forEachSupportedSystem = f:
      nixpkgs.lib.genAttrs supportedSystems (system:
        f {
          pkgs = import nixpkgs {inherit system;};
        });
  in {
    devShells = forEachSupportedSystem ({pkgs}: {
      default = pkgs.mkShell {
        packages = with pkgs; [
          automake
          ccache
          coreutils-full
          gnused
          go
          libiconv
          libtool
          ninja
          pkg-config
          ruby
          rustc
          cargo
          bun
          llvmPackages_16.lldb
          llvmPackages_16.libstdcxxClang
          llvmPackages_16.libllvm
          llvmPackages_16.libcxx
          lld_16
          clang-tools
          clang_16
          autoconf
        ];

        shellHook = ''
             export CC="${pkgs.llvmPackages_16.libstdcxxClang}/bin/clang"
             export CXX="${pkgs.llvmPackages_16.libstdcxxClang}/bin/clang++"
        '';
      };
    });
  };
}