Skip to content

Commit

Permalink
Merge pull request tweag#91 from tweag/relative-imports
Browse files Browse the repository at this point in the history
Fix relative imports across directories in nixpkgs_package
  • Loading branch information
mboes authored Oct 23, 2019
2 parents 00e9777 + fd03d41 commit 546aa58
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
11 changes: 11 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ filegroup(
repository = "@nixpkgs",
)

nixpkgs_package(
name = "relative-imports",
attribute_path = "hello",
nix_file = "//tests:relative_imports.nix",
nix_file_deps = [
"//:nixpkgs.nix",
"//tests:relative_imports/nixpkgs.nix",
],
repository = "@nixpkgs",
)

nixpkgs_cc_configure(repository = "@remote_nixpkgs")
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)
1 change: 1 addition & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package(default_testonly = 1)
"nix-file-test",
"nix-file-deps-test",
"nixpkgs-git-repository-test",
"relative-imports",
]
] + [
# These tests use the nix package generated by ./output.nix
Expand Down
4 changes: 4 additions & 0 deletions tests/relative_imports.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ system ? builtins.currentSystem
, pkgs ? import relative_imports/nixpkgs.nix { inherit system; config = {}; overlays = []; }
}:
{ inherit (pkgs) hello; }
1 change: 1 addition & 0 deletions tests/relative_imports/nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ../../nixpkgs.nix

0 comments on commit 546aa58

Please sign in to comment.