|
| 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