diff --git a/.bazelrc b/.bazelrc
index 566dbb3c..3455684b 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,3 +1,5 @@
+build:bzlmod --enable_bzlmod
+
build --host_platform=@rules_nixpkgs_core//platforms:host
# Using toolchain resolution can lead to spurious dependencies on
diff --git a/.github/build-and-test b/.github/build-and-test
index ba65899c..f1c867d8 100755
--- a/.github/build-and-test
+++ b/.github/build-and-test
@@ -27,12 +27,14 @@ trap on_exit EXIT
if [[ ${BZLMOD_ENABLED-} = true ]]; then
declare -ra build_dirs=(
core
+ toolchains/java
)
declare -ra test_dirs=(
testing/core
+ testing/java
)
declare -ra extra_flags=(
- --enable_bzlmod
+ --config=bzlmod
)
else
declare -ra build_dirs=(
@@ -48,6 +50,7 @@ else
toolchains/rust
toolchains/nodejs
testing/core
+ testing/java
)
declare -ra extra_flags=(
)
diff --git a/README.md b/README.md
index cc516f12..6fd36d49 100644
--- a/README.md
+++ b/README.md
@@ -614,7 +614,8 @@ Additional arguments to forward to the underlying repository rule.
nixpkgs_java_configure(name, attribute_path, java_home_path, repository, repositories, nix_file,
nix_file_content, nix_file_deps, nixopts, fail_not_supported, quiet, toolchain,
- toolchain_name, toolchain_version, exec_constraints, target_constraints)
+ register, toolchain_name, toolchain_version, exec_constraints,
+ target_constraints)
Define a Java runtime provided by nixpkgs.
@@ -840,7 +841,21 @@ default is False
-Create & register a Bazel toolchain based on the Java runtime.
+Create a Bazel toolchain based on the Java runtime.
+
+
+
+
+
+register |
+
+
+optional.
+default is None
+
+
+
+Register the created toolchain. Requires `toolchain` to be `True`. Defaults to the value of `toolchain`.
|
diff --git a/core/.bazelrc b/core/.bazelrc
index e488a9ab..cc17469b 100644
--- a/core/.bazelrc
+++ b/core/.bazelrc
@@ -1,3 +1,5 @@
+build:bzlmod --enable_bzlmod
+
build --host_platform=@rules_nixpkgs_core//platforms:host
# User Configuration
diff --git a/registry/README.md b/registry/README.md
new file mode 100644
index 00000000..f6904729
--- /dev/null
+++ b/registry/README.md
@@ -0,0 +1,10 @@
+# Local Bazel Registry for rules\_nixpkgs modules
+
+This [local Bazel registry][registry] is only intended for testing and local
+development purposes. The rules\_nixpkgs repository is split into multiple
+Bazel modules that may depend on each other. Unfortunately, overrides such as
+`local_path_override` are only allowed in the main module, and command-line
+configuration like `--override_module` is difficult to configure correctly
+since it does not support relative paths. Instead, we use a local registry.
+
+[registry]: https://bazel.build/external/registry
diff --git a/registry/bazel_registry.json b/registry/bazel_registry.json
new file mode 100644
index 00000000..dae3ff27
--- /dev/null
+++ b/registry/bazel_registry.json
@@ -0,0 +1,4 @@
+{
+ "mirrors": [],
+ "module_base_path": ".."
+}
diff --git a/registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel b/registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel
new file mode 120000
index 00000000..cdee8c79
--- /dev/null
+++ b/registry/modules/rules_nixpkgs_core/0.9.0/MODULE.bazel
@@ -0,0 +1 @@
+../../../../core/MODULE.bazel
\ No newline at end of file
diff --git a/registry/modules/rules_nixpkgs_core/0.9.0/source.json b/registry/modules/rules_nixpkgs_core/0.9.0/source.json
new file mode 100644
index 00000000..d6f6ebe5
--- /dev/null
+++ b/registry/modules/rules_nixpkgs_core/0.9.0/source.json
@@ -0,0 +1,4 @@
+{
+ "type": "local_path",
+ "path": "core"
+}
diff --git a/registry/modules/rules_nixpkgs_core/metadata.json b/registry/modules/rules_nixpkgs_core/metadata.json
new file mode 100644
index 00000000..fede241d
--- /dev/null
+++ b/registry/modules/rules_nixpkgs_core/metadata.json
@@ -0,0 +1,6 @@
+{
+ "homepage": "",
+ "maintainers": [],
+ "versions": ["0.9.0"],
+ "yanked_versions": []
+}
diff --git a/testing/java/.bazelrc b/testing/java/.bazelrc
new file mode 120000
index 00000000..c70ccda0
--- /dev/null
+++ b/testing/java/.bazelrc
@@ -0,0 +1 @@
+../../.bazelrc
\ No newline at end of file
diff --git a/testing/java/BUILD.bazel b/testing/java/BUILD.bazel
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/java/MODULE.bazel b/testing/java/MODULE.bazel
new file mode 100644
index 00000000..92818dde
--- /dev/null
+++ b/testing/java/MODULE.bazel
@@ -0,0 +1,29 @@
+module(name = "rules_nixpkgs_java_testing")
+
+bazel_dep(name = "rules_nixpkgs_java")
+local_path_override(
+ module_name = "rules_nixpkgs_java",
+ path = "../../toolchains/java",
+)
+
+bazel_dep(name = "rules_nixpkgs_core")
+local_path_override(
+ module_name = "rules_nixpkgs_core",
+ path = "../../core",
+)
+
+bazel_dep(name = "bazel_skylib", version = "1.0.3")
+
+# TODO[AH] Remove these transitive dependencies once nixpkgs_java_configure has
+# become a module extension in rules_nixpkgs_java.
+bazel_dep(name = "platforms", version = "0.0.4")
+bazel_dep(name = "rules_java", version = "4.0.0")
+
+java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains")
+use_repo(java_toolchains, "remote_java_tools")
+
+non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")
+use_repo(non_module_deps, "nixpkgs")
+use_repo(non_module_deps, "nixpkgs_java_runtime")
+use_repo(non_module_deps, "nixpkgs_java_runtime_toolchain")
+register_toolchains("@nixpkgs_java_runtime_toolchain//:all")
diff --git a/testing/java/WORKSPACE b/testing/java/WORKSPACE
new file mode 100644
index 00000000..1c5b52a6
--- /dev/null
+++ b/testing/java/WORKSPACE
@@ -0,0 +1,33 @@
+workspace(name = "rules_nixpkgs_java_testing")
+
+local_repository(
+ name = "rules_nixpkgs_java",
+ path = "../../toolchains/java",
+)
+
+local_repository(
+ name = "rules_nixpkgs_core",
+ path = "../../core",
+)
+
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+http_archive(
+ name = "bazel_skylib",
+ sha256 = "f24ab666394232f834f74d19e2ff142b0af17466ea0c69a3f4c276ee75f6efce",
+ urls = [
+ "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
+ "https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
+ ],
+)
+
+load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
+
+bazel_skylib_workspace()
+
+load(
+ "//tests:nixpkgs_repositories.bzl",
+ "nixpkgs_repositories",
+)
+
+nixpkgs_repositories(bzlmod = False)
diff --git a/testing/java/WORKSPACE.bzlmod b/testing/java/WORKSPACE.bzlmod
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/java/flake.lock b/testing/java/flake.lock
new file mode 120000
index 00000000..23b0a9b5
--- /dev/null
+++ b/testing/java/flake.lock
@@ -0,0 +1 @@
+../../flake.lock
\ No newline at end of file
diff --git a/testing/java/nixpkgs.nix b/testing/java/nixpkgs.nix
new file mode 120000
index 00000000..ead0b005
--- /dev/null
+++ b/testing/java/nixpkgs.nix
@@ -0,0 +1 @@
+../../docs/nixpkgs.nix
\ No newline at end of file
diff --git a/testing/java/non_module_deps.bzl b/testing/java/non_module_deps.bzl
new file mode 100644
index 00000000..1e758095
--- /dev/null
+++ b/testing/java/non_module_deps.bzl
@@ -0,0 +1,11 @@
+load(
+ "//tests:nixpkgs_repositories.bzl",
+ "nixpkgs_repositories",
+)
+
+def _non_module_deps_impl(ctx):
+ nixpkgs_repositories(bzlmod = True)
+
+non_module_deps = module_extension(
+ implementation = _non_module_deps_impl,
+)
diff --git a/toolchains/java/tests/BUILD.bazel b/testing/java/tests/BUILD.bazel
similarity index 100%
rename from toolchains/java/tests/BUILD.bazel
rename to testing/java/tests/BUILD.bazel
diff --git a/toolchains/java/tests/JavaTest.java b/testing/java/tests/JavaTest.java
similarity index 100%
rename from toolchains/java/tests/JavaTest.java
rename to testing/java/tests/JavaTest.java
diff --git a/toolchains/java/tests/java-test.bzl b/testing/java/tests/java-test.bzl
similarity index 100%
rename from toolchains/java/tests/java-test.bzl
rename to testing/java/tests/java-test.bzl
diff --git a/testing/java/tests/nixpkgs_repositories.bzl b/testing/java/tests/nixpkgs_repositories.bzl
new file mode 100644
index 00000000..ad5a4429
--- /dev/null
+++ b/testing/java/tests/nixpkgs_repositories.bzl
@@ -0,0 +1,19 @@
+load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository")
+load("@rules_nixpkgs_java//:java.bzl", "nixpkgs_java_configure")
+
+def nixpkgs_repositories(*, bzlmod):
+ nixpkgs_local_repository(
+ name = "nixpkgs",
+ nix_file = "//:nixpkgs.nix",
+ nix_file_deps = ["//:flake.lock"],
+ )
+
+ nixpkgs_java_configure(
+ name = "nixpkgs_java_runtime",
+ attribute_path = "jdk11.home",
+ repository = "@nixpkgs",
+ toolchain = True,
+ register = not bzlmod,
+ toolchain_name = "nixpkgs_java",
+ toolchain_version = "11",
+ )
diff --git a/toolchains/java/.bazelrc b/toolchains/java/.bazelrc
index 566dbb3c..a3a707ee 100644
--- a/toolchains/java/.bazelrc
+++ b/toolchains/java/.bazelrc
@@ -1,3 +1,9 @@
+build:bzlmod --enable_bzlmod
+build:bzlmod --registry=file://%workspace%/../../registry
+build:bzlmod --registry=https://bcr.bazel.build
+# Skip docs directories in bzlmod mode. Stardoc is not yet bzlmod compatible.
+build:bzlmod --deleted_packages=docs
+
build --host_platform=@rules_nixpkgs_core//platforms:host
# Using toolchain resolution can lead to spurious dependencies on
diff --git a/toolchains/java/MODULE.bazel b/toolchains/java/MODULE.bazel
index 934cee67..008d8f60 100644
--- a/toolchains/java/MODULE.bazel
+++ b/toolchains/java/MODULE.bazel
@@ -5,3 +5,4 @@ module(
bazel_dep(name = "rules_nixpkgs_core", version = "0.9.0")
bazel_dep(name = "rules_java", version = "4.0.0")
+bazel_dep(name = "bazel_skylib", version = "1.0.3")
diff --git a/toolchains/java/README.md b/toolchains/java/README.md
index 5886ee5f..7ee0c053 100644
--- a/toolchains/java/README.md
+++ b/toolchains/java/README.md
@@ -18,7 +18,8 @@
nixpkgs_java_configure(name, attribute_path, java_home_path, repository, repositories, nix_file,
nix_file_content, nix_file_deps, nixopts, fail_not_supported, quiet, toolchain,
- toolchain_name, toolchain_version, exec_constraints, target_constraints)
+ register, toolchain_name, toolchain_version, exec_constraints,
+ target_constraints)
Define a Java runtime provided by nixpkgs.
@@ -244,7 +245,21 @@ default is False
-Create & register a Bazel toolchain based on the Java runtime.
+Create a Bazel toolchain based on the Java runtime.
+
+
+
+
+
+register |
+
+
+optional.
+default is None
+
+
+
+Register the created toolchain. Requires `toolchain` to be `True`. Defaults to the value of `toolchain`.
|
diff --git a/toolchains/java/WORKSPACE b/toolchains/java/WORKSPACE
index fd9f1d79..ccc6b774 100644
--- a/toolchains/java/WORKSPACE
+++ b/toolchains/java/WORKSPACE
@@ -28,11 +28,3 @@ load("@rules_nixpkgs_docs//:dependencies_2.bzl", "docs_dependencies_2")
docs_dependencies_2()
### end generic dependencies for rendering documentation
-
-### dependencies for tests
-
-load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
-
-bazel_skylib_workspace()
-
-### end dependencies for tests
diff --git a/toolchains/java/java.bzl b/toolchains/java/java.bzl
index ed583881..3231b55e 100644
--- a/toolchains/java/java.bzl
+++ b/toolchains/java/java.bzl
@@ -114,6 +114,7 @@ def nixpkgs_java_configure(
fail_not_supported = True,
quiet = False,
toolchain = False,
+ register = None,
toolchain_name = None,
toolchain_version = None,
exec_constraints = None,
@@ -181,7 +182,8 @@ def nixpkgs_java_configure(
nixopts: See [`nixpkgs_package`](#nixpkgs_package-nixopts).
fail_not_supported: See [`nixpkgs_package`](#nixpkgs_package-fail_not_supported).
quiet: See [`nixpkgs_package`](#nixpkgs_package-quiet).
- toolchain: Create & register a Bazel toolchain based on the Java runtime.
+ toolchain: Create a Bazel toolchain based on the Java runtime.
+ register: Register the created toolchain. Requires `toolchain` to be `True`. Defaults to the value of `toolchain`.
toolchain_name: The name of the toolchain that can be used in --java_runtime_version.
toolchain_version: The version of the toolchain that can be used in --java_runtime_version.
exec_constraints: Constraints for the execution platform.
@@ -233,4 +235,7 @@ def nixpkgs_java_configure(
exec_constraints = exec_constraints,
target_constraints = target_constraints,
)
- native.register_toolchains("@{}_toolchain//:all".format(name))
+ if register or register == None:
+ native.register_toolchains("@{}_toolchain//:all".format(name))
+ elif register:
+ fail("toolchain must be True if register is set to True.")