Skip to content

Commit f9ff630

Browse files
authored
Refactor flake modules (#1699)
This change is breaking for flake module users. Removes settings in the configuration, e.g. use nixos.path instead of nixos.settings.path now. Factors the logic behind installation-script out into `installation-script.nix` and doesn't use submodules anymore to reduce complexity.
1 parent d319fda commit f9ff630

File tree

12 files changed

+444
-628
lines changed

12 files changed

+444
-628
lines changed

flake-module.nix

Lines changed: 94 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,99 @@
33
flake-parts-lib,
44
...
55
}: {
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-
};
6+
options.perSystem = flake-parts-lib.mkPerSystemOption (
7+
{
8+
config,
9+
options,
10+
pkgs,
11+
...
12+
}: let
13+
namespace = "nativelink";
14+
cfg = config.${namespace};
15+
in {
16+
options.${namespace} = {
17+
enable = lib.mkOption {
18+
type = lib.types.bool;
19+
default = true;
20+
};
21+
installationScript = lib.mkOption {
22+
type = lib.types.str;
23+
default = "";
24+
description = lib.mkDoc ''
25+
A bash snippet that creates a nixos.bazelrc file in the repository.
26+
'';
27+
};
28+
api-key = lib.mkOption {
29+
type = lib.types.str;
30+
description = lib.mdDoc ''
31+
The API key to connect to the NativeLink Cloud.
32+
33+
You should only use read-only keys here to prevent cache-poisoning and
34+
malicious artifact extractions.
35+
36+
Defaults to NativeLink's shared read-only api key.
37+
'';
38+
default = "065f02f53f26a12331d5cfd00a778fb243bfb4e857b8fcd4c99273edfb15deae";
4039
};
41-
}
42-
);
43-
};
40+
endpoint = lib.mkOption {
41+
type = lib.types.str;
42+
description = lib.mdDoc ''
43+
The NativeLink Cloud endpoint.
44+
45+
Defaults to NativeLink's shared cache.
46+
'';
47+
default = "grpcs://cas-tracemachina-shared.build-faster.nativelink.net";
48+
};
49+
prefix = lib.mkOption {
50+
type = lib.types.str;
51+
description = lib.mdDoc ''
52+
An optional Bazel config prefix for the flags in `nativelink.bazelrc`.
53+
54+
If set, builds need to explicitly enable the nativelink config via
55+
`--config=<prefix>`.
56+
57+
Defaults to an empty string, enabling the cache by default.
58+
'';
59+
default = "";
60+
};
61+
};
62+
63+
config.${namespace} = {
64+
installationScript = let
65+
bazelrcContent = lib.concatLines maybePrefixedConfig;
66+
67+
# These flags cause Bazel builds to connect to NativeLink's read-only cache.
68+
#
69+
# ```nix
70+
# devShells.default = pkgs.mkShell {
71+
# shellHook = ''
72+
# # Generate the `lre.bazelrc` config file.
73+
# ${config.nativelink.installationScript}
74+
# '';
75+
# };
76+
# ```
77+
defaultConfig = [
78+
"--remote_cache=${cfg.endpoint}"
79+
"--remote_header=x-nativelink-api-key=${cfg.api-key}"
80+
"--remote_header=x-nativelink-project=nativelink-ci"
81+
"--nogenerate_json_trace_profile"
82+
"--remote_upload_local_results=false"
83+
"--remote_cache_async"
84+
];
85+
86+
# If the `nativelink.prefix` is set to a nonempty string,
87+
# prefix the Bazel build commands with that string. This will disable
88+
# connecting to the nativelink-cloud by default and require adding
89+
# `--config=<prefix>` to Bazel invocations.
90+
maybePrefixedConfig =
91+
if (cfg.prefix == "")
92+
then map (x: "build " + x) defaultConfig
93+
else map (x: "build:" + cfg.prefix + " " + x) defaultConfig;
94+
in
95+
import ./tools/installation-script.nix {
96+
inherit bazelrcContent namespace pkgs;
97+
};
98+
};
99+
}
100+
);
44101
}

