Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

bzlmod support #21

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Usage of this ruleset involves three main steps:

- [rules_docker](examples/rules_docker/)
- [rules_oci](examples/rules_oci)
- [bzlmod](examples/bzlmod)

## Public API Docs

Expand Down
25 changes: 25 additions & 0 deletions debian_packages/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load(":defs.bzl", "debian_packages_repository")

_repository_attrs = {
"name": attr.string(doc = "TODO"),
"lock_file": attr.label(),
"default_distro": attr.string(),
"default_arch": attr.string(),
}

def _impl(mctx):
for mod in mctx.modules:
for repository in mod.tags.repository:
debian_packages_repository(
name = repository.name,
lock_file = repository.lock_file,
default_distro = repository.default_distro,
default_arch = repository.default_arch,
)

debian_packages = module_extension(
implementation = _impl,
tag_classes = {
"repository": tag_class(attrs = _repository_attrs),
},
)
Empty file.
21 changes: 21 additions & 0 deletions debian_packages/private/bzlmod/repository.build.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# AUTO GENERATED. DO NOT EDIT!

load("@rules_debian_packages//debian_packages/private:utils.bzl", "package_rule", "package_layer_rule")

package(default_visibility = ["//visibility:public"])

_REPOSITORY = "{REPOSITORY}"

_PACKAGES = {PACKAGES}

[
package_rule(_REPOSITORY, name, distro, arch, deps)
for name, distro, arch, deps in _PACKAGES
]

[
package_layer_rule(_REPOSITORY, name, distro, arch, deps)
for name, distro, arch, deps in _PACKAGES
]

exports_files(["packages.bzl"])
85 changes: 85 additions & 0 deletions debian_packages/private/bzlmod/repository.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
load("@rules_debian_packages//debian_packages/private:utils.bzl", "debfile_name", "package_name")

_ARCHIVE_BUILD_FILE_CONTENT = """\
load("@rules_debian_packages//debian_packages/private:utils.bzl", "debfile_layer_rule")
debfile_layer_rule()
"""

def _debian_packages_repository_impl(rctx):
print("IN BZLMOD REPO:", rctx.attr.name)
lock_file_path = rctx.path(rctx.attr.lock_file)
lock_file_content = json.decode(rctx.read(lock_file_path))
lock_file_files = lock_file_content.get("files")
lock_file_packages = lock_file_content.get("packages")

for distro, archs in lock_file_files.items():
for arch, arch_files in archs.items():
for file in arch_files:
http_file(
name = rctx.attr.name + "_" + debfile_name(file["name"], distro, arch),
urls = [file["url"]],
sha256 = file["sha256"],
downloaded_file_path = file["url"].split("/")[-1],
)
http_archive(
name = rctx.attr.name + "_" + package_name(file["name"], distro, arch),
urls = [file["url"]],
sha256 = file["sha256"],
build_file_content = _ARCHIVE_BUILD_FILE_CONTENT,
)

packages = []
for distro, archs in lock_file_packages.items():
for arch, arch_packages in archs.items():
for package in arch_packages:
packages.append(
(
package["name"],
distro,
arch,
tuple(package["dependencies"]),
),
)

print("writing BZLMOD BUILD")
rctx.template(
"BUILD.bazel",
Label("@rules_debian_packages//debian_packages/private/bzlmod:repository.build.tmpl"),
substitutions = {
"{REPOSITORY}": rctx.attr.name,
"{PACKAGES}": str(tuple(packages)),
},
)

print("writing BZLMOD packages")
rctx.template(
"packages.bzl",
Label("@rules_debian_packages//debian_packages/private/bzlmod:repository.packages.tmpl"),
substitutions = {
"{REPOSITORY}": rctx.attr.name,
"{DEFAULT_DISTRO}": rctx.attr.default_distro,
"{DEFAULT_ARCH}": rctx.attr.default_arch,
},
)

_debian_packages_repository_attrs = {
"lock_file": attr.label(
doc = "The lockfile to generate a repository from.",
allow_single_file = True,
mandatory = True,
),
"default_distro": attr.string(
doc = "The debian-distro to assume as default.",
mandatory = True,
),
"default_arch": attr.string(
doc = "The architecture to assume as default.",
mandatory = True,
),
}

debian_packages_repository = repository_rule(
implementation = _debian_packages_repository_impl,
attrs = _debian_packages_repository_attrs,
)
21 changes: 21 additions & 0 deletions debian_packages/private/bzlmod/repository.packages.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# AUTO GENERATED. DO NOT EDIT!

load("@rules_debian_packages//debian_packages/private:utils.bzl", "debfile_target", "debfile_layer_target", "package_target", "package_layer_target")

_REPOSITORY = "{REPOSITORY}"

_DEFAULT_DISTRO = "{DEFAULT_DISTRO}"

