@@ -30,13 +30,19 @@ def _nixpkgs_package_impl(ctx):
30
30
else :
31
31
ctx .template ("BUILD" , Label ("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg" ))
32
32
33
+ strFailureImplicitNixpkgs = (
34
+ "One of 'path', 'repository', 'nix_file' or 'nix_file_content' must be provided. "
35
+ + "The NIX_PATH environment variable is not inherited." )
36
+
33
37
expr_args = []
34
38
if ctx .attr .nix_file and ctx .attr .nix_file_content :
35
39
fail ("Specify one of 'nix_file' or 'nix_file_content', but not both." )
36
40
elif ctx .attr .nix_file :
37
41
ctx .symlink (ctx .attr .nix_file , "default.nix" )
38
42
elif ctx .attr .nix_file_content :
39
43
expr_args = ["-E" , ctx .attr .nix_file_content ]
44
+ elif not (ctx .attr .path or ctx .attr .repository ):
45
+ fail (strFailureImplicitNixpkgs )
40
46
else :
41
47
expr_args = ["-E" , "import <nixpkgs> {}" ]
42
48
@@ -54,35 +60,34 @@ def _nixpkgs_package_impl(ctx):
54
60
])
55
61
56
62
# If neither repository or path are set, leave empty which will use
57
- # default value from NIX_PATH.
58
- path = []
63
+ # default value from NIX_PATH, which will fail unless a pinned nixpkgs is
64
+ # set in the 'nix_file' attribute.
65
+ nix_path = ""
59
66
if ctx .attr .repository and ctx .attr .path :
60
67
fail ("'repository' and 'path' attributes are mutually exclusive." )
61
68
elif ctx .attr .repository :
62
69
# XXX Another hack: the repository label typically resolves to
63
70
# some top-level package in the external workspace. So we use
64
71
# dirname to get the actual workspace path.
65
- path = [ "-I" , "nixpkgs={0}" . format (ctx .path (ctx .attr .repository ).dirname )]
72
+ nix_path = str (ctx .path (ctx .attr .repository ).dirname )
66
73
elif ctx .attr .path :
67
- path = ["-I" , "nixpkgs={0}" .format (ctx .attr .path )]
68
- else :
69
- print ("""
70
- WARNING: Implicitly using '<nixpkgs>' as the location of Nixpkgs.
71
- This is not recommended because it makes the build non-hermetic.
72
- Set which Nixpkgs to use explicitly using 'repository' or 'path' attributes.
73
- """ )
74
+ nix_path = str (ctx .attr_path )
75
+ elif not (ctx .attr .nix_file or ctx .attr .nix_file_content ):
76
+ fail (strFailureImplicitNixpkgs )
74
77
75
78
nix_build_path = ctx .which ("nix-build" )
76
79
if nix_build_path == None :
77
80
fail ("Could not find nix-build on the path. Please install it. See: https://nixos.org/nix/" )
78
81
79
- nix_build = [nix_build_path ] + path + expr_args
82
+ nix_build = [nix_build_path ] + expr_args
80
83
81
84
# Large enough integer that Bazel can still parse. We don't have
82
85
# access to MAX_INT and 0 is not a valid timeout so this is as good
83
86
# as we can do.
84
87
timeout = 1073741824
85
- res = ctx .execute (nix_build , quiet = False , timeout = timeout )
88
+
89
+ res = ctx .execute (nix_build , quiet = False , timeout = timeout ,
90
+ environment = dict (NIX_PATH = "nixpkgs=" + nix_path ))
86
91
if res .return_code == 0 :
87
92
output_path = res .stdout .splitlines ()[- 1 ]
88
93
else :
@@ -118,5 +123,4 @@ nixpkgs_package = repository_rule(
118
123
"build_file_content" : attr .string (),
119
124
},
120
125
local = True ,
121
- environ = ["NIX_PATH" ],
122
126
)
0 commit comments