@@ -15,6 +15,16 @@ 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 ctx .attr .attribute and ctx .attr .expression :
22
+ fail ("'attribute' and 'expression' are mutually exclusive." )
23
+ elif ctx .attr .expression :
24
+ return ["-E" , ctx .attr .expression ]
25
+ else :
26
+ return ["-E" , "(import <nixpkgs> {{}}).{0}" .format (ctx .attr .attribute or ctx .attr .name )]
27
+
18
28
def _nixpkgs_package_impl (ctx ):
19
29
if ctx .attr .build_file and ctx .attr .build_file_content :
20
30
fail ("Specify one of 'build_file' or 'build_file_content', but not both." )
@@ -25,19 +35,22 @@ def _nixpkgs_package_impl(ctx):
25
35
else :
26
36
ctx .template ("BUILD" , Label ("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg" ))
27
37
28
- path = '<nixpkgs>'
38
+ # If neither repository or path are set, leave empty which will use
39
+ # default value from NIX_PATH
40
+ path = ''
29
41
if ctx .attr .repository and ctx .attr .path :
30
42
fail ("'repository' and 'path' fields are mutually exclusive." )
31
43
if ctx .attr .repository :
32
44
# XXX Another hack: the repository label typically resolves to
33
45
# some top-level package in the external workspace. So we use
34
46
# dirname to get the actual workspace path.
35
- path = ctx .path (ctx .attr .repository ).dirname
47
+ path = "-I nixpkgs={0}" . format ( ctx .path (ctx .attr .repository ).dirname )
36
48
if ctx .attr .path :
37
- path = ctx .attr .path
49
+ path = "-I nixpkgs={0}" .format (ctx .attr .path )
50
+
51
+ buildExpr = _mk_build_expression (ctx )
52
+ buildCmd = ["nix-build" , path , "--no-out-link" ] + buildExpr
38
53
39
- attr_path = ctx .attr .attribute_path or ctx .name
40
- buildCmd = ["nix-build" , path , "-A" , attr_path , "--no-out-link" ]
41
54
res = ctx .execute (buildCmd , quiet = False )
42
55
if res .return_code == 0 :
43
56
output_path = res .stdout .splitlines ()[- 1 ]
@@ -48,7 +61,12 @@ def _nixpkgs_package_impl(ctx):
48
61
nixpkgs_package = repository_rule (
49
62
implementation = _nixpkgs_package_impl ,
50
63
attrs = {
51
- "attribute_path" : attr .string (),
64
+ "attribute" : attr .string (
65
+ doc = "Nix attribute to build. Exclusive to expression."
66
+ ),
67
+ "expression" : attr .string (
68
+ doc = "Nix expression to build. Rule name used as attribute if not present." ,
69
+ ),
52
70
"path" : attr .string (),
53
71
"repository" : attr .label (),
54
72
"build_file" : attr .string (),
0 commit comments