1{ lib, ... }:
2rec {
3 # Borrowed from https://github.com/NixOS/nixpkgs/pull/355616
4 uniqueStrings = list: builtins.attrNames (builtins.groupBy lib.id list);
5
6 /*
7 Converts a `packagePlatformPath` into a `packagePlatformAttr`
8
9 Turns
10 "hello.aarch64-linux"
11 into
12 {
13 name = "hello";
14 packagePath = [ "hello" ];
15 platform = "aarch64-linux";
16 }
17 */
18 convertToPackagePlatformAttr =
19 packagePlatformPath:
20 let
21 # python312Packages.numpy.aarch64-linux -> ["python312Packages" "numpy" "aarch64-linux"]
22 splittedPath = lib.splitString "." packagePlatformPath;
23
24 # ["python312Packages" "numpy" "aarch64-linux"] -> ["python312Packages" "numpy"]
25 packagePath = lib.sublist 0 (lib.length splittedPath - 1) splittedPath;
26
27 # "python312Packages.numpy"
28 name = lib.concatStringsSep "." packagePath;
29 in
30 if name == "" then
31 null
32 else
33 {
34 # [ "python312Packages" "numpy" ]
35 inherit packagePath;
36
37 # python312Packages.numpy
38 inherit name;
39
40 # "aarch64-linux"
41 platform = lib.last splittedPath;
42 };
43
44 /*
45 Converts a list of `packagePlatformPath`s into a list of `packagePlatformAttr`s
46
47 Turns
48 [
49 "hello.aarch64-linux"
50 "hello.x86_64-linux"
51 "hello.aarch64-darwin"
52 "hello.x86_64-darwin"
53 "bye.x86_64-darwin"
54 "bye.aarch64-darwin"
55 "release-checks" <- Will be dropped
56 ]
57 into
58 [
59 { name = "hello"; platform = "aarch64-linux"; packagePath = [ "hello" ]; }
60 { name = "hello"; platform = "x86_64-linux"; packagePath = [ "hello" ]; }
61 { name = "hello"; platform = "aarch64-darwin"; packagePath = [ "hello" ]; }
62 { name = "hello"; platform = "x86_64-darwin"; packagePath = [ "hello" ]; }
63 { name = "bye"; platform = "aarch64-darwin"; packagePath = [ "hello" ]; }
64 { name = "bye"; platform = "x86_64-darwin"; packagePath = [ "hello" ]; }
65 ]
66 */
67 convertToPackagePlatformAttrs =
68 packagePlatformPaths:
69 builtins.filter (x: x != null) (builtins.map convertToPackagePlatformAttr packagePlatformPaths);
70
71 /*
72 Converts a list of `packagePlatformPath`s directly to a list of (unique) package names
73
74 Turns
75 [
76 "hello.aarch64-linux"
77 "hello.x86_64-linux"
78 "hello.aarch64-darwin"
79 "hello.x86_64-darwin"
80 "bye.x86_64-darwin"
81 "bye.aarch64-darwin"
82 ]
83 into
84 [
85 "hello"
86 "bye"
87 ]
88 */
89 extractPackageNames =
90 packagePlatformPaths:
91 let
92 packagePlatformAttrs = convertToPackagePlatformAttrs (uniqueStrings packagePlatformPaths);
93 in
94 uniqueStrings (builtins.map (p: p.name) packagePlatformAttrs);
95
96 /*
97 Computes the key difference between two attrs
98
99 {
100 added: [ <keys only in the second object> ],
101 removed: [ <keys only in the first object> ],
102 changed: [ <keys with different values between the two objects> ],
103 }
104 */
105 diff =
106 let
107 filterKeys = cond: attrs: lib.attrNames (lib.filterAttrs cond attrs);
108 in
109 old: new: {
110 added = filterKeys (n: _: !(old ? ${n})) new;
111 removed = filterKeys (n: _: !(new ? ${n})) old;
112 changed = filterKeys (
113 n: v:
114 # Filter out attributes that don't exist anymore
115 (new ? ${n})
116
117 # Filter out attributes that are the same as the new value
118 && (v != (new.${n}))
119 ) old;
120 };
121
122 /*
123 Group a list of `packagePlatformAttr`s by platforms
124
125 Turns
126 [
127 { name = "hello"; platform = "aarch64-linux"; ... }
128 { name = "hello"; platform = "x86_64-linux"; ... }
129 { name = "hello"; platform = "aarch64-darwin"; ... }
130 { name = "hello"; platform = "x86_64-darwin"; ... }
131 { name = "bye"; platform = "aarch64-darwin"; ... }
132 { name = "bye"; platform = "x86_64-darwin"; ... }
133 ]
134 into
135 {
136 aarch64-linux = [ "hello" ];
137 x86_64-linux = [ "hello" ];
138 aarch64-darwin = [ "hello" "bye" ];
139 x86_64-darwin = [ "hello" "bye" ];
140 }
141 */
142 groupByPlatform =
143 packagePlatformAttrs:
144 let
145 packagePlatformAttrsByPlatform = builtins.groupBy (p: p.platform) packagePlatformAttrs;
146 extractPackageNames = map (p: p.name);
147 in
148 lib.mapAttrs (_: extractPackageNames) packagePlatformAttrsByPlatform;
149
150 # Turns
151 # [
152 # { name = "hello"; platform = "aarch64-linux"; ... }
153 # { name = "hello"; platform = "x86_64-linux"; ... }
154 # { name = "hello"; platform = "aarch64-darwin"; ... }
155 # { name = "hello"; platform = "x86_64-darwin"; ... }
156 # { name = "bye"; platform = "aarch64-darwin"; ... }
157 # { name = "bye"; platform = "x86_64-darwin"; ... }
158 # ]
159 #
160 # into
161 #
162 # {
163 # linux = [ "hello" ];
164 # darwin = [ "hello" "bye" ];
165 # }
166 groupByKernel =
167 packagePlatformAttrs:
168 let
169 filterKernel =
170 kernel:
171 builtins.attrNames (
172 builtins.groupBy (p: p.name) (
173 builtins.filter (p: lib.hasSuffix kernel p.platform) packagePlatformAttrs
174 )
175 );
176 in
177 lib.genAttrs [ "linux" "darwin" ] filterKernel;
178
179 /*
180 Maps an attrs of `kernel - rebuild counts` mappings to a list of labels
181
182 Turns
183 {
184 linux = 56;
185 darwin = 1;
186 }
187 into
188 [
189 "10.rebuild-darwin: 1"
190 "10.rebuild-darwin: 1-10"
191 "10.rebuild-linux: 11-100"
192 ]
193 */
194 getLabels =
195 rebuildCountByKernel:
196 lib.concatLists (
197 lib.mapAttrsToList (
198 kernel: rebuildCount:
199 let
200 numbers =
201 if rebuildCount == 0 then
202 [ "0" ]
203 else if rebuildCount == 1 then
204 [
205 "1"
206 "1-10"
207 ]
208 else if rebuildCount <= 10 then
209 [ "1-10" ]
210 else if rebuildCount <= 100 then
211 [ "11-100" ]
212 else if rebuildCount <= 500 then
213 [ "101-500" ]
214 else if rebuildCount <= 1000 then
215 [
216 "501-1000"
217 "501+"
218 ]
219 else if rebuildCount <= 2500 then
220 [
221 "1001-2500"
222 "501+"
223 ]
224 else if rebuildCount <= 5000 then
225 [
226 "2501-5000"
227 "501+"
228 ]
229 else
230 [
231 "5001+"
232 "501+"
233 ];
234 in
235 lib.forEach numbers (number: "10.rebuild-${kernel}: ${number}")
236 ) rebuildCountByKernel
237 );
238}