diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 6a765791..4b3b9890 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -10,3 +10,4 @@ # Name Mathieu Boespflug +Mateusz Kowalczyk diff --git a/WORKSPACE b/WORKSPACE index 5d06ecc3..5050a51d 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -10,3 +10,22 @@ nixpkgs_git_repository( ) nixpkgs_package(name = "hello", repository = "@nixpkgs") + +nixpkgs_package( + name = "expr-test", + expression = "let pkgs = import {}; in pkgs.hello", + repository = "@nixpkgs" +) + +nixpkgs_package( + name = "attribute-test", + attribute_path = "hello", + repository = "@nixpkgs" +) + +nixpkgs_package( + name = "expr-attribute-test", + expression = "import {}", + attribute_path = "hello", + repository = "@nixpkgs", +) diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 6f850626..b9c682c9 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -25,19 +25,27 @@ def _nixpkgs_package_impl(ctx): else: ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg")) - path = '' + # 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 {}", + "-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] @@ -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(), diff --git a/tests/BUILD b/tests/BUILD index 42e02273..1937ff54 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -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", +]]