Skip to content

Commit 26df13b

Browse files
authored
Introduce the NativeLink Cloud flake module (TraceMachina#1365)
This change enables LRE by default and adds a config file that connects Nix users to the NativeLink Cloud. This means that users running Bazel from within the nix flake can fetch artifacts directly from CI builds.
1 parent 9be5902 commit 26df13b

File tree

10 files changed

+334
-33
lines changed

10 files changed

+334
-33
lines changed

.bazelrc

+3
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@ try-import %workspace%/lre.bazelrc
8484
# Generated by the darwin flake module.
8585
try-import %workspace%/darwin.bazelrc
8686

87+
# Generated by the nativelink flake module.
88+
try-import %workspace%/nativelink.bazelrc
89+
8790
# Allow user-side customization.
8891
try-import %workspace%/user.bazelrc

.github/workflows/lre.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ jobs:
5050
run: >
5151
nix develop --impure --command
5252
bash -c "bazel run \
53-
--config=lre \
5453
--verbose_failures \
5554
@local-remote-execution//examples:hello_lre"
5655
@@ -122,7 +121,6 @@ jobs:
122121
run: >
123122
nix develop --impure --command
124123
bash -c "bazel run \
125-
--config=lre \
126124
--remote_instance_name=main \
127125
--remote_cache=grpc://$cache_ip \
128126
--remote_executor=grpc://$scheduler_ip \

.github/workflows/main.yml

+33-26
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,43 @@ jobs:
6666
//...
6767
6868
nativelink-dot-com-cloud-cache-legacy-dockerfile-test:
69-
runs-on: ubuntu-22.04
69+
runs-on: ubuntu-24.04
7070
environment: production
71-
name: NativeLink.com Cloud / Remote Cache (Legacy Dockerfile Test)
71+
name: NativeLink.com Cloud / Remote Cache (LRE)
7272
env:
7373
NL_COM_API_KEY: ${{ secrets.NATIVELINK_COM_API_HEADER || '065f02f53f26a12331d5cfd00a778fb243bfb4e857b8fcd4c99273edfb15deae' }}
7474
steps:
75-
- name: Checkout
76-
uses: >- # v4.1.1
77-
actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
78-
79-
- name: Setup Bazelisk
80-
uses: >- # v0.8.1
81-
bazel-contrib/setup-bazel@b388b84bb637e50cdae241d0f255670d4bd79f29
82-
with:
83-
bazelisk-cache: true
84-
85-
- name: Run Bazel tests
86-
shell: bash
87-
run: |
88-
bazel test \
89-
--remote_cache=grpcs://cas-tracemachina-shared.build-faster.nativelink.net \
90-
--remote_header=x-nativelink-api-key=$NL_COM_API_KEY \
91-
--remote_instance_name=main \
92-
--bes_backend=grpcs://bes-tracemachina-shared.build-faster.nativelink.net \
93-
--bes_header=x-nativelink-api-key=$NL_COM_API_KEY \
94-
--bes_results_url=https://app.nativelink.com/a/e3b1e0e0-4b73-45d6-85bc-5cb7b02edea5/build \
95-
--remote_header=x-nativelink-project=nativelink-ci \
96-
--jobs=200 \
97-
${{ github.ref == 'refs/heads/main' && ' ' || '--nogenerate_json_trace_profile --remote_upload_local_results=false' }} \
98-
//...
75+
- name: Checkout
76+
uses: >- # v4.1.1
77+
actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
78+
79+
- name: Free disk space
80+
uses: >- # v2.0.0
81+
endersonmenezes/free-disk-space@3f9ec39ebae520864ac93467ee395f5237585c21
82+
with:
83+
remove_android: true
84+
remove_dotnet: true
85+
remove_haskell: true
86+
remove_tool_cache: false
87+
88+
- name: Install Nix
89+
uses: >- # v10
90+
DeterminateSystems/nix-installer-action@de22e16c4711fca50c816cc9081563429d1cf563
91+
92+
- name: Cache Nix derivations
93+
uses: >- # v4
94+
DeterminateSystems/magic-nix-cache-action@fc6aaceb40b9845a02b91e059ec147e78d1b4e41
95+
96+
- name: Run Bazel tests
97+
run: >
98+
nix develop --impure --command
99+
bash -c "bazel test \
100+
--remote_header=x-nativelink-api-key=$NL_COM_API_KEY \
101+
--bes_backend=grpcs://bes-tracemachina-shared.build-faster.nativelink.net \
102+
--bes_header=x-nativelink-api-key=$NL_COM_API_KEY \
103+
--bes_results_url=https://app.nativelink.com/a/e3b1e0e0-4b73-45d6-85bc-5cb7b02edea5/build \
104+
${{ github.ref == 'refs/heads/main' && ' ' || '--nogenerate_json_trace_profile --remote_upload_local_results=false' }} \
105+
//..."
99106
100107
docker-compose-compiles-nativelink:
101108
# The type of runner that the job will run on.

.github/workflows/nix.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
os: [ubuntu-22.04, macos-13, macos-14]
23+
os: [ubuntu-24.04, macos-13, macos-14]
2424
name: Bazel Dev / ${{ matrix.os }}
2525
runs-on: ${{ matrix.os }}
2626
timeout-minutes: 45

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Pulumi.dev.yaml
2020
lre.bazelrc
2121
rust-project.json
2222
darwin.bazelrc
23+
nativelink.bazelrc

flake-module.nix

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
lib,
3+
flake-parts-lib,
4+
...
5+
}: {
6+
options = {
7+
perSystem = flake-parts-lib.mkPerSystemOption (
8+
{
9+
config,
10+
options,
11+
pkgs,
12+
...
13+
}: let
14+
cfg = config.nativelink;
15+
in {
16+
options = {
17+
nativelink = {
18+
pkgs = lib.mkOption {
19+
type = lib.types.uniq (lib.types.lazyAttrsOf (lib.types.raw or lib.types.unspecified));
20+
description = "Nixpkgs to use.";
21+
default = pkgs;
22+
defaultText = lib.literalMD "`pkgs` (module argument)";
23+
};
24+
settings = lib.mkOption {
25+
type = lib.types.submoduleWith {
26+
modules = [./modules/nativelink.nix];
27+
specialArgs = {inherit (cfg) pkgs;};
28+
};
29+
default = {};
30+
description = "Configuration for Bazel on Darwin.";
31+
};
32+
installationScript = lib.mkOption {
33+
type = lib.types.str;
34+
description = "Create nativelink.bazelrc.";
35+
default = cfg.settings.installationScript;
36+
defaultText = lib.literalMD "bazelrc content";
37+
readOnly = true;
38+
};
39+
};
40+
};
41+
}
42+
);
43+
};
44+
}

