-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathnixpkgs.bzl
72 lines (66 loc) · 2.49 KB
/
nixpkgs.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def _nixpkgs_git_repository_impl(ctx):
ctx.file('BUILD', content = 'filegroup(name = "%s", glob = ["**"])' % ctx.name)
# XXX Hack because ctx.path below bails out if resolved path not a regular file.
ctx.file(ctx.name)
ctx.download_and_extract(
url = "https://github.com/NixOS/nixpkgs/archive/%s.tar.gz" % ctx.attr.revision,
stripPrefix = "nixpkgs-" + ctx.attr.revision,
)
nixpkgs_git_repository = repository_rule(
implementation = _nixpkgs_git_repository_impl,
attrs = {
"revision": attr.string(),
},
local = False,
)
def _nixpkgs_package_impl(ctx):
if ctx.attr.build_file and ctx.attr.build_file_content:
fail("Specify one of 'build_file' or 'build_file_content', but not both.")
elif ctx.attr.build_file:
ctx.symlink(ctx.attr.build_file, "BUILD")
elif ctx.attr.build_file_content:
ctx.file("BUILD", content = ctx.attr.build_file_content)
else:
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
# If neither repository or path are set, leave empty which will use
# default value from NIX_PATH.
path = []
if ctx.attr.repository and ctx.attr.path:
fail("'repository' and 'path' fields are mutually exclusive.")
if ctx.attr.repository:
# XXX Another hack: the repository label typically resolves to
# some top-level package in the external workspace. So we use
# dirname to get the actual workspace path.
path = ["-I", "nixpkgs={0}".format(ctx.path(ctx.attr.repository).dirname)]
if ctx.attr.path:
path = ["-I", "nixpkgs={0}".format(ctx.attr.path)]
extraArgs = [
"-E", ctx.attr.expression or "import <nixpkgs> {}",
"-A", ctx.attr.attribute_path
if ctx.attr.expression
else ctx.attr.attribute_path or ctx.attr.name,
]
buildCmd = ["nix-build"] + path + ["--no-out-link"] + extraArgs
res = ctx.execute(buildCmd, quiet = False)
if res.return_code == 0:
output_path = res.stdout.splitlines()[-1]
else:
fail("Cannot build Nix attribute %s." % ctx.attr.name)
ctx.symlink(output_path, "nix")
nixpkgs_package = repository_rule(
implementation = _nixpkgs_package_impl,
attrs = {
"attribute_path": attr.string(
doc="Nix attribute to build. Exclusive to expression."
),
"expression": attr.string(
doc="Nix expression to build. Rule name used as attribute if not present.",
),
"path": attr.string(),
"repository": attr.label(),
"build_file": attr.label(),
"build_file_content": attr.string(),
},
local = True,
environ = ["NIX_PATH"],
)