@@ -243,6 +243,76 @@ existing attributes:
243243* Adding additional Python versions via {bzl: obj }` python.single_version_override ` or
244244 {bzl: obj }` python.single_version_platform_override ` .
245245
246+ ### Registering custom runtimes
247+
248+ Because the python-build-standalone project has _ thousands_ of prebuilt runtimes
249+ available, rules_python only includes popular runtimes in its built in
250+ configurations. If you want to use a runtime that isn't already known to
251+ rules_python then {obj}` single_version_platform_override() ` can be used to do
252+ so. In short, it allows specifying an arbitrary URL and using custom flags
253+ to control when a runtime is used.
254+
255+ In the example below, we register a particular python-build-standalone runtime
256+ that is activated for Linux x86 builds when the custom flag
257+ ` --//:runtime=my-custom-runtime ` is set.
258+
259+ ```
260+ # File: MODULE.bazel
261+ bazel_dep(name = "bazel_skylib", version = "1.7.1.")
262+ bazel_dep(name = "rules_python", version = "1.5.0")
263+ python = use_extension("@rules_python//python/extensions:python.bzl", "python")
264+ python.toolchain(python_version="3.13.3")
265+ python.single_version_platform_override(
266+ platform = "my-platform",
267+ python_version = "3.13.3",
268+ sha256 = "01d08b9bc8a96698b9d64c2fc26da4ecc4fa9e708ce0a34fb88f11ab7e552cbd",
269+ target_compatible_with = [
270+ "@platforms//os:linux",
271+ "@platforms//cpu:x86_64",
272+ ],
273+ target_settings = [
274+ "@@//:runtime=my-custom-runtime",
275+ ],
276+ urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250409/cpython-3.13.3+20250409-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"],
277+ )
278+ # File: //:BUILD.bazel
279+ load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
280+ string_flag(
281+ name = "custom_runtime",
282+ build_setting_default = "",
283+ )
284+ config_setting(
285+ name = "is_custom_runtime_linux-x86-install-only-stripped",
286+ flag_values = {
287+ ":custom_runtime": "linux-x86-install-only-stripped",
288+ },
289+ )
290+ ```
291+
292+ Notes:
293+ - While any URL and archive can be used, it's assumed their content looks how
294+ a python-build-standalone archive looks.
295+ - ` python.toolchain() ` is required if the version is unknown; if the version
296+ is already known, it can be omitted.
297+ - A "version aware" toolchain is registered, which means the Python version flag
298+ must also match (e.g. ` --@rules_python//python/config_settings:python_version=3.13.3 `
299+ must be set -- see ` minor_mapping ` and ` is_default ` for controls and docs
300+ about version matching and selection).
301+ - The labels in ` target_settings ` must be absolute; ` @@ ` refers to the main repo.
302+ - The ` target_settings ` are ` config_setting ` targets, which means you can
303+ customize how matching occurs.
304+
305+ :::{seealso}
306+ See {obj}` //python/config_settings ` for flags rules_python already defines
307+ that can be used with ` target_settings ` . Some particular ones of note are:
308+ {flag}` --py_linux_libc ` and {flag}` --py_freethreaded ` , among others.
309+ :::
310+
311+ :::{versionadded} VERSION_NEXT_FEATURE
312+ Added support for custom platform names, ` target_compatible_with ` , and
313+ ` target_settings ` with ` single_version_platform_override ` .
314+ :::
315+
246316### Using defined toolchains from WORKSPACE
247317
248318It is possible to use toolchains defined in ` MODULE.bazel ` in ` WORKSPACE ` . For example
0 commit comments