Skip to content

Commit

Permalink
chore: Add invert_repositories function
Browse files Browse the repository at this point in the history
Generic function for working around
bazelbuild/bazel#5356
  • Loading branch information
thufschmitt committed Jan 14, 2019
1 parent 6747660 commit 570f1d1
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ nixpkgs_local_repository = repository_rule(
)

def _nixpkgs_package_impl(repository_ctx):
repository = repository_ctx.attr.repository
repositories = repository_ctx.attr.repositories

if repository and repositories or not repository and not repositories:
fail("Specify one of 'repository' or 'repositories' (but not both).")
elif repository:
repositories = {repository_ctx.attr.repository: "nixpkgs"}

if repository_ctx.attr.build_file and repository_ctx.attr.build_file_content:
fail("Specify one of 'build_file' or 'build_file_content', but not both.")
elif repository_ctx.attr.build_file:
Expand Down Expand Up @@ -167,21 +161,31 @@ _nixpkgs_package = repository_rule(
)

def nixpkgs_package(*args, **kwargs):
invert_repositories(_nixpkgs_package, *args, **kwargs)

def invert_dict(dict):
"""Swap the keys and values of a dict − assuming that all values are
distinct
"""
return {value: key for (key, value) in dict.items()}

def invert_repositories(f, *args, **kwargs):
# Because of https://github.com/bazelbuild/bazel/issues/5356 we can't
# directly pass a dict from strings to labels to the rule (which we'd like
# for the `repositories` arguments), but we can pass a dict from labels to
# strings. So we swap the keys and the values (assuming they all are
# distinct).
if "repositories" in kwargs:
inversed_repositories = {value: key for (key, value) in kwargs["repositories"].items()}
kwargs.pop("repositories")
_nixpkgs_package(
repositories = inversed_repositories,
*args,
**kwargs
)
else:
_nixpkgs_package(*args, **kwargs)
hasRepository = "repository" in kwargs and kwargs["repository"] != None
hasRepositories = "repositories" in kwargs and kwargs["repositories"] != None
if hasRepository and hasRepositories:
fail("Specify one of 'repository' or 'repositories' (but not both).")
if hasRepositories:
inversed_repositories = invert_dict(kwargs["repositories"])
kwargs["repositories"] = inversed_repositories
if hasRepository:
repository = kwargs.pop("repository")
kwargs["repositories"] = { repository: "nixpkgs" }
f(*args, **kwargs)

def nixpkgs_cc_autoconf_impl(repository_ctx):
cpu_value = get_cpu_value(repository_ctx)
Expand Down Expand Up @@ -252,7 +256,7 @@ nixpkgs_cc_autoconf = repository_rule(

def nixpkgs_cc_configure(
repository = None,
repositories = {},
repositories = None,
nix_file = None,
nix_file_deps = None,
nix_file_content = None):
Expand Down

0 comments on commit 570f1d1

Please sign in to comment.