Skip to content

Commit 86eb290

Browse files
authored
Merge pull request #4 from tweag/allow-expressions
Allow arbitrary expressions in attribute_path.
2 parents a300f57 + 5e34b6e commit 86eb290

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-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_path = "hello",
23+
repository = "@nixpkgs"
24+
)
25+
26+
nixpkgs_package(
27+
name = "expr-attribute-test",
28+
expression = "import <nixpkgs> {}",
29+
attribute_path = "hello",
30+
repository = "@nixpkgs",
31+
)

nixpkgs/nixpkgs.bzl

+19-6
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)]
40+
41+
extraArgs = [
42+
"-E", ctx.attr.expression or "import <nixpkgs> {}",
43+
"-A", ctx.attr.attribute_path
44+
if ctx.attr.expression
45+
else ctx.attr.attribute_path or ctx.attr.name,
46+
]
47+
buildCmd = ["nix-build"] + path + ["--no-out-link"] + extraArgs
3848

39-
attr_path = ctx.attr.attribute_path or ctx.name
40-
buildCmd = ["nix-build", path, "-A", attr_path, "--no-out-link"]
4149
res = ctx.execute(buildCmd, quiet = False)
4250
if res.return_code == 0:
4351
output_path = res.stdout.splitlines()[-1]
@@ -48,7 +56,12 @@ def _nixpkgs_package_impl(ctx):
4856
nixpkgs_package = repository_rule(
4957
implementation = _nixpkgs_package_impl,
5058
attrs = {
51-
"attribute_path": attr.string(),
59+
"attribute_path": attr.string(
60+
doc="Nix attribute to build. Exclusive to expression."
61+
),
62+
"expression": attr.string(
63+
doc="Nix expression to build. Rule name used as attribute if not present.",
64+
),
5265
"path": attr.string(),
5366
"repository": attr.label(),
5467
"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)