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