Skip to content

Commit 9763046

Browse files
committed
Allow arbitrary expressions in attribute_path.
This allows more advanced attributes to be used. Motivating use-case is ghcWithPackages.
1 parent 6a06c3e commit 9763046

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

nixpkgs/nixpkgs.bzl

+12-4
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ def _nixpkgs_package_impl(ctx):
2525
else:
2626
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
2727

28-
path = '<nixpkgs>'
28+
# If neither repository or path are set, leave empty which will use
29+
# default value from NIX_PATH
30+
path = ''
2931
if ctx.attr.repository and ctx.attr.path:
3032
fail("'repository' and 'path' fields are mutually exclusive.")
3133
if ctx.attr.repository:
3234
# XXX Another hack: the repository label typically resolves to
3335
# some top-level package in the external workspace. So we use
3436
# dirname to get the actual workspace path.
35-
path = ctx.path(ctx.attr.repository).dirname
37+
path = "-I nixpkgs={0}".format(ctx.path(ctx.attr.repository).dirname)
3638
if ctx.attr.path:
37-
path = ctx.attr.path
39+
path = "-I nixpkgs={0}".format(ctx.attr.path)
3840

3941
attr_path = ctx.attr.attribute_path or ctx.name
40-
res = ctx.execute(["nix-build", path, "-A", attr_path, "--no-out-link"])
42+
# Use -E instead of -A: this way we can use build arbitrary
43+
# attributes rather than being limited to what -A accepts. An
44+
# example of this is haskellPackages.ghcWithPackages which is
45+
# unusable with -A.
46+
res = ctx.execute(["nix-build", path, "-E",
47+
"(import <nixpkgs> {{}}).{0}".format(attr_path),
48+
"--no-out-link"])
4149
if res.return_code == 0:
4250
output_path = res.stdout.splitlines()[-1]
4351
else:

0 commit comments

Comments
 (0)