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

Simplify rules_nixpkgs_core module extensions #423

Merged
merged 36 commits into from
Oct 16, 2023
Merged

Conversation

aherrmann
Copy link
Member

@aherrmann aherrmann commented Sep 29, 2023

Part of #183.

The previous implementation of the rules_nixpkgs_core module extensions (#344, #363, #369, #364) used a hub-repository to manage Nix repository and package import scope.

Since then, Bazel (>= 6.3) has introduced isolated module extension instances and automatic generation of use_repo stanzas. Both these features solve the same problems that the hub-repository implementation addressed, see below.

This PR replaces the previous hub-repository based implementation by a simpler implementation based on these new Bazel features. A clear advantage is that this means that users can use plain Bazel labels to refer to imported repositories and packages instead of having to access them through hub-repository accessors. This should also be an advantage in terms of WORKSPACE to bzlmod migration effort.

How isolated extensions and automatically generated use_repo address rules_nixpkgs's requirements:

  • Separating globally unified and locally specialized packages. New: Globally unified behavior is the default, and locally specialized behavior is achieved using an isoalted module extension instance.
  • Exposing imports under human friendly names. New: Because isolated module extension instances have their own namespace, we no longer need to worry about name mangling ourselves. Instead, we can generate external repositories with human friendly names directly, either the canonical attribute name in case of globally unified packages, or a human defined name in case of isolated instances or overrides in root or rules_nixpkgs_core. Thanks to the automatically generated use_repo stanzas, it is very easy for a user to manage the imports.

This uses the new isolated module extensions feature [1], instead of a
hub-repository approach [2] [3], to distinguish globally unified
repositories and locally specialized repositories.

[1]: bazelbuild/bazel#18529
[2]: bazelbuild/bazel#17048
[3]: bazelbuild/bazel#17493
Otherwise the tests only cover the cases of rules_nixpkgs_core or the
root module.
Except those that require further simple repository tags to be
implemented first.
After removing the old extensions we can now use the shorter names
"repository" and "package".
This is so that modules can define overrides as dev-depencies.
This is to test logic that differs between an intermediate module or the
root module or rules_nixpkgs_core.
@aherrmann aherrmann requested a review from benradf as a code owner September 29, 2023 12:23
@aherrmann
Copy link
Member Author

The CI failure seems to be caused by NixOS/nixpkgs#258607. I've opened NixOS/nixpkgs#258608 with a fix. And pushed a workaround until the fix is available in nixpkgs.

The corresponding files no longer exist and the import was no longer in
effect since the @nixpkgs import in docs_dependencies_2 took precedence.
To prevent the following build error on MacOS x86_64
```
> error: don't yet have a `targetPackages.darwin.LibsystemCross for x86_64-apple-darwin`

ERROR: /Users/runner/work/rules_nixpkgs/rules_nixpkgs/WORKSPACE:45:16: fetching _nixpkgs_package rule //external:coreutils_static: Traceback (most recent call last):
  File "/private/var/tmp/_bazel_runner/a4a3182e50c9460fa52b8c6c1e6f916f/external/rules_nixpkgs_core/nixpkgs.bzl", line 589, column 31, in _nixpkgs_package_impl
    _nixpkgs_build_and_symlink(repository_ctx, [nix_build_path], expr_args, build_file_content)
  File "/private/var/tmp/_bazel_runner/a4a3182e50c9460fa52b8c6c1e6f916f/external/rules_nixpkgs_core/nixpkgs.bzl", line 447, column 34, in _nixpkgs_build_and_symlink
    exec_result = execute_or_fail(
  File "/private/var/tmp/_bazel_runner/a4a3182e50c9460fa52b8c6c1e6f916f/external/rules_nixpkgs_core/util.bzl", line 97, column 13, in execute_or_fail
    fail("""
Error in fail:
  Cannot build Nix derivation for package '@coreutils_static'.
    Command: "/nix/store/x5wfa3afc4l2rc7zx3wiiwcm5j3j4lbc-nix-2.17.0/bin/nix-build" "-I" "nixpkgs=/private/var/tmp/_bazel_runner/a4a3182e50c9460fa52b8c6c1e6f916f/external/nixpkgs/nixpkgs" "-E" "import <nixpkgs> { config = {}; overlays = []; }" "-A" "pkgsStatic.coreutils" "--out-link" "bazel-support/nix-out-link"
```
https://github.com/tweag/rules_nixpkgs/actions/runs/6390957120/job/17345262106?pr=423#step:6:846

Error message origin in nixpkgs:
https://github.com/aherrmann/nixpkgs/blob/59998678277cf465e749a144cf707021285bdf82/pkgs/top-level/all-packages.nix#L21799-L21802
@aherrmann
Copy link
Member Author

The second CI failure seems to be caused by an incompatibility with more recent nixpkgs revisions and MacOS 11 on x86_64. I have not been able to track down the exact reason, but worked around the issue by falling back to pkgs.coreutils. I've opened #424 to track this.

@aherrmann
Copy link
Member Author

CI is green now, so this is ready for review.

Copy link
Collaborator

@layus layus left a comment

Choose a reason for hiding this comment

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

Interesting changeset. Overall not much changed in the interface, except for some simplifications, so this is a much welcome change.

I would love to see the implications for more complex use cases of rules_nixpkgs, where @nixpkgs is not a vanilla upstream nixpkgs, but I think there is kind of a global assumption that @nixpkgs is some version of upstream nixpkgs.

I am still annoyed that bzlmod (at least for rules_nixpkgs) relies on so many conventions and unchecked assumptions, but there is not much we can do about that.

Some comments I left are merely notes for me to check after the review, these are marked with XXX.

WORKSPACE Outdated Show resolved Hide resolved
WORKSPACE Show resolved Hide resolved
core/MODULE.bazel Show resolved Hide resolved
@@ -1,6 +1,6 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any specific reason for this change ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, to get access to Bazel version 6.3.2, which is not yet available in NixOS-22.11.

testing/core/tests/nixpkgs_repositories.bzl Show resolved Hide resolved
@@ -8,7 +8,7 @@ sh_test(
"//nixpkgs:srcs",
"//tests/invalid_nixpkgs_package:srcs",
"@coreutils_static//:bin",
"@nix_2_7//:bin",
"@nix_2_10//:bin",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for thins change, except general maintenance ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, version 2.7 (and 2.8 and 2.9) are no longer available in the more recent nixpkgs revision (unstable). So we need to bump the version.

Co-authored-by: Guillaume Maudoux <[email protected]>
@aherrmann aherrmann added the merge-queue merge on green CI label Oct 16, 2023
@mergify mergify bot merged commit 0fd4a8f into master Oct 16, 2023
15 checks passed
@mergify mergify bot deleted the bzlmod-isolate branch October 16, 2023 12:58
@mergify mergify bot removed the merge-queue merge on green CI label Oct 16, 2023
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.

2 participants