Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: SYSROOT generated by rules_distroless for hermetic CC complation #124

Open
thesayyn opened this issue Dec 4, 2024 · 7 comments
Open

Comments

@thesayyn
Copy link
Collaborator

thesayyn commented Dec 4, 2024

While working on a GLIBC related problem, i realized that rules_distroless could have made hermetic cc compilation so much easier by generating a sysroot out of the debian packages that are being fetched.

This is great for one single reason, what's actually going into the container is also what you are linking against!

I can imagine a bzlmod only API, as such

# Generate a sysroot using the apt extensions
apt = use_extension("@rules_distroless//apt:extensions.bzl", "apt")

apt.install("libc6-dev")

apt.sysroot(
    name = "sysroot_amd64"
    arch = "amd64"
)
 
apt.sysroot(
    name = "sysroot_arm64"
    arch = "arm64"
)
 
use_repo(apt, "sysroot_amd64", "sysroot_arm64")
 
# Use with toolchains_llvm
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.sysroot(
    name = "llvm_toolchain_with_sysroot",
    targets = ["linux-x86_64"],
    label = "@sysroot_amd64//:sysroot",
)
llvm.sysroot(
    name = "llvm_toolchain_with_sysroot",
    targets = ["linux-arm64"],
    label = "@sysroot_arm64//:sysroot",
)
@JonathanPerry651
Copy link
Contributor

This is a very cool idea

@jjmaestro
Copy link
Contributor

This is awesome!! I was wondering about something like this as well! TIL about bazel-contrib/toolchains_llvm's sysroot!

Having something like this API would be amazing, I had to hack something together for a project I'm working on. Basically a custom Docker image from where I then create toolchains using bazelbuild/bazel-toolchains and I was going to eventually try something like this but much less nice, just create the image with rules_distroless + rules_oci and still have to call rbe_configs_gen.

This is neat and I'd love to have it! Hopefully other Cxx toolchains and other rules support something like this sysroot... and maybe Bazel itself would support it natively, it would be great to be able to create a sysroot(s) for the full linux hermetic dashboard. Currently it's very hard to setup, requires manual fiddling, etc. Being able to produce a sysroot and tell Bazel to use it for sandboxing would be 🔥

@JonathanPerry651
Copy link
Contributor

So just thinking about this one a bit more, and with the caveat that I’m way out of my depth - could we figure out a way to make bazel invoke actions under a root thats completely controlled by rules_distroless? This would be (very close to) the holy grail for making sure you fully understand your environment, and for making sure that your builds tests and production runtime are deeply aligned…

@JonathanPerry651
Copy link
Contributor

Context here is that this repo has been exceptionally helpful for us uplifting our containers across the board (thank you!), but we still have the horrible situation where test environments (ie remote build base containers) contain, say, grep, but our production runtime doesn’t - and grep is required by bazel’s java_binary wrapper script. Sadness ensues.

@lukasoyen
Copy link

I was inspired by the idea of this FR and started hacking in https://github.com/lukasoyen/bazel_debian_packages/tree/apt-sysroot.

But then I realized this might contradict the stated goal of the repository, so I decided to fork rules_distroless and strip it down to only the functionality to create sysroots.

If you see it within scope of rules_distroless I am happy to collaborate on integrating the idea.

@thesayyn
Copy link
Collaborator Author

Sure, please make a PR so we can discuss there.

@apsaltis-ddog
Copy link

👋 I'd love to see this too -- I independently came to a similar conclusion as y'all last week, and would be happy to collaborate on this.

What I have so far is a bunch of targets for assembling the sysroots externally of where they would actually be used; more befitting of an standalone example, or an example of how code might be generated more than anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants