Skip to content

Commit

Permalink
Invalid nixpkgs_package on nixpkgs_local_repository change
Browse files Browse the repository at this point in the history
Nix files of a nixpkgs_local_repository are not tracked by the
nixpkgs_package rule. In this commit, we generate the list of these
files in the nixpkgs_local_repository and make the nixpkgs_package
rule reading this list to add all of these files in its own repository
to let Bazel track them.

The list of files is propagated from a rule to the other one throught
a file named `nix-file-deps` created in the
`nixpkgs_local_repository` repository.

Closes #113
  • Loading branch information
nlewo committed Apr 20, 2020
1 parent 07668ff commit d79f224
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ nixpkgs_git_repository = repository_rule(
)

def _nixpkgs_local_repository_impl(repository_ctx):
repository_ctx.file("BUILD")
if not bool(repository_ctx.attr.nix_file) != \
bool(repository_ctx.attr.nix_file_content):
fail("Specify one of 'nix_file' or 'nix_file_content' (but not both).")
Expand All @@ -36,12 +35,30 @@ def _nixpkgs_local_repository_impl(repository_ctx):
content = repository_ctx.attr.nix_file_content,
executable = False,
)
target = repository_ctx.path("default.nix")
target_filename = "default.nix"
target = repository_ctx.path(target_filename)
else:
target = _cp(repository_ctx, repository_ctx.attr.nix_file)
target_filename = repository_ctx.attr.nix_file
target = _cp(repository_ctx, target_filename)

print(repository_ctx.attr.nix_file_deps)
dep_files = []
for dep in repository_ctx.attr.nix_file_deps:
_cp(repository_ctx, dep)
dest = _cp(repository_ctx, dep)
dep_files.append(dest)

# Export all specified Nix files to make them dependencies of a
# nixpkgs_package rule.
export_files = 'exports_files(["{}", {}])'.format(
target_filename.name,
",".join(['"{}"'.format(p) for p in dep_files]))
repository_ctx.file("BUILD", content = export_files)

# Create a file listing all Nix files of this repository. This
# file is used by the nixpgks_package rule to register all Nix
# files.
repository_files = [target_filename.name] + dep_files
repository_ctx.file("nix-file-deps", content = "\n".join(repository_files))

# Make "@nixpkgs" (syntactic sugar for "@nixpkgs//:nixpkgs") a valid
# label for the target Nix file.
Expand Down Expand Up @@ -124,6 +141,15 @@ def _nixpkgs_package_impl(repository_ctx):

expr_args.extend(repository_ctx.attr.nixopts)

for (target, path_name) in repositories.items():
path = str(repository_ctx.path(target).dirname) + "/nix-file-deps"
if repository_ctx.path(path).exists:
content = repository_ctx.read(path)
for f in content.splitlines():
# Hack: this is to register all Nix files as dependencies
# of this rule (see issue #113)
repository_ctx.path(Label("@{}//:{}".format(path_name, f)))

# If repositories is not set, leave empty so nix will fail
# unless a pinned nixpkgs is set in the `nix_file` attribute.
nix_paths = []
Expand Down

0 comments on commit d79f224

Please sign in to comment.