-
Notifications
You must be signed in to change notification settings - Fork 321
/
nix.bzl
167 lines (153 loc) · 4.64 KB
/
nix.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
load(
"@prelude//python:toolchain.bzl",
"PythonToolchainInfo",
)
load(
"//build_context:toolchain.bzl",
"BuildContextToolchainInfo",
)
load(
"//git:toolchain.bzl",
"GitToolchainInfo",
)
load(
"//nix:toolchain.bzl",
"NixToolchainInfo",
)
load(
"//build_context.bzl",
"BuildContext",
_build_context = "build_context",
)
load(
"//git.bzl",
"GitInfo",
_git_info = "git_info",
)
load(
"//artifact.bzl",
"ArtifactInfo",
)
NixOmnibusPkgInfo = provider(fields = {
"artifact": provider_field(typing.Any, default = None), # [Artifact]
"build_metadata": provider_field(typing.Any, default = None), # [Artifact]
"pkg_metadata": provider_field(typing.Any, default = None), # [Artifact]
})
def nix_flake_lock_impl(ctx: AnalysisContext) -> list[DefaultInfo]:
out = ctx.actions.declare_output("flake.lock")
output = ctx.actions.copy_file(out, ctx.attrs.src)
return [DefaultInfo(default_output = out)]
nix_flake_lock = rule(
impl = nix_flake_lock_impl,
attrs = {
"src": attrs.source(
doc = """flake.lock source.""",
),
"nix_flake": attrs.dep(
default = "//:flake.nix",
doc = """Nix flake dependency.""",
),
},
)
def nix_omnibus_pkg_impl(ctx: AnalysisContext) -> list[[
DefaultInfo,
NixOmnibusPkgInfo,
ArtifactInfo,
GitInfo,
]]:
if ctx.attrs.pkg_name:
name = ctx.attrs.pkg_name
else:
name = ctx.attrs.name
build_context = _build_context(ctx, [ctx.attrs.build_dep], ctx.attrs.srcs)
git_info = _git_info(ctx)
artifact = ctx.actions.declare_output("{}.tar.gz".format(name))
build_metadata = ctx.actions.declare_output("build_metadata.json")
pkg_metadata = ctx.actions.declare_output("pkg_metadata.json")
nix_toolchain = ctx.attrs._nix_toolchain[NixToolchainInfo]
cmd = cmd_args(
ctx.attrs._python_toolchain[PythonToolchainInfo].interpreter,
nix_toolchain.nix_omnibus_pkg_build[DefaultInfo].default_outputs,
"--git-info-json",
git_info.file,
"--artifact-out-file",
artifact.as_output(),
"--build-metadata-out-file",
build_metadata.as_output(),
"--pkg-metadata-out-file",
pkg_metadata.as_output(),
"--build-context-dir",
build_context.root,
"--name",
name,
"--author",
ctx.attrs.author,
"--source-url",
ctx.attrs.source_url,
"--license",
ctx.attrs.license,
)
ctx.actions.run(cmd, category = "nix_omnibus_pkg_build")
return [
DefaultInfo(
default_output = artifact,
),
NixOmnibusPkgInfo(
artifact = artifact,
build_metadata = build_metadata,
pkg_metadata = pkg_metadata,
),
ArtifactInfo(
artifact = artifact,
metadata = build_metadata,
# TODO(johnrwatson): it would be better to calculate these fields
# or have some other manner or determining them.
family = "nix_omnibus",
variant = "tar",
),
git_info,
]
nix_omnibus_pkg = rule(
impl = nix_omnibus_pkg_impl,
attrs = {
"pkg_name": attrs.option(
attrs.string(),
default = None,
doc = """package name (default: 'attrs.name').""",
),
"build_dep": attrs.dep(
doc = """Buck2 target that will be built in a package.""",
),
"srcs": attrs.dict(
attrs.source(allow_directory = True),
attrs.string(),
default = {},
doc = """Mapping of sources files to the relative directory in a build context..""",
),
"author": attrs.string(
doc = """Image author to be used in image metadata.""",
),
"source_url": attrs.string(
doc = """Source code URL to be used in image metadata.""",
),
"license": attrs.string(
doc = """Image license string to be used in image metadata.""",
),
"_python_toolchain": attrs.toolchain_dep(
default = "toolchains//:python",
providers = [PythonToolchainInfo],
),
"_build_context_toolchain": attrs.toolchain_dep(
default = "toolchains//:build_context",
providers = [BuildContextToolchainInfo],
),
"_git_toolchain": attrs.toolchain_dep(
default = "toolchains//:git",
providers = [GitToolchainInfo],
),
"_nix_toolchain": attrs.toolchain_dep(
default = "toolchains//:nix",
providers = [NixToolchainInfo],
),
},
)