flake.nix

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
inputs.git-hooks.flakeModule
4343
./local-remote-execution/flake-module.nix
4444
./tools/darwin/flake-module.nix
45+
./flake-module.nix
4546
];
4647
perSystem = {
4748
config,
@@ -420,7 +421,7 @@
420421
if pkgs.stdenv.isDarwin
421422
then [] # Doesn't support Darwin yet.
422423
else lre-cc.meta.Env;
423-
prefix = "lre";
424+
prefix = "linux";
424425
};
425426
devShells.default = pkgs.mkShell {
426427
nativeBuildInputs = let
@@ -482,10 +483,14 @@
482483
# development shell.
483484
${config.pre-commit.installationScript}
484485
485-
# Generate lre.bazelrc which configures LRE toolchains when running
486-
# in the nix environment.
486+
# Generate lre.bazelrc which configures LRE toolchains when
487+
# running in the nix environment.
487488
${config.local-remote-execution.installationScript}
488489
490+
# Generate nativelink.bazelrc which gives Bazel invocations access
491+
# to NativeLink's read-only cache.
492+
${config.nativelink.installationScript}
493+
489494
# The Bazel and Cargo builds in nix require a Clang toolchain.
490495
# TODO(aaronmondal): The Bazel build currently uses the
491496
# irreproducible host C++ toolchain. Provide
@@ -508,8 +513,9 @@
508513
}
509514
// {
510515
flakeModule = {
511-
default = ./local-remote-execution/flake-module.nix;
516+
default = ./flake-module.nix;
512517
darwin = ./tools/darwin/flake-module.nix;
518+
local-remote-execution = ./local-remote-execution/flake-module.nix;
513519
};
514520
};
515521
}

