-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchlinux.lib.nix
355 lines (327 loc) · 10.6 KB
/
archlinux.lib.nix
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Library for parsing Arch Linux formats using Nix.
#
rec
{
#
# Generic helpers
#
# Identity function (it is the identity function).
identity = x: x;
# Strips the given chars from the start and end of the string.
strip = char: builtins.match "^${char}*(.*[^${char}])?${char}+$";
# Flattens one level of lists
flatten = builtins.foldl' (fin: attr: fin ++ attr) [];
# Reverses a list
reverse = builtins.foldl' (coll: item: [item] ++ coll) [];
# Removes nulls from a list
compact = builtins.filter (i: i != null);
# Keeps only unique elements from a list
unique =
builtins.foldl'
(ret: curr:
if builtins.elem curr ret
then ret
else (ret ++ [curr])
)
[]
;
firstChar = str: builtins.head (builtins.match "^(.).*" str);
mergeDeep' = mergeOperations: first: second:
first // second //
(
let
common = builtins.intersectAttrs first second;
in
builtins.mapAttrs (
name: value:
let
first' = first."${name}";
second' = second."${name}";
mergeOp = "${builtins.typeOf first'}+${builtins.typeOf second'}";
in
if mergeOperations ? "${mergeOp}"
then (mergeOperations."${mergeOp}" mergeOperations first' second')
else second'
) common
)
;
mergeOps' = {
deep = {
"set+set" =
selfOps: mergeDeep' selfOps
;
};
shallow = {
"set+set" =
_: a: b: (a // b)
;
# Shallow list+list
"list+list" =
_: a: b: a ++ b
;
};
};
mergeAttrsDeep = mergeDeep' {
"set+set" = mergeOps'.deep."set+set";
};
mergeAttrsDeepAndListsShallow = mergeDeep' {
"set+set" = mergeOps'.deep."set+set";
"list+list" = mergeOps'.shallow."list+list";
};
#
# ArchLinux formats parsing
#
# Transforms a "db" entry into a list of the form: `[ [ key ] value ... ]`
dbToKVList =
desc:
builtins.tail (
builtins.split "\n*%([^%]+)%\n*" (builtins.head (strip "\n" desc))
)
;
# Given a pair of ordered list keys/values, makes the matching attrset.
KVListsToAttrs =
elements:
builtins.listToAttrs (
builtins.map (i:
{
name =
builtins.head
(builtins.elemAt elements.keys i)
;
value =
builtins.filter
(el: (builtins.typeOf el) == "string")
(
builtins.split "\n"
(builtins.elemAt elements.values i)
)
;
}
) (builtins.genList (x: (x + 1) - 1) (builtins.length elements.keys))
)
;
# Transforms a list of the form `[ [ key ] value ... ]` into a pair of ordered list keys/values.
KVListToKVLists =
list:
let
inherit
(
builtins.partition
(el: (builtins.typeOf el) == "list")
list
)
right # the keys
wrong # the values
;
in
{
keys = right;
values = wrong;
}
;
#
# Actual interfaces that should be used
#
db = {
# db.parse (builtins.readFile file)
parse = desc: KVListsToAttrs (KVListToKVLists (dbToKVList desc));
# db.all {
# path = ./path/to/unpacked/reponame;
# /* defaults to the basename of the target path */ repo = "reponame";
# }
all = { path, repo ? builtins.unsafeDiscardStringContext (builtins.baseNameOf path) }:
let
packagesAttrs =
builtins.listToAttrs (builtins.attrValues (
builtins.mapAttrs (packagePath: _type:
let
value = db.parse (builtins.readFile (path + "/${packagePath}/desc"));
in
{
value = value // {
"$repo" = repo;
};
name = builtins.head value."NAME";
}
)
(builtins.readDir path)
))
;
packages = builtins.attrValues packagesAttrs;
in
{
# This lets us attach a bit more data.
"$db" = {
"provides" =
let
nameOnly = entry: builtins.head (builtins.match "([^=]+).*" entry);
# NOTE: This is losing the versioning information...
# This is *okay* since the use-case is to automatically cast
# a wide net around packages to fetch for using in the sandbox.
list =
flatten
(builtins.map (package:
if !(package ? PROVIDES) then [] else
builtins.map
(provider: { name = nameOnly provider; value = builtins.head package.NAME; })
package.PROVIDES
) packages)
;
in
builtins.mapAttrs
(provide: entries: builtins.map (entry: entry.value) entries)
(builtins.groupBy (entry:
entry.name
) list)
;
};
} // packagesAttrs
;
# Given a list of `db.all` output, merge them appropriately.
merge =
builtins.foldl'
(coll: curr:
# We do NOT want to merge those deep.
# Any repository defined after has priority for a same-name package.
# TODO: verify this assumption is correct.
coll //
curr //
{
"$db" = mergeAttrsDeepAndListsShallow coll."$db" curr."$db";
}
)
{
"$db".provides = {};
}
;
# Given a package set, and a package description, returns a list of package descriptions it directly depends on.
depsForPackage =
{ packages # Output from e.g. `db.all`
, package # A single package desc
, _seen ? [] # Used internally to prevent infrec by keeping a tally of seen packages
}:
# From: PKGBUILD(5)
# > depends (array)
# > An array of packages this package depends on to run.
# > Entries in this list should be surrounded with single
# > quotes and contain at least the package name. Entries can
# > also include a version requirement of the form
# > name<>version, where <> is one of five comparisons: >=
# > (greater than or equal to), <= (less than or equal to), =
# > (equal to), > (greater than), or < (less than).
# >
# > If the dependency name appears to be a library (ends with
# > .so), makepkg will try to find a binary that depends on the
# > library in the built package and append the version needed
# > by the binary. Appending the version yourself disables
# > automatic detection.
# >
# > Additional architecture-specific depends can be added by
# > appending an underscore and the architecture name e.g.,
# > depends_x86_64=().
let
compOperators = [
">=" # (greater than or equal to)
"<=" # (less than or equal to)
"=" # (equal to)
">" # (greater than)
"<" # (less than)
];
parseDep = builtins.match "^([^><=]+)[><=]*(.*)$";
in
compact (
builtins.map (depName:
let
parsed = parseDep depName;
parsedDepName = builtins.head parsed;
parsedDepPayload = builtins.tail parsed;
in
# Skips over packages already handled
if builtins.elem parsedDepName _seen then null else
# NOTE: This is losing the versioning information...
# This is *okay* since the use-case is to automatically cast
# a wide net around packages to fetch for using in the sandbox.
# Is that dep in the packages set?
if packages ? "${parsedDepName}"
then packages."${parsedDepName}"
else
# Is that dep in the provides set?
if packages."$db"."provides" ? ${parsedDepName}
then (/* NOTE: incomplete semantics. Only picks whatever is first in the list. */
let
name = builtins.head packages."$db"."provides"."${parsedDepName}";
in
packages."${name}"
)
else
(builtins.trace "WARNING: missing dep ${parsedDepName}... skipping it even though we shouldn't!" null)
) (if package ? DEPENDS then package.DEPENDS else [])
)
;
# Given a package set, and a package description, returns a list of all package descriptions it depends on.
allDepsForPackage =
{ packages # Output from e.g. `db.all`
, package # A single package desc
, _seen ? [] # Used internally to prevent infrec by keeping a tally of seen packages
}:
let
selfDeps = db.depsForPackage { inherit packages package _seen; };
prev_seen = _seen;
in
let
_seen = prev_seen ++ (flatten (builtins.map (p: p.NAME) selfDeps));
in
unique (
selfDeps ++ (flatten (builtins.map (package: db.allDepsForPackage { inherit packages package _seen; }) selfDeps))
)
;
# Given a package set, and a list of package names, returns a list of package descriptions the packages depends on.
# This is useful to describe a set of packages as a list of names
allDepsForPackageNames =
{ packages # Output from e.g. `db.all`
, names # List of package names to resolved dependencies for
}:
db.allDepsForPackage {
inherit packages;
# Synthetic package desc
package = {
DEPENDS = names;
};
}
;
};
repo = {
repos = { arch, name }: {
#core = "https://geo.mirror.pkgbuild.com/core/os/${arch}/";
#extra = "https://geo.mirror.pkgbuild.com/extra/os/${arch}/";
# Relying on the archlinux packages archive, otherwise it's hard to
# reproduce the builds, since inputs disappear eagerly.
core = "https://archive.archlinux.org/packages/${firstChar name}/${name}/";
extra = "https://archive.archlinux.org/packages/${firstChar name}/${name}/";
};
fetchPackage =
let _repos = repo.repos; in # Keep the right `repos` ref around to break infrec...
{ desc
, repos ? _repos
, repo ? desc."$repo"
, arch ? builtins.head (builtins.match "^([^-]+)-.*" builtins.currentSystem)
}:
let
h = builtins.head;
name = h desc.NAME;
filename = h desc.FILENAME;
sha256 = h desc.SHA256SUM;
storeEscape = builtins.replaceStrings [":"] ["__COLON__"];
in
{
inherit filename;
file = (builtins.fetchurl {
name = storeEscape filename;
url = (repos { inherit arch name; })."${repo}" + filename;
inherit sha256;
});
}
;
};
}