Skip to content

Commit

Permalink
Preserve relative nix file paths
Browse files Browse the repository at this point in the history
Paths relative to the repository root are preserved for `nix_file` and
`nix_file_deps` attributes.

This avoids `No such file or directory` errors when a Nix file tries to
load another file by relative path in a different directory.
  • Loading branch information
aherrmann committed Oct 23, 2019
1 parent 00e9777 commit f4e2ec9
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def _nixpkgs_package_impl(repository_ctx):
if repository_ctx.attr.nix_file and repository_ctx.attr.nix_file_content:
fail("Specify one of 'nix_file' or 'nix_file_content', but not both.")
elif repository_ctx.attr.nix_file:
_cp(repository_ctx, repository_ctx.path(repository_ctx.attr.nix_file), "default.nix")
nix_file = _cp(repository_ctx, repository_ctx.attr.nix_file)
expr_args = [nix_file]
elif repository_ctx.attr.nix_file_content:
expr_args = ["-E", repository_ctx.attr.nix_file_content]
elif not repositories:
Expand All @@ -98,8 +99,7 @@ def _nixpkgs_package_impl(repository_ctx):
expr_args = ["-E", "import <nixpkgs> { config = {}; overlays = []; }"]

for dep in repository_ctx.attr.nix_file_deps:
path = repository_ctx.path(dep)
_cp(repository_ctx, path, path.basename)
_cp(repository_ctx, dep)

expr_args.extend([
"-A",
Expand Down Expand Up @@ -348,5 +348,25 @@ def _executable_path(repository_ctx, exe_name, extra_msg = ""):
.format(exe_name, " " + extra_msg if extra_msg else ""))
return path

def _cp(repository_ctx, src, dest):
def _cp(repository_ctx, src, dest = None):
"""Copy the given file into the external repository root.
Args:
repository_ctx: The repository context of the current repository rule.
src: The source file. Must be a Label if dest is None.
dest: Optional, The target path within the current repository root.
By default the relative path to the repository root is preserved.
Returns:
The absolute target path.
"""
if dest == None:
if type(src) != "Label":
fail("src must be a Label if dest is not specified explicitly.")
dest = "/".join([
component
for component in [src.workspace_root, src.package, src.name]
if component
])
repository_ctx.template(dest, src, executable = False)
return repository_ctx.path(dest)

0 comments on commit f4e2ec9

Please sign in to comment.