Skip to content

Setup

❄️ Prepare Nix

Install Nix and enable flake support. The new experimental nix installer does this for you: https://github.com/NixOS/experimental-nix-installer

Install direnv and add the direnv hook to your .bashrc:

Terminal window
nix profile install nixpkgs#direnv
# For hooks into shells other than bash see https://direnv.net/docs/hook.html.
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
source ~/.bashrc

Now clone rules_mojo, cd into it and run direnv allow:

Terminal window
git clone git@github.com/TraceMachina/rules_mojo
cd rules_mojo
direnv allow

Inside the Nix environment you’ll have access to the mojo command:

Terminal window
# The mojo REPL.
mojo
# Build a mojo executable.
mojo build examples/hello.mojo
# Build a mojo package.
mojo package examples/mypackage
# Invoke the nix-packaged `mojo` executable
# outside of the `rules_mojo` repository:
nix run github:TraceMachina/rules_mojo#mojo

To test the mojo_toolchain with Bazel, run the Mojo standard library test suite:

Terminal window
bazel test @mojo//...

To run the example in the examples directory:

Terminal window
bazel run examples:hello

🚢 Building in Kubernetes

You can test the remote execution capabilities of rules_mojo by spinning up the builtin Kubernetes cluster with a pre-configured NativeLink setup.

NativeLink LRE Mojo Cluster

Make sure you have a recent version of Docker installed. Then you can invoke the following command from within the Nix flake:

Terminal window
lre-mojo-cluster

This spins up a local kind cluster, builds a Mojo remote execution container and deploys it into a NativeLink remote execution setup.

The setup may take a minute to boot up. When running the command the first time it might take some time to build NativeLink from source inside the cluster pipelines. Once the command finishes you can invoke the following command to run a local dashboard:

Terminal window
kubectl -n kube-system port-forward svc/hubble-ui 8080:80

Visit http://localhost:8080/?namespace=default to view the cluster topology.

To now point Bazel invocations to this cluster, invoke lre-bazel instead of bazel:

Terminal window
lre-bazel test @mojo//...
lre-bazel run examples:hello

This sends build invocations to the cluster instead of building locally on your own machine.

To delete the cluster, run:

Terminal window
lre-kill-the-mojo

🌱 Use in external projects

See the templates/default directory for templates. To start an entirely new project:

Terminal window
mkdir myproject
cd myproject
git init
nix flake init -t github:TraceMachina/rules_mojo