Skip to content

Nix flake module

The NativeLink Cloud flake module lets your contributors conveniently reuse artifacts from your CI builds.

Prerequisites

Cache sharing between CI and local development environments requires perfect reproducibility between the two.

Consider using Local Remote Execution to create environments that are reproducible across distributions.

Containerized environments that are the same for local development and CI might work as well.

Setup

  1. Add the nativelink flake module to your flake:

    flake.nix
    # In your flake inputs:
    inputs.nativelink.url = "github:TraceMachina/nativelink";
    # In your flake-parts.lib.mkFlake imports:
    imports = [
    nativelink.flakeModule
    ];
    # In your shellHook:
    devShells.default = pkgs.mkShell {
    shellHook = ''
    # Generate nativelink.bazelrc which gives Bazel invocations access
    # to NativeLink's read-only cache.
    ${config.nativelink.installationScript}
    '';
  2. Add the following to your .bazelrc:

    .bazelrc
    try-import %workspace%/nativelink.bazelrc
  3. Ignore the generated file:

    .gitignore
    nativelink.bazelrc
  4. Optionally, customize the endpoint and API key, or gate the configuration behind a --config=nativelink Bazel flag:

    flake.nix
    nativelink.settings = {
    endpoint = "grpcs://my-custom-endpoint.com";
    api-key = "my-custom-readonly-api-key";
    prefix = "nativelink";
    };

How it works

The nativelink flake module creates a nativelink.bazel file. The default configuration points to NativeLink’s public cache:

nativelink.bazelrc
# These flags are dynamically generated by the nativelink flake module.
#
# Add `try-import %workspace%/nativelink.bazelrc` to your .bazelrc to
# include these flags when running Bazel in a nix environment.
build --remote_cache=grpcs://cas-tracemachina-shared.build-faster.nativelink.net
build --remote_header=x-nativelink-api-key=065f02f53f26a12331d5cfd00a778fb243bfb4e857b8fcd4c99273edfb15deae
build --remote_instance_name=main
build --remote_header=x-nativelink-project=nativelink-ci
build --nogenerate_json_trace_profile
build --remote_upload_local_results=false
build --experimental_remote_cache_async

With the modifications from the previous section it looks like this:

nativelink.bazelrc
# These flags are dynamically generated by the nativelink flake module.
#
# Add `try-import %workspace%/nativelink.bazelrc` to your .bazelrc to
# include these flags when running Bazel in a nix environment.
build:nativelink --remote_cache=grpcs://my-custom-endpoints.com
build:nativelink --remote_header=x-nativelink-api-key=my-custom-readonly-api-key
build:nativelink --remote_header=x-nativelink-project=nativelink-ci
build:nativelink --nogenerate_json_trace_profile
build:nativelink --remote_upload_local_results=false
build:nativelink --experimental_remote_cache_async