modules/nativelink.nix

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
...
6+
}: let
7+
# These flags cause Bazel builds to connect to NativeLink's read-only cache.
8+
#
9+
# ```nix
10+
# devShells.default = pkgs.mkShell {
11+
# shellHook = ''
12+
# # Generate the `lre.bazelrc` config file.
13+
# ${config.nativelink.installationScript}
14+
# '';
15+
# };
16+
# ```
17+
defaultConfig = [
18+
"--remote_cache=${config.endpoint}"
19+
"--remote_header=x-nativelink-api-key=${config.api-key}"
20+
"--remote_instance_name=main"
21+
"--remote_header=x-nativelink-project=nativelink-ci"
22+
"--nogenerate_json_trace_profile"
23+
"--remote_upload_local_results=false"
24+
"--experimental_remote_cache_async"
25+
];
26+
27+
# If the `nativelink.settings.prefix` is set to a nonempty string,
28+
# prefix the Bazel build commands with that string. This will disable
29+
# connecting to the nativelink-cloud by default and require adding
30+
# `--config=<prefix>` to Bazel invocations.
31+
maybePrefixedConfig =
32+
if (config.prefix == "")
33+
then map (x: "build " + x) defaultConfig
34+
else map (x: "build:" + config.prefix + " " + x) defaultConfig;
35+
36+
configFile = pkgs.runCommand "nativelink.bazelrc" {} ''
37+
printf '# These flags are dynamically generated by the nativelink flake module.
38+
#
39+
# Add `try-import %%workspace%%/nativelink.bazelrc` to your .bazelrc to
40+
# include these flags when running Bazel in a nix environment.
41+
42+
${lib.concatLines maybePrefixedConfig}' >$out
43+
'';
44+
in {
45+
options = {
46+
installationScript = lib.mkOption {
47+
type = lib.types.str;
48+
description = lib.mkDoc ''
49+
A bash snippet which creates a nativelink.bazelrc file in the repository.
50+
'';
51+
};
52+
endpoint = lib.mkOption {
53+
type = lib.types.str;
54+
description = lib.mdDoc ''
55+
The NativeLink Cloud endpoint.
56+
57+
Defaults to NativeLink's shared cache.
58+
'';
59+
default = "grpcs://cas-tracemachina-shared.build-faster.nativelink.net";
60+
};
61+
api-key = lib.mkOption {
62+
type = lib.types.str;
63+
description = lib.mdDoc ''
64+
The API key to connect to the NativeLink Cloud.
65+
66+
You should only use read-only keys here to prevent cache-poisoning and
67+
malicious artifact extractions.
68+
69+
Defaults to NativeLink's shared read-only api key.
70+
'';
71+
default = "065f02f53f26a12331d5cfd00a778fb243bfb4e857b8fcd4c99273edfb15deae";
72+
};
73+
prefix = lib.mkOption {
74+
type = lib.types.str;
75+
description = lib.mdDoc ''
76+
An optional Bazel config prefix for the flags in `nativelink.bazelrc`.
77+
78+
If set, builds need to explicitly enable the nativelink config via
79+
`--config=<prefix>`.
80+
81+
Defaults to an empty string, enabling the cache by default.
82+
'';
83+
default = "";
84+
};
85+
};
86+
87+
config = {
88+
installationScript = ''
89+
if ! type -t git >/dev/null; then
90+
# In pure shells
91+
echo 1>&2 "WARNING: NativeLink: git command not found; skipping installation."
92+
elif ! ${pkgs.git}/bin/git rev-parse --git-dir &> /dev/null; then
93+
echo 1>&2 "WARNING: NativeLink: .git not found; skipping installation."
94+
else
95+
GIT_WC=`${pkgs.git}/bin/git rev-parse --show-toplevel`
96+
97+
# These update procedures compare before they write, to avoid
98+
# filesystem churn. This improves performance with watch tools like
99+
# lorri and prevents installation loops by lorri.
100+
101+
if ! readlink "''${GIT_WC}/nativelink.bazelrc" >/dev/null \
102+
|| [[ $(readlink "''${GIT_WC}/nativelink.bazelrc") != ${configFile} ]]; then
103+
echo 1>&2 "NativeLink: updating $PWD repository"
104+
[ -L nativelink.bazelrc ] && unlink nativelink.bazelrc
105+
106+
if [ -e "''${GIT_WC}/nativelink.bazelrc" ]; then
107+
echo 1>&2 "NativeLink: WARNING: Refusing to install because of pre-existing nativelink.bazelrc"
108+
echo 1>&2 " Remove the nativelink.bazelrc file and add nativelink.bazelrc to .gitignore."
109+
else
110+
ln -fs ${configFile} "''${GIT_WC}/nativelink.bazelrc"
111+
fi
112+
fi
113+
fi
114+
'';
115+
};
116+
}

0 commit comments

Comments
 (0)