Skip to content

Commit

Permalink
Factor out & improve symlinking store paths.
Browse files Browse the repository at this point in the history
Adds `-mindepth 1` to find, because otherwise the parent directory
would always be symlinked as well.
`-print0` is used to be stable in the face of filenames that could
possibly contain the `\n` character.
  • Loading branch information
Profpatsch committed Aug 29, 2018
1 parent 967db99 commit 1451c7a
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,8 @@ def _nixpkgs_package_impl(ctx):

# Build a forest of symlinks (like new_local_package() does) to the
# Nix store.
_symlink_children(output_path, ctx)

find_path = _executable_path("find", ctx)

res = ctx.execute([find_path, output_path, "-maxdepth", "1"])
if res.return_code == 0:
for i in res.stdout.splitlines():
basename = i.rpartition("/")[-1]
ctx.symlink(i, ctx.path(basename))
else:
_execute_error(res, "find failed on {}".format(output_path))

nixpkgs_package = repository_rule(
implementation = _nixpkgs_package_impl,
Expand All @@ -124,6 +116,27 @@ nixpkgs_package = repository_rule(
)


def _symlink_children(target_dir, rep_ctx):
"""Create a symlink to all children of `target_dir` in the current
build directory."""
find_args = [
_executable_path("find", rep_ctx),
target_dir,
"-maxdepth", "1",
# otherwise the directory is printed as well
"-mindepth", "1",
# filenames can contain \n
"-print0",
]
find_res = rep_ctx.execute(find_args)
if find_res.return_code == 0:
for target in find_res.stdout.rstrip("\0").split("\0"):
basename = target.rpartition("/")[-1]
rep_ctx.symlink(target, basename)
else:
_execute_error(find_res)


def _executable_path(exe_name, rep_ctx, extra_msg=""):
"""Try to find the executable, fail with an error."""
path = rep_ctx.which(exe_name)
Expand Down

0 comments on commit 1451c7a

Please sign in to comment.