Skip to content

Experimenting with bazel, nix & multiple languages in a monorepo.

Notifications You must be signed in to change notification settings

kourtni/polyglot-bazel-nix-monorepo

 
 

Repository files navigation

Polyglot Bazel Nix Monorepo

A Nixified fork of kriscfoster/multi-language-bazel-monorepo

I wanted to take a multi-language monorepo that was already using bazel and update it to integrate Nix to enable reproducible development environments. Luckily @kriscfoster had already created a multi-language monorepo, so this fork will attempt to integrate Nix and create a good baseline development environment.

Note: Recently added a copy of https://github.com/aspect-build/bazel-examples/tree/main/react-cra as projects/react-cra. It is setup as its own Bazel workspace, but I am trying to get that example project to build as a project in a larger monorepo. Previous attempts have resulted in me filing a bug but it turned out that I just sucked at integrating it as a project in a larger workspace.🤣🤦🏿‍♂️

The goal is to support development in linux compatible environments, including:

  • Linux (most, if not all, flavors)
  • Windows running WSL2
  • MacOS (required for any iOS development)

Prerequisites

Since the whole point here is to automate the dev environment there are only a few components required to be on your system.

  1. devenv - following their guide will install all items in this list
  2. nix
  3. direnv - comes pre-installed on some systems

Useful Commands

For now I'm leaving the commands here that @kriscfoster listed. It's not clear if some of them may need to be updated once Nix has been fully integrated, but will updated them as needed.

Build all targets

  • bazel build //...

Test all test targets

  • bazel test //...

Run Java Greeter app

Sync Go Dependencies with go.mod

  • bazel run //:gazelle-update-repos

Format BUILD.bazel files (completely generates BUILd.bazel files for go projects)

  • bazel run //:gazelle

Run Python web app

Run Go web app

Run NodeJS web app

Run TypeScript app

  • bazel run //projects/ts_app

Run React app

  • bazel run //projects/react_app:start

Run React CRA app

  • Currently must cd into projects/react-cra then run yarn start or bazel run :start
  • TODO: integrate react-cra project into monorepo workspace

Build & Run NodeJS web app docker image

➜ bazel run projects/node_web:node_web_image --@io_bazel_rules_docker//transitions:enable=yes -- --norun
...
INFO: Build completed successfully, 1 total action
Loaded image ID: sha256:XXX
Tagging YYY as bazel/projects/node_web:node_web_image
➜
➜ docker run -p 8080:8080 bazel/projects/node_web:node_web_image
listening on port 8080

Build & Run Go web app docker image

➜ bazel run projects/go_web:go_web_image --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 -- --norun
...
INFO: Build completed successfully, 1 total action
Loaded image ID: sha256:XXX
Tagging YYY as bazel/projects/go_web:go_web_image
➜
➜ docker run -p 8080:8080 bazel/projects/go_web:go_web_image
2022/05/30 20:35:51 Going to listen on port 8080

Build & Run Go web app docker image (custom base)

➜ bazel run projects/go_web:go_web_image_custom_base --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 -- --norun
...
INFO: Build completed successfully, 1 total action
Loaded image ID: sha256:XXX
Tagging YYY as bazel/projects/go_web:go_web_image_custom_base
➜
➜ docker run -p 8080:880 bazel/projects/go_web:go_web_image_custom_base
2023/03/15 21:29:46 Going to listen on port 8080

Build & Run Python web app docker image

➜ bazel run projects/python_web:python_web_image -- --norun
...
INFO: Build completed successfully, 1 total action
Loaded image ID: sha256:XXX
Tagging YYY as bazel/projects/python_web:python_web_image
➜
➜ docker run -p 5000:5000 bazel/projects/python_web:python_web_image
...
* Running on http://127.0.0.1:5000

Publishing Python web app docker image

➜ bazel run projects/python_web:publish         
...
INFO: Build completed successfully, 1 total action
2022/06/24 20:13:33 Successfully pushed Docker image to registry.hub.docker.com/krisfoster96/monorepo-python-web:1 - registry.hub.docker.com/krisfoster96/monorepo-python-web@sha256:024bcf5dd677d6fbce32fcf9d09329f4c80931cc12c90965bb397af1f497bf39

Deploying Go web app to Heroku

➜ bazel run projects/go_web:bazoku-deployment --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
...

About

Experimenting with bazel, nix & multiple languages in a monorepo.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Starlark 58.0%
  • HTML 9.9%
  • JavaScript 8.2%
  • TypeScript 6.2%
  • Nix 5.5%
  • CSS 5.3%
  • Other 6.9%