Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arbitrary expressions in attribute_path. #4

Merged
merged 3 commits into from
Dec 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
# Name <email address>

Mathieu Boespflug <[email protected]>
Mateusz Kowalczyk <[email protected]>
19 changes: 19 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ nixpkgs_git_repository(
)

nixpkgs_package(name = "hello", repository = "@nixpkgs")

nixpkgs_package(
name = "expr-test",
expression = "let pkgs = import <nixpkgs> {}; in pkgs.hello",
repository = "@nixpkgs"
)

nixpkgs_package(
name = "attribute-test",
attribute_path = "hello",
repository = "@nixpkgs"
)

nixpkgs_package(
name = "expr-attribute-test",
expression = "import <nixpkgs> {}",
attribute_path = "hello",
repository = "@nixpkgs",
)
25 changes: 19 additions & 6 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ def _nixpkgs_package_impl(ctx):
else:
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))

path = '<nixpkgs>'
# If neither repository or path are set, leave empty which will use
# default value from NIX_PATH.
path = []
if ctx.attr.repository and ctx.attr.path:
fail("'repository' and 'path' fields are mutually exclusive.")
if ctx.attr.repository:
# XXX Another hack: the repository label typically resolves to
# some top-level package in the external workspace. So we use
# dirname to get the actual workspace path.
path = ctx.path(ctx.attr.repository).dirname
path = ["-I", "nixpkgs={0}".format(ctx.path(ctx.attr.repository).dirname)]
if ctx.attr.path:
path = ctx.attr.path
path = ["-I", "nixpkgs={0}".format(ctx.attr.path)]

extraArgs = [
"-E", ctx.attr.expression or "import <nixpkgs> {}",
"-A", ctx.attr.attribute_path
if ctx.attr.expression
else ctx.attr.attribute_path or ctx.attr.name,
]
buildCmd = ["nix-build"] + path + ["--no-out-link"] + extraArgs

attr_path = ctx.attr.attribute_path or ctx.name
buildCmd = ["nix-build", path, "-A", attr_path, "--no-out-link"]
res = ctx.execute(buildCmd, quiet = False)
if res.return_code == 0:
output_path = res.stdout.splitlines()[-1]
Expand All @@ -48,7 +56,12 @@ def _nixpkgs_package_impl(ctx):
nixpkgs_package = repository_rule(
implementation = _nixpkgs_package_impl,
attrs = {
"attribute_path": attr.string(),
"attribute_path": attr.string(
doc="Nix attribute to build. Exclusive to expression."
),
"expression": attr.string(
doc="Nix expression to build. Rule name used as attribute if not present.",
),
"path": attr.string(),
"repository": attr.label(),
"build_file": attr.label(),
Expand Down
15 changes: 10 additions & 5 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package(default_testonly = 1)

sh_test(
name = "run-hello",
[sh_test(
name= "run-{0}".format(test),
srcs = ["test_bin.sh"],
args = ["$(location @hello//:bin)"],
data = ["@hello//:bin"],
args=["$(location @{0}//:bin)".format(test)],
data = ["@{0}//:bin".format(test)],
timeout = "short",
)
) for test in [
"hello",
"expr-test",
"attribute-test",
"expr-attribute-test",
]]