From 1451c7adf7dabf0af488f99bbb2b23680241008f Mon Sep 17 00:00:00 2001 From: Philip Patsch Date: Wed, 29 Aug 2018 23:36:23 +0200 Subject: [PATCH] Factor out & improve symlinking store paths. 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. --- nixpkgs/nixpkgs.bzl | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 9736a1632..8208e3f35 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -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, @@ -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)