diff --git a/distribution/BUCK b/distribution/BUCK index 2218f792..21f92ae3 100644 --- a/distribution/BUCK +++ b/distribution/BUCK @@ -63,6 +63,31 @@ box.sh_test( ], ) +# distribution.packages() adds the list of the family's key and the list of the release's key to the common +# packages. The arch family has no key, so arch.rolling gets the list of the `DEFAULT` key. The arguments +# are the packages that distribution.packages() returns, `--`, and the expected packages from a plain +# select(). +box.sh_test( + name = "packages-test", + box = "//tools:dev.box", + script = 'actual=(); while [[ "$1" != -- ]]; do actual+=("$1"); shift; done; shift; [[ "${actual[*]}" == "$*" ]]', + args = distribution.packages( + ["common"], + { + "DEFAULT": ["default"], + "debian": ["family"], + "fedora": ["family"], + "fedora.rawhide": ["release"], + }, + ) + + ["--"] + + select({ + "catalog//:arch.rolling.distribution": ["common", "default"], + "catalog//:debian.testing.distribution": ["common", "family"], + "catalog//:fedora.rawhide.distribution": ["common", "family", "release"], + }), +) + # A box whose release is a select() on the distribution takes this as its incoming transition. distribution.box_host(name = "release.host") diff --git a/distribution/defs.bzl b/distribution/defs.bzl index ce2545f2..1848d5c6 100644 --- a/distribution/defs.bzl +++ b/distribution/defs.bzl @@ -319,6 +319,20 @@ def by_distribution(values: dict[str, typing.Any]) -> Select: """ return select({_condition(key): value for key, value in values.items()}) +def packages(common: list[str], selected: dict[str, list[str]]) -> Select: + """Return `common` plus the packages that `selected` lists for the family and the release that a build chooses. + + `selected` is keyed like `distribution.select()`. The packages of a release key add to the packages of + its family key. A distribution whose family has no key gets the packages of the `DEFAULT` key, if + `selected` has one. + """ + + # Buck picks only the most specific matching key of one select(), so the family keys and the release + # keys go into separate selects. + families = {key: value for key, value in selected.items() if key == "DEFAULT" or key in FAMILIES} + releases = {key: value for key, value in selected.items() if key not in families} + return common + by_distribution({"DEFAULT": []} | families) + by_distribution({"DEFAULT": []} | releases) + def release_target(role: str) -> Select: """Return a `select()` of the catalog target `role`, such as `box`, of the release that a build chooses. @@ -511,6 +525,7 @@ distribution = struct( for_package = for_package, new = new, package_manager = package_manager, + packages = packages, release_name = release_name, release_target = release_target, set_for_package = set_for_package, diff --git a/docs/user/box.md b/docs/user/box.md index a5a71677..c90b293d 100644 --- a/docs/user/box.md +++ b/docs/user/box.md @@ -146,3 +146,24 @@ The box is configured for its release's distribution, so `distribution.select()` release's family, as it does in an image. A key such as `debian.testing` names one release, and Buck picks it over the key of its family. If no key matches, the build fails with an error that names the box and lists the keys. If the release's repositories do not contain a listed package, resolving the box fails. + +When most packages have the same name everywhere, `distribution.packages()` takes them as a common list. It +adds the packages of the family key and the packages of the release key, such as `debian.testing`. A +distribution whose family has no key gets no family packages: + +```Starlark +box.new( + name = "dev.box", + packages = distribution.packages( + ["git", "meson"], + { + "debian": ["python3-yaml"], + "fedora": ["python3-pyyaml"], + }, + ), + release = "catalog//:debian.testing.release", +) +``` + +A `.bzl` file can call `distribution.select()` and `distribution.packages()` too, so a project can keep its +package lists in a `.bzl` file and load them from its `BUCK` files.