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

extract bzlmod modules for core rules and toolchains #193

Merged
merged 25 commits into from
Mar 15, 2022
Merged

Conversation

fricklerhandwerk
Copy link
Member

@fricklerhandwerk fricklerhandwerk commented Mar 7, 2022

closes #182

This is a fairly large change in terms of LOC, and also has some noise due to a generated README.md being checked into version control. It's best to follow it commit by commit, as they are fairly self-contained.

The main line of thought is to literally separate rules by concern, as described in #182, and keep the tests working (which accounts for most of the non-obvious changes).

@fricklerhandwerk fricklerhandwerk force-pushed the issue-182 branch 8 times, most recently from dd5bb89 to f5861e3 Compare March 9, 2022 11:41
this is in preparation to support `bzlmod`
otherwise the entire file will be consumed as the next line, skipping
everything after the last match.
tl;dr: end users of `rules_nixpkgs` should not be affected by this
change at all. the additional code is temporary and purely for
migration reasons.

migrating the go toolchain to `bzlmod` necessitates accessing
`rules_nixpkgs_core` as an external repository (later module). therefore
we have to incorporate that new repository into the `WORKSPACE` setup.

to maintain compatibility with existing installations,
`rules_nixpkgs_dependencies()` automatically fetches the new
sub-repository from a remote archive. but local testing requires taking
it from the working directory! therefore we temporary introduce
a `local=` parameter which lets the caller point optionally to
a directory for local sub-repositories. the option will be removed when
migration to `bzlmod` is complete.

it must be configurable,  because the call to `local_repository()` takes
its path starting from the workspace directory where
`rules_nixpkgs_dependencies()` is called:
  - `bazel test //...` needs `local="."`
  - `cd examples/toolchains/{go, java, ...}; bazel run :hello` needs
  `local="../../.."`
no alias for `default_java_toolchain`, since that seems to be used only
internally, and a copy of what is usually distributed with Bazel.
leave aliases to existing files to ensure backwards compatibility.  put
CC toolchain in separate workspace, which is required because
`nixpkgs_cc_configure()` references a file `cc.nix` from its own
repository, which will be external at the call site.
@fricklerhandwerk fricklerhandwerk force-pushed the issue-182 branch 3 times, most recently from 90ccdc5 to 8dad4fd Compare March 9, 2022 12:21
otherwise internal references will break
necessary to facilitate sandboxed integration testing, where the original
source directory of the main workspace is not accessible without more
jumping through hoops.
requires exposing `:srcs` for all the toolchain modules, so they can be
added as `runfiles` to the sandbox.
@fricklerhandwerk fricklerhandwerk marked this pull request as ready for review March 10, 2022 10:27
Copy link
Member

@aherrmann aherrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thank you!
A few comments, though this already looks very good.

To be sure that the backwards compatibility aspect works we should also test this on some users of rules_nixpkgs. rules_haskell and daml come to mind.

docs/BUILD.bazel Show resolved Hide resolved
docs/update-readme.sh Outdated Show resolved Hide resolved
nixpkgs/toolchains/rust.bzl Outdated Show resolved Hide resolved
nixpkgs/repositories.bzl Outdated Show resolved Hide resolved
toolchains/rust/MODULE.bazel Outdated Show resolved Hide resolved
docs/BUILD.bazel Show resolved Hide resolved
nixpkgs/BUILD.bazel Show resolved Hide resolved
the boilerplate for accessing runfiles turned out to be not needed.

from now on one can just add targets or filenames for generating
documentation and checking that the generated files are up to date under
version control.
while incomplete, this should cover most existing use cases seamlessly,
such that existing users would not notice the refactoring.
this requires some more cleanup and refactoring before it is readable
again, see TODOs.
@fricklerhandwerk fricklerhandwerk changed the title extract bzlmod module for core rules extract bzlmod modules for core rules and toolchains Mar 15, 2022
Copy link
Member

@aherrmann aherrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Good working teasing those apart!

A few last minor comments, and avdv/daml@04dff55#r68737946. Feel free to merge once they're resolved.

This is one of those PRs, where git's colorMoved = default and colorMovedWS = allow-indentation-change is really useful.

Comment on lines +90 to +93
# sorry for this mess. Bazel `str.format()` does not allow nested `{}`
# pairs and `awk` is sensitive to line breaks.
# TODO: consider alternative: leave reference for the end and just
# append the generated string with `cat`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one can escape { as {{ and then it can be nested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Does that make anything substantially better? I'll try...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work, not even in Python 3.8 (ValueError: unexpected '{' in field name). This is a pathological case. Just look at the amount of additional braces! It is probably better to have a simpler setup in the first place, as noted in the comment.

examples/toolchains/rust/WORKSPACE Show resolved Hide resolved
@mergify mergify bot merged commit b6b6e42 into master Mar 15, 2022
@mergify mergify bot deleted the issue-182 branch March 15, 2022 16:39
@aherrmann
Copy link
Member

Uhm, that was a bit faster than intended, sorry about that! It looks like we lost the merge-queue label requirement in the Mergify config. I've opened #199 to fix that.
@fricklerhandwerk In that case, could you address the last minor comments in a follow-up?

@avdv
Copy link
Member

avdv commented Mar 16, 2022

To be sure that the backwards compatibility aspect works we should also test this on some users of rules_nixpkgs. rules_haskell and daml come to mind.

FTR, here are the changes that were necessary in the daml project to use rules_nixpkgs after extracting the modules:

digital-asset/daml@main...avdv:rules_nixpkgs-pr-193

(due to not using rules_nixpkgs_dependencies() the logic from nixpkgs/repositories.bzl had to be duplicated and also the patch did not apply cleanly anymore)

@fricklerhandwerk
Copy link
Member Author

@avdv Which patch do you mean?

@avdv
Copy link
Member

avdv commented Mar 16, 2022

@avdv Which patch do you mean?

In daml they are applying a patch to rules_nixpkgs to avoid "occasional segmentation faults of nix":
https://github.com/digital-asset/daml/blob/e0965709fe62b5c86f980ef25472a5eeb59f3adb/deps.bzl#L51-L59

this one:
https://github.com/digital-asset/daml/blob/main/bazel_tools/nixpkgs-disable-http2.patch

I did not care too much about it, as I did not see a segfault and the patch could be adapted if needed.

@aherrmann
Copy link
Member

@avdv Thanks for testing and sharing the diff!

due to not using rules_nixpkgs_dependencies() the logic from nixpkgs/repositories.bzl had to be duplicated

Raised a question here:

Was it necessary to duplicate this here, or could this have been done by calling rules_nixpkgs_dependencies in WORKSPACE as well?

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

Successfully merging this pull request may close these issues.

Split rules_nixpkgs into separate components
3 participants