_DEFAULT_ARCH = "{DEFAULT_ARCH}"

def debfile(name, distro = _DEFAULT_DISTRO, arch = _DEFAULT_ARCH):
return debfile_target(_REPOSITORY, name, distro, arch)

def debfile_layer(name, distro = _DEFAULT_DISTRO, arch = _DEFAULT_ARCH):
return debfile_layer_target(_REPOSITORY, name, distro, arch)

def debian_package(name, distro = _DEFAULT_DISTRO, arch = _DEFAULT_ARCH):
return package_target(_REPOSITORY, name, distro, arch)

def debian_package_layer(name, distro = _DEFAULT_DISTRO, arch = _DEFAULT_ARCH):
return package_layer_target(_REPOSITORY, name, distro, arch)
8 changes: 8 additions & 0 deletions debian_packages/private/repository.workspace.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# AUTO GENERATED. DO NOT EDIT!

_REPOSITORY = "{REPOSITORY}"

workspace(name = _REPOSITORY)

load(":packages.bzl", "install_deps")
install_deps()
1 change: 1 addition & 0 deletions examples/bzlmod/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common --enable_bzlmod
1 change: 1 addition & 0 deletions examples/bzlmod/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.4.0
76 changes: 76 additions & 0 deletions examples/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@debian_packages//:packages.bzl", "debian_package_layer")
load("@rules_pkg//pkg:mappings.bzl", "pkg_mklink")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
# load("@rules_debian_packages//debian_packages:defs.bzl", "debian_packages_lockfile")

package(default_visibility = ["//visibility:public"])

pkg_mklink(
name = "usr_bin_python_symlink",
link_name = "/usr/bin/python",
target = "/bin/bin/python3.11",
)

pkg_mklink(
name = "usr_bin_python3_symlink",
link_name = "/usr/bin/python3",
target = "/bin/bin/python3.11",
)

pkg_tar(
name = "symlinks_layer",
srcs = [
":usr_bin_python3_symlink",
":usr_bin_python_symlink",
],
)

oci_image(
name = "image",
architecture = "amd64",
entrypoint = ["/usr/bin/python3.11"],
env = {"LANG": "C.UTF-8"},
os = "linux",
tars = [
# required to replicate distroless/static
debian_package_layer("base-files"),
debian_package_layer("netbase"),
debian_package_layer("tzdata"),
debian_package_layer("ca-certificates"),
# required to replicate distroless/base
debian_package_layer("libc6"),
debian_package_layer("libssl3"),
debian_package_layer("openssl"),
# required to replicate distroless/cc
debian_package_layer("libgcc-s1"),
debian_package_layer("libgomp1"),
debian_package_layer("libstdc++6"),
# required to replicate distroless/python
debian_package_layer("dash"),
debian_package_layer("libc-bin"),
debian_package_layer("python3.11-minimal"),
debian_package_layer("libpython3.11-stdlib"),
debian_package_layer("python3-distutils"),
# symlinks
":symlinks_layer",
],
visibility = ["//visibility:private"],
)

oci_tarball(
name = "image.tar",
image = ":image",
repo_tags = ["image:latest"],
)

# Generate lockfile with:
# bazel run :debian_packages.generate
# Update snapshots with:
# bazel run :debian_packages.update
# debian_packages_lockfile(
# name = "debian_packages",
# lock_file = "packages.lock",
# packages_file = "packages.yaml",
# snapshots_file = "snapshots.yaml",
# )
26 changes: 26 additions & 0 deletions examples/bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module(
name = "rules_debian_packages_bzlmod_example",
version = "0.0.0",
compatibility_level = 1,
)

bazel_dep(name = "rules_oci", version = "1.4.0")
bazel_dep(name = "rules_pkg", version = "0.9.1")
bazel_dep(name = "rules_python", version = "0.26.0")
bazel_dep(name = "rules_debian_packages", version = "0.0.0")
local_path_override(
module_name = "rules_debian_packages",
path = "../..",
)

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.11")

debian_packages = use_extension("@rules_debian_packages//debian_packages:extensions.bzl", "debian_packages")
debian_packages.repository(
name = "debian_packages",
default_arch = "amd64",
default_distro = "debian12",
lock_file = "//:packages.lock",
)
use_repo(debian_packages, "debian_packages")
9 changes: 9 additions & 0 deletions examples/bzlmod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example

This workspace provides an example of using `rules_debian_packages` with `rules_oci`.

It demonstrates how to build a container-image that is almost equivalent to `distroless/python3-debian12`.

To build the image run: `bazel build :image`

To update the debian-snapshots run: `bazel run :debian_packages.update`
2 changes: 2 additions & 0 deletions examples/bzlmod/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Empty file indicating the root of a Bazel workspace.
# Dependencies and setup are in MODULE.bazel.
Loading
Loading