Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 689e297

Browse files
committedAug 8, 2018
Fix explicit nixpkgs
- providing no `path`, `repository`, `nix_file`, `nix_file_content` means that nixpkgs is implicit and will fail in bazel - we ensure that NIX_PATH cannot be used.
1 parent 4575647 commit 689e297

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed
 

‎README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ nixpkgs_package(
103103
)
104104
```
105105

106-
If neither `repository` or `path` are specified, `<nixpkgs>` is
107-
assumed. Specifying one of `repository` or `path` is strongly
106+
If neither `repository` or `path` are specified, you must provide a
107+
nixpkgs clone in `nix_file` or `nix_file_content`. Specifying one of
108+
`repository` or `path` is strongly
108109
recommended. The two are mutually exclusive.
109110

110111
<table class="table table-condensed table-bordered table-params">

‎nixpkgs/nixpkgs.bzl

+17-13
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ def _nixpkgs_package_impl(ctx):
3030
else:
3131
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
3232

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+
3337
expr_args = []
3438
if ctx.attr.nix_file and ctx.attr.nix_file_content:
3539
fail("Specify one of 'nix_file' or 'nix_file_content', but not both.")
3640
elif ctx.attr.nix_file:
3741
ctx.symlink(ctx.attr.nix_file, "default.nix")
3842
elif ctx.attr.nix_file_content:
3943
expr_args = ["-E", ctx.attr.nix_file_content]
44+
elif not (ctx.attr.path or ctx.attr.repository):
45+
fail(strFailureImplicitNixpkgs)
4046
else:
4147
expr_args = ["-E", "import <nixpkgs> {}"]
4248

@@ -54,35 +60,34 @@ def _nixpkgs_package_impl(ctx):
5460
])
5561

5662
# 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 = ""
5966
if ctx.attr.repository and ctx.attr.path:
6067
fail("'repository' and 'path' attributes are mutually exclusive.")
6168
elif ctx.attr.repository:
6269
# XXX Another hack: the repository label typically resolves to
6370
# some top-level package in the external workspace. So we use
6471
# 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)
6673
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)
7477

7578
nix_build_path = ctx.which("nix-build")
7679
if nix_build_path == None:
7780
fail("Could not find nix-build on the path. Please install it. See: https://nixos.org/nix/")
7881

79-
nix_build = [nix_build_path] + path + expr_args
82+
nix_build = [nix_build_path] + expr_args
8083

8184
# Large enough integer that Bazel can still parse. We don't have
8285
# access to MAX_INT and 0 is not a valid timeout so this is as good
8386
# as we can do.
8487
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))
8691
if res.return_code == 0:
8792
output_path = res.stdout.splitlines()[-1]
8893
else:
@@ -118,5 +123,4 @@ nixpkgs_package = repository_rule(
118123
"build_file_content": attr.string(),
119124
},
120125
local = True,
121-
environ = ["NIX_PATH"],
122126
)

0 commit comments

Comments
 (0)
Please sign in to comment.