There are many security as well as practical reasons why one might need to reconfigure Bazel’s downloading behavior. One concrete case that I ran into fairly recently was Google rate-limiting our CI for an unknown reason. To work around that, I needed to redirect the downloader to a mirror. There are many ways to achieve that, like patching individual rules (tedious), using an internal registry (doesn’t solve everything), etc.
Bazel downloader config
Bazel offers a way to configure its downloader in a very simple manner. Unfortunately, it is not very well documented, but there are various resources online as well as the actual Bazel source, which explains it quite nicely. To enable it, we simply pass --downloader_config=<path_to_file> either on the command line or in .bazelrc.
File structure and syntax
The structure is easy to understand because it allows only a small set of directives:
allow host.nameto allow a specific domainblock host.nameto block a certain domain (also supportsblock *to block everything except what is explicitly allowed)rewrite pattern replacementto rewrite URLs using regexall_blocked_message message— a message shown if all candidate URLs end up blocked
Rewrite directive
Because all other directives are fairly self-explanatory, I will focus only on rewrite.
As an example, if we want to ensure that all GitHub downloads are redirected to an internal Artifactory, we could write a file like this:
rewrite github.com/(.*) internal.artifactory.example.com/$1
Of course, it is possible to define more sophisticated rewrite patterns, e.g.:
rewrite android.googlesource.com/platform/dalvik/\+archive/([0-9a-f]+)\.tar\.gz mirror.bazel.build/android.googlesource.com/platform/dalvik/+archive/$1.tar.gz
rewrite android.googlesource.com/platform/dalvik/\+archive/([0-9a-f]+)\.tar\.gz android.googlesource.com/platform/dalvik/+archive/$1.tar.gz
This rewrites requests for android.googlesource.com to mirror.bazel.build for this specific dalvik archive. The second rewrite directive ensures that Bazel falls back to the original URL if the mirror is unavailable.
Evaluation order
Bazel applies the directives in the following order, regardless of their position in the file:
rewriteallowblock
Comments
It is possible to add comments using # at the beginning of a line. Keep in mind that inline comments are not supported.
Conclusion
Typically, this is not needed very often, but it is good to keep the option in the back of your mind so you can reach for it when needed.