Skip to content

Commit 5e34b6e

Browse files
committed
Simplify attribute selection logic.
1 parent 085bd69 commit 5e34b6e

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

nixpkgs/nixpkgs.bzl

+8-16
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ 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_path:
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_path or ctx.attr.name]
30-
3118
def _nixpkgs_package_impl(ctx):
3219
if ctx.attr.build_file and ctx.attr.build_file_content:
3320
fail("Specify one of 'build_file' or 'build_file_content', but not both.")
@@ -39,7 +26,7 @@ def _nixpkgs_package_impl(ctx):
3926
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
4027

4128
# If neither repository or path are set, leave empty which will use
42-
# default value from NIX_PATH
29+
# default value from NIX_PATH.
4330
path = []
4431
if ctx.attr.repository and ctx.attr.path:
4532
fail("'repository' and 'path' fields are mutually exclusive.")
@@ -51,8 +38,13 @@ def _nixpkgs_package_impl(ctx):
5138
if ctx.attr.path:
5239
path = ["-I", "nixpkgs={0}".format(ctx.attr.path)]
5340

54-
buildExpr = _mk_build_expression(ctx)
55-
buildCmd = ["nix-build"] + path + ["--no-out-link"] + buildExpr
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
5648

5749
res = ctx.execute(buildCmd, quiet = False)
5850
if res.return_code == 0:

0 commit comments

Comments
 (0)