@@ -15,6 +15,19 @@ nixpkgs_git_repository = repository_rule(
15
15
local = False ,
16
16
)
17
17
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
+
18
31
def _nixpkgs_package_impl (ctx ):
19
32
if ctx .attr .build_file and ctx .attr .build_file_content :
20
33
fail ("Specify one of 'build_file' or 'build_file_content', but not both." )
@@ -25,19 +38,22 @@ def _nixpkgs_package_impl(ctx):
25
38
else :
26
39
ctx .template ("BUILD" , Label ("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg" ))
27
40
28
- path = '<nixpkgs>'
41
+ # If neither repository or path are set, leave empty which will use
42
+ # default value from NIX_PATH
43
+ path = []
29
44
if ctx .attr .repository and ctx .attr .path :
30
45
fail ("'repository' and 'path' fields are mutually exclusive." )
31
46
if ctx .attr .repository :
32
47
# XXX Another hack: the repository label typically resolves to
33
48
# some top-level package in the external workspace. So we use
34
49
# 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 )]
36
51
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
38
56
39
- attr_path = ctx .attr .attribute_path or ctx .name
40
- buildCmd = ["nix-build" , path , "-A" , attr_path , "--no-out-link" ]
41
57
res = ctx .execute (buildCmd , quiet = False )
42
58
if res .return_code == 0 :
43
59
output_path = res .stdout .splitlines ()[- 1 ]
@@ -48,7 +64,12 @@ def _nixpkgs_package_impl(ctx):
48
64
nixpkgs_package = repository_rule (
49
65
implementation = _nixpkgs_package_impl ,
50
66
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
+ ),
52
73
"path" : attr .string (),
53
74
"repository" : attr .label (),
54
75
"build_file" : attr .label (),
0 commit comments