1diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools
2index a8af76e90c..7f8b030f63 100644
3--- a/tools/jdk/BUILD.tools
4+++ b/tools/jdk/BUILD.tools
5@@ -146,6 +146,25 @@ py_test(
6 ],
7 )
8
9+##### Nonprebuilt toolchains definitions for NixOS and nix build sandboxes ####
10+
11+load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain", "NONPREBUILT_TOOLCHAIN_CONFIGURATION")
12+
13+[
14+ default_java_toolchain(
15+ name = "nonprebuilt_toolchain_java" + str(version),
16+ configuration = NONPREBUILT_TOOLCHAIN_CONFIGURATION,
17+ java_runtime = "@local_jdk//:jdk",
18+ source_version = str(version),
19+ target_version = str(version),
20+ )
21+ # Ideally we would only define toolchains for the java versions that the
22+ # local jdk supports. But we cannot access this information in a BUILD
23+ # file, and this is a hack anyway, so just pick a large enough upper bound.
24+ # At the current pace, java <= 30 should cover all realeases until 2028.
25+ for version in range(8, 31)
26+]
27+
28 #### Aliases to rules_java to keep backward-compatibility (begin) ####
29
30 TARGET_NAMES = [