Skip to content

Commit

Permalink
Factor out discovery of executables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Profpatsch committed Aug 29, 2018
1 parent 8cb9314 commit c1acbbd
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def _nixpkgs_package_impl(ctx):
elif not (ctx.attr.nix_file or ctx.attr.nix_file_content):
fail(strFailureImplicitNixpkgs)

nix_build_path = ctx.which("nix-build")
if nix_build_path == None:
fail("Could not find nix-build on the path. Please install it. See: https://nixos.org/nix/")

nix_build_path = _executable_path(
"nix-build", ctx,
extra_msg = "See: https://nixos.org/nix/"
)
nix_build = [nix_build_path] + expr_args

# Large enough integer that Bazel can still parse. We don't have
Expand All @@ -99,11 +99,9 @@ def _nixpkgs_package_impl(ctx):
# Build a forest of symlinks (like new_local_package() does) to the
# Nix store.

find_path = ctx.which("find")
if find_path == None:
fail("Could not find the 'find' command. Please ensure it is in your PATH.")
find_path = _executable_path("find", ctx)

res = ctx.execute(["find", output_path, "-maxdepth", "1"])
res = ctx.execute([find_path, output_path, "-maxdepth", "1"])
if res.return_code == 0:
for i in res.stdout.splitlines():
basename = i.rpartition("/")[-1]
Expand All @@ -125,3 +123,12 @@ nixpkgs_package = repository_rule(
},
local = True,
)


def _executable_path(exe_name, rep_ctx, extra_msg=""):
"""Try to find the executable, fail with an error."""
path = rep_ctx.which(exe_name)
if path == None:
fail("Could not find the `{}` executable in PATH.{}\n"
.format(exe_name, " " + extra_msg if extra_msg else ""))
return path

0 comments on commit c1acbbd

Please sign in to comment.