Skip to content

Commit e908785

Browse files
committed
Allow arbitrary expressions in nixpkgs_package
This allows us to be much more flexible when we want to be while maintaining convenience for simple attribute cases.
1 parent acfc866 commit e908785

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

WORKSPACE

+12
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ nixpkgs_git_repository(
1010
)
1111

1212
nixpkgs_package(name = "hello", repository = "@nixpkgs")
13+
14+
nixpkgs_package(
15+
name = "expr-test",
16+
expression = "let pkgs = import <nixpkgs> {}; in pkgs.hello",
17+
repository = "@nixpkgs"
18+
)
19+
20+
nixpkgs_package(
21+
name = "attribute-test",
22+
attribute = "hello",
23+
repository = "@nixpkgs"
24+
)

nixpkgs/nixpkgs.bzl

+24-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ nixpkgs_git_repository = repository_rule(
1515
local = False,
1616
)
1717

18+
def _mk_build_expression(ctx):
19+
"""Generate a nix expression that picks a package from nixpkgs.
20+
"""
21+
if ctx.attr.attribute and ctx.attr.expression:
22+
fail("'attribute' and 'expression' are mutually exclusive.")
23+
elif ctx.attr.expression:
24+
return ["-E", ctx.attr.expression]
25+
else:
26+
return ["-E", "(import <nixpkgs> {{}}).{0}".format(ctx.attr.attribute or ctx.attr.name)]
27+
1828
def _nixpkgs_package_impl(ctx):
1929
if ctx.attr.build_file and ctx.attr.build_file_content:
2030
fail("Specify one of 'build_file' or 'build_file_content', but not both.")
@@ -25,19 +35,22 @@ def _nixpkgs_package_impl(ctx):
2535
else:
2636
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
2737

28-
path = '<nixpkgs>'
38+
# If neither repository or path are set, leave empty which will use
39+
# default value from NIX_PATH
40+
path = ''
2941
if ctx.attr.repository and ctx.attr.path:
3042
fail("'repository' and 'path' fields are mutually exclusive.")
3143
if ctx.attr.repository:
3244
# XXX Another hack: the repository label typically resolves to
3345
# some top-level package in the external workspace. So we use
3446
# dirname to get the actual workspace path.
35-
path = ctx.path(ctx.attr.repository).dirname
47+
path = "-I nixpkgs={0}".format(ctx.path(ctx.attr.repository).dirname)
3648
if ctx.attr.path:
37-
path = ctx.attr.path
49+
path = "-I nixpkgs={0}".format(ctx.attr.path)
50+
51+
buildExpr = _mk_build_expression(ctx)
52+
buildCmd = ["nix-build", path, "--no-out-link"] + buildExpr
3853

39-
attr_path = ctx.attr.attribute_path or ctx.name
40-
buildCmd = ["nix-build", path, "-A", attr_path, "--no-out-link"]
4154
res = ctx.execute(buildCmd, quiet = False)
4255
if res.return_code == 0:
4356
output_path = res.stdout.splitlines()[-1]
@@ -48,7 +61,12 @@ def _nixpkgs_package_impl(ctx):
4861
nixpkgs_package = repository_rule(
4962
implementation = _nixpkgs_package_impl,
5063
attrs = {
51-
"attribute_path": attr.string(),
64+
"attribute": attr.string(
65+
doc="Nix attribute to build. Exclusive to expression."
66+
),
67+
"expression": attr.string(
68+
doc="Nix expression to build. Rule name used as attribute if not present.",
69+
),
5270
"path": attr.string(),
5371
"repository": attr.label(),
5472
"build_file": attr.string(),

tests/BUILD

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package(default_testonly = 1)
22

3-
sh_test(
4-
name = "run-hello",
3+
[sh_test(
4+
name= "run-{0}".format(test),
55
srcs = ["test_bin.sh"],
6-
args = ["$(location @hello//:bin)"],
7-
data = ["@hello//:bin"],
6+
args=["$(location @{0}//:bin)".format(test)],
7+
data = ["@{0}//:bin".format(test)],
88
timeout = "short",
9-
)
9+
) for test in [
10+
"hello",
11+
"expr-test",
12+
"attribute-test",
13+
]]

0 commit comments

Comments
 (0)