flake.nix

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@
381381
nightly-rust = pkgs.rust-bin.nightly.${pkgs.lre.nightly-rust.meta.version};
382382
};
383383
};
384-
lre.settings = {
384+
lre = {
385385
Env = with pkgs.lre;
386386
if pkgs.stdenv.isDarwin
387387
then lre-rs.meta.Env # C++ doesn't support Darwin yet.
@@ -391,7 +391,7 @@
391391
then "macos"
392392
else "linux";
393393
};
394-
nixos.settings.path = with pkgs; [
394+
nixos.path = with pkgs; [
395395
"/run/current-system/sw/bin"
396396
"${binutils.bintools}/bin"
397397
"${uutils-coreutils-noprefix}/bin"
@@ -470,20 +470,21 @@
470470
# development shell.
471471
${config.pre-commit.installationScript}
472472
473-
# Generate lre.bazelrc which configures LRE toolchains when
473+
# Generate local-remote-execution.bazelrc which configures LRE toolchains when
474474
# running in the nix environment.
475475
${config.lre.installationScript}
476476
477477
# Generate nativelink.bazelrc which gives Bazel invocations access
478478
# to NativeLink's read-only cache.
479479
${config.nativelink.installationScript}
480480
481-
# If on NixOS, generate nixos.bazelrc which adds the required
481+
# If on NixOS, generate nixos.bazelrc, which adds the required
482482
# NixOS binary paths to the bazel environment.
483-
if [ -e /etc/nixos ]; then
484-
${config.nixos.installationScript}
485-
export CC=customClang
486-
fi
483+
${config.nixos.installationScript}
484+
485+
# If on Darwin, generate darwin.bazelrc, which configures darwin
486+
# libs and frameworks.
487+
${config.darwin.installationScript}
487488
488489
# The Bazel and Cargo builds in nix require a Clang toolchain.
489490
# TODO(aaronmondal): The Bazel build currently uses the
@@ -497,11 +498,6 @@
497498
export PATH=$HOME/.deno/bin:$PATH
498499
deno types > web/platform/utils/deno.d.ts
499500
''
500-
+ pkgs.lib.optionalString pkgs.stdenv.isDarwin ''
501-
# On Darwin generate darwin.bazelrc which configures
502-
# darwin libs & frameworks when running in the nix environment.
503-
${config.darwin.installationScript}
504-
''
505501
# TODO(aaronmondal): Generalize this.
506502
+ pkgs.lib.optionalString (system == "x86_64-linux") ''
507503
export CC_x86_64_unknown_linux_gnu=customClang

local-remote-execution/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ an optional `prefix` input to configure the generated `lre.bazelrc`:
106106

107107
```nix
108108
# This is a top-level field, next to `packages` and `apps`, and `devShells`
109-
lre.settings = {
109+
lre = {
110110
# In this example we import the lre-cc environment from nativelink and make it
111111
# available locally.
112112
inherit (lre-cc.meta) Env;
@@ -270,8 +270,8 @@ invoke a build against the cluster:
270270
CACHE=$(kubectl get gtw cache -o=jsonpath='{.status.addresses[0].value}')
271271
SCHEDULER=$(kubectl get gtw scheduler -o=jsonpath='{.status.addresses[0].value}')
272272
273-
# Note: If you omit setting a `prefix` the `lre.settings` you
274-
# can omit `--config=lre` here as LRE will be enabled by default.
273+
# Note: If you don't set `lre.prefix`, you can omit `--config=lre` here as
274+
# LRE will be enabled by default.
275275
bazel build \
276276
--config=lre \
277277
--remote_instance_name=main \
@@ -300,8 +300,8 @@ bazel clean
300300
301301
CACHE=$(kubectl get gtw cache -o=jsonpath='{.status.addresses[0].value}')
302302
303-
# Note: If you omit setting a `prefix` the `lre.settings` you
304-
# can omit `--config=lre` here as LRE will be enabled by default.
303+
# Note: If you don't set `lre.prefix`, you can omit `--config=lre` here as
304+
# LRE will be enabled by default.
305305
bazel build \
306306
--config=lre \
307307
--remote_instance_name=main \

0 commit comments

Comments
 (0)