-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathnix_evaluation.py
More file actions
341 lines (266 loc) · 10.7 KB
/
nix_evaluation.py
File metadata and controls
341 lines (266 loc) · 10.7 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from django.contrib.postgres import fields
from django.contrib.postgres.indexes import BTreeIndex, GinIndex
from django.contrib.postgres.search import SearchVectorField
from django.db import models
from django.utils.translation import gettext_lazy as _
from pgtrigger import UpdateSearchVector
def text_length(choices: type[models.TextChoices]) -> int:
return max(map(len, choices.values))
class TimeStampMixin(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta: # type: ignore[override]
abstract = True
class NixMaintainer(models.Model):
"""
This represents a maintainer in the `maintainers` field of a package.
"""
github_id = models.IntegerField(unique=True, primary_key=True)
github = models.CharField(max_length=200, unique=True)
email = models.CharField(max_length=200, null=True)
matrix = models.CharField(max_length=200, null=True)
name = models.CharField(max_length=200, null=True)
def __str__(self) -> str:
return f"@{self.github}"
class NixLicense(models.Model):
"""
This represents a Nix license data, we don't keep only the SPDX ID
for maximal faithfulness to what nixpkgs tells us.
We refuse any license without an SPDX ID, it is not realistic
to handle them without as this would make reconcilation hard.
"""
spdx_id = models.CharField(max_length=255, unique=True)
deprecated = models.BooleanField()
free = models.BooleanField()
full_name = models.CharField(max_length=255, null=True)
short_name = models.CharField(max_length=255, null=True)
redistributable = models.BooleanField()
url = models.URLField(null=True)
def __str__(self) -> str:
return f"{self.spdx_id}"
class NixSourceProvenance(models.Model):
"""
Source provenance informs you about whether
something is a binary native code or comes from a real source build.
"""
qualifier = models.CharField(max_length=255)
source = models.BooleanField()
# TODO: define binary as !source
class NixPlatform(models.Model):
"""
e.g. x86_64-linux.
"""
system_double = models.CharField(max_length=255, unique=True)
def __str__(self) -> str:
return self.system_double
class NixDerivationMeta(models.Model):
"""
All the meta attribute of a derivation
is synthesized here.
"""
name = models.CharField(max_length=255, null=True)
maintainers = models.ManyToManyField(NixMaintainer)
licenses = models.ManyToManyField(NixLicense)
source_provenances = models.ManyToManyField(NixSourceProvenance)
known_vulnerabilities = fields.ArrayField(
models.CharField(max_length=255), default=list
)
insecure = models.BooleanField()
available = models.BooleanField()
broken = models.BooleanField()
unfree = models.BooleanField()
unsupported = models.BooleanField()
homepage = models.URLField(null=True)
description = models.TextField(null=True)
main_program = models.CharField(max_length=255, null=True)
# FIXME(raitobezarius):
# Ridiculously big, we should encode all reasonable known platforms
# into a static BitField (~120 of them, so 7 bits?).
# Ideally, we should find a way to deal with "inspect patterns"
# which are really dynamic things, maybe just project over our set of statically
# known platforms.
# platforms = models.ManyToManyField(NixPlatform)
position = models.URLField(null=True)
search_vector = SearchVectorField(null=True)
def __str__(self) -> str:
return self.description or ""
class Meta: # type: ignore[override]
indexes = [
# Add a GIN index to speed up vector search queries
GinIndex(fields=["search_vector"]),
]
triggers = [
# Add a trigger to maintain the search vector updated with row changes
UpdateSearchVector(
name="description_search_vector_idx",
vector_field="search_vector",
document_fields=[
"description",
],
)
]
class NixOutput(models.Model):
"""
This is all the known outputs names.
"""
output_name = models.CharField(max_length=255, unique=True)
def __str__(self) -> str:
return self.output_name
class NixStorePathOutput(models.Model):
"""
This is all the outputs of a given derivation, e.g. out, doc, etc.
associated to their store paths.
This represents in database as '{store_path}!{out}'.
"""
store_path = models.CharField(max_length=255, unique=True)
def __hash__(self) -> int:
return hash(self.store_path)
class NixDerivationOutput(models.Model):
"""
A derivation may depend on another derivation,
but it must specify two things:
- derivation path
- output depended upon
e.g. depending on /nix/store/eeeeeeeeeeeeeee-something.drv and its 'out' output.
"""
outputs = models.ManyToManyField(NixOutput)
derivation_path = models.CharField(max_length=255)
class NixChannel(TimeStampMixin):
"""
This represents a "Nixpkgs" (*) channel, e.g.
- a Git object representing a branch that moves regularly.
- a state, e.g. EOL, deprecated, stable, unstable.
- if it's not unstable, release number.
(*): Anything that looks like Nixpkgs is also good.
"""
class ChannelState(models.TextChoices):
END_OF_LIFE = "END_OF_LIFE", _("End of life")
DEPRECATED = "DEPRECATED", _("Deprecated")
BETA = "BETA", _("Beta")
STABLE = "STABLE", _("Stable")
UNSTABLE = "UNSTABLE", _("Unstable")
# "Special" channel, for which the staging-branch == staging.
# The channel_branch is staging-next in this instance.
# But it's complicated because there's no channel move per se.
STAGING = "STAGING", _("Staging")
# A staging branch is the `release-$number` branch or `master` for unstable.
# Not to confuse with the staging branch itself.
staging_branch = models.CharField(max_length=255)
# A channel branch is the `nixos-$number` branch of
# `nixos-unstable(-small)` for unstable(-small). Not to confuse with the
# channel tarballs and scripts from releases.nixos.org.
channel_branch = models.CharField(max_length=255, primary_key=True)
# The currently known HEAD SHA1 commit of that channel.
head_sha1_commit = models.CharField(max_length=255)
state = models.CharField(
max_length=text_length(ChannelState), choices=ChannelState.choices
)
release_version = models.CharField(max_length=255, null=True)
# Repository can be stored as URLs for now...
# We can always reparse them as proper GitHub URIs if necessary
# It's a bit annoying though
# TODO(raitobezarius): make a proper ForeignKey?
repository = models.CharField(max_length=255)
def __str__(self) -> str:
return f"{self.staging_branch} -> {self.channel_branch} (Release: {self.release_version})"
class NixEvaluation(TimeStampMixin):
"""
This is a Nix evaluation of a repository,
potentially ongoing.
It contains its derivations via `derivations` attribute
set by the `NixDerivation` model.
"""
class EvaluationState(models.TextChoices):
COMPLETED = "COMPLETED", _("Completed")
WAITING = (
"WAITING",
_("Waiting to be started"),
)
IN_PROGRESS = (
"IN_PROGRESS",
_("In progress"),
)
# Crash means resource exhaustion, unexpected crash, or forced shutdown of the worker
CRASHED = (
"CRASHED",
_("Crashed"),
)
# Failed means critical evaluation errors
FAILED = "FAILED", _("Failed")
# Parent channel of that evaluation.
channel = models.ForeignKey(
NixChannel, related_name="evaluations", on_delete=models.CASCADE
)
# Commit SHA1 on which the evaluation was done precisely.
commit_sha1 = models.CharField(max_length=255)
# State in which the evaluation is in.
state = models.CharField(
max_length=text_length(EvaluationState), choices=EvaluationState.choices
)
# How many times have been we trying to evaluate
# this? We use it for the crash backoff loop.
attempt = models.IntegerField(default=0)
# Last failure reason
failure_reason = models.TextField(null=True)
# Time elapsed in seconds for this evaluation.
elapsed = models.FloatField(null=True)
def __str__(self) -> str:
return f"{self.channel} {self.commit_sha1[:8]}"
class Meta: # type: ignore[override]
unique_together = ("channel", "commit_sha1")
class NixPackage(models.Model):
name = models.CharField(max_length=255, unique=True)
def __str__(self) -> str:
return self.name
class NixDerivation(models.Model):
"""
This represents a Nix derivation "evaluated",
we fill this model using two things:
- evaluation result
- parsing the .drv
"""
package = models.ForeignKey(NixPackage, on_delete=models.CASCADE, null=True)
attribute = models.CharField(max_length=255)
derivation_path = models.CharField(max_length=255)
dependencies = models.ManyToManyField(NixDerivationOutput)
name = models.CharField(max_length=255)
metadata = models.OneToOneField(
NixDerivationMeta,
related_name="derivation",
on_delete=models.CASCADE,
null=True,
)
outputs = models.ManyToManyField(NixStorePathOutput)
system = models.CharField(max_length=255)
parent_evaluation = models.ForeignKey(
NixEvaluation, related_name="derivations", on_delete=models.CASCADE
)
search_vector = SearchVectorField(null=True)
def __str__(self) -> str:
hash = self.derivation_path.split("-")[0].split("/")[-1]
return f"{self.name} {hash[:8]}"
class Meta: # type: ignore[override]
indexes = [
BTreeIndex(fields=["name"]),
GinIndex(fields=["search_vector"]),
]
triggers = [
# Add a trigger to maintain the search vector updated with row changes
UpdateSearchVector(
name="attribute_name_search_vector_idx",
vector_field="search_vector",
document_fields=[
"attribute",
"name",
],
)
]
# Major channels are the important channels that a user wants to keep an eye on.
# FIXME figure this out dynamically
MAJOR_CHANNELS = ["23.11", "24.05", "24.11", "unstable"]
# The major channel that a branch name (e.g. nixpkgs-24.05-darwin) belongs to
def get_major_channel(branch_name: str) -> str | None:
for mc in MAJOR_CHANNELS:
if mc in branch_name:
return f"nixos-{mc}"
return None