Skip to content

Commit 25f8361

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 a300f57 commit 25f8361

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

CONTRIBUTORS

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
# Name <email address>
1111

1212
Mathieu Boespflug <[email protected]>
13+
Mateusz Kowalczyk <[email protected]>

WORKSPACE

+19
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,22 @@ 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+
)
25+
26+
nixpkgs_package(
27+
name = "expr-attribute-test",
28+
expression = "import <nixpkgs> {}",
29+
attribute = "hello",
30+
repository = "@nixpkgs",
31+
)

nixpkgs/nixpkgs.bzl

+27-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ 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 user specified expression only, use expression only: they may
22+
# be picking their attributes in the expression itself already.
23+
if ctx.attr.expression and not ctx.attr.attribute:
24+
return ["-E", ctx.attr.expression]
25+
# In all other cases we can craft a correct query by using user's
26+
# input with some defaults.
27+
else:
28+
return ["-E", ctx.attr.expression or "import <nixpkgs> {}",
29+
"-A", ctx.attr.attribute or ctx.attr.name]
30+
1831
def _nixpkgs_package_impl(ctx):
1932
if ctx.attr.build_file and ctx.attr.build_file_content:
2033
fail("Specify one of 'build_file' or 'build_file_content', but not both.")
@@ -25,19 +38,22 @@ def _nixpkgs_package_impl(ctx):
2538
else:
2639
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
2740

28-
path = '<nixpkgs>'
41+
# If neither repository or path are set, leave empty which will use
42+
# default value from NIX_PATH
43+
path = []
2944
if ctx.attr.repository and ctx.attr.path:
3045
fail("'repository' and 'path' fields are mutually exclusive.")
3146
if ctx.attr.repository:
3247
# XXX Another hack: the repository label typically resolves to
3348
# some top-level package in the external workspace. So we use
3449
# dirname to get the actual workspace path.
35-
path = ctx.path(ctx.attr.repository).dirname
50+
path = ["-I", "nixpkgs={0}".format(ctx.path(ctx.attr.repository).dirname)]
3651
if ctx.attr.path:
37-
path = ctx.attr.path
52+
path = ["-I", "nixpkgs={0}".format(ctx.attr.path)]
53+
54+
buildExpr = _mk_build_expression(ctx)
55+
buildCmd = ["nix-build"] + path + ["--no-out-link"] + buildExpr
3856

39-
attr_path = ctx.attr.attribute_path or ctx.name
40-
buildCmd = ["nix-build", path, "-A", attr_path, "--no-out-link"]
4157
res = ctx.execute(buildCmd, quiet = False)
4258
if res.return_code == 0:
4359
output_path = res.stdout.splitlines()[-1]
@@ -48,7 +64,12 @@ def _nixpkgs_package_impl(ctx):
4864
nixpkgs_package = repository_rule(
4965
implementation = _nixpkgs_package_impl,
5066
attrs = {
51-
"attribute_path": attr.string(),
67+
"attribute": attr.string(
68+
doc="Nix attribute to build. Exclusive to expression."
69+
),
70+
"expression": attr.string(
71+
doc="Nix expression to build. Rule name used as attribute if not present.",
72+
),
5273
"path": attr.string(),
5374
"repository": attr.label(),
5475
"build_file": attr.label(),

tests/BUILD

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
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+
"expr-attribute-test",
14+
]]

0 commit comments

Comments
 (0)