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 Group a list of `packagePlatformAttr`s by platforms
98
99 Turns
100 [
101 { name = "hello"; platform = "aarch64-linux"; ... }
102 { name = "hello"; platform = "x86_64-linux"; ... }
103 { name = "hello"; platform = "aarch64-darwin"; ... }
104 { name = "hello"; platform = "x86_64-darwin"; ... }
105 { name = "bye"; platform = "aarch64-darwin"; ... }
106 { name = "bye"; platform = "x86_64-darwin"; ... }
107 ]
108 into
109 {
110 aarch64-linux = [ "hello" ];
111 x86_64-linux = [ "hello" ];
112 aarch64-darwin = [ "hello" "bye" ];
113 x86_64-darwin = [ "hello" "bye" ];
114 }
115 */
116 groupByPlatform =
117 packagePlatformAttrs:
118 let
119 packagePlatformAttrsByPlatform = builtins.groupBy (p: p.platform) packagePlatformAttrs;
120 extractPackageNames = map (p: p.name);
121 in
122 lib.mapAttrs (_: extractPackageNames) packagePlatformAttrsByPlatform;
123
124 # Turns
125 # [
126 # { name = "hello"; platform = "aarch64-linux"; ... }
127 # { name = "hello"; platform = "x86_64-linux"; ... }
128 # { name = "hello"; platform = "aarch64-darwin"; ... }
129 # { name = "hello"; platform = "x86_64-darwin"; ... }
130 # { name = "bye"; platform = "aarch64-darwin"; ... }
131 # { name = "bye"; platform = "x86_64-darwin"; ... }
132 # ]
133 #
134 # into
135 #
136 # {
137 # linux = [ "hello" ];
138 # darwin = [ "hello" "bye" ];
139 # }
140 groupByKernel =
141 packagePlatformAttrs:
142 let
143 filterKernel =
144 kernel:
145 builtins.attrNames (
146 builtins.groupBy (p: p.name) (
147 builtins.filter (p: lib.hasSuffix kernel p.platform) packagePlatformAttrs
148 )
149 );
150 in
151 lib.genAttrs [ "linux" "darwin" ] filterKernel;
152
153 /*
154 Maps an attrs of `kernel - rebuild counts` mappings to an attrs of labels
155
156 Turns
157 {
158 linux = 56;
159 darwin = 1;
160 }
161 into
162 {
163 "10.rebuild-darwin: 1" = true;
164 "10.rebuild-darwin: 1-10" = true;
165 "10.rebuild-darwin: 11-100" = false;
166 # [...]
167 "10.rebuild-darwin: 1" = false;
168 "10.rebuild-darwin: 1-10" = false;
169 "10.rebuild-linux: 11-100" = true;
170 # [...]
171 }
172 */
173 getLabels =
174 rebuildCountByKernel:
175 lib.mergeAttrsList (
176 lib.mapAttrsToList (
177 kernel: rebuildCount:
178 let
179 range = from: to: from <= rebuildCount && (to == null || rebuildCount <= to);
180 in
181 lib.mapAttrs' (number: lib.nameValuePair "10.rebuild-${kernel}: ${number}") {
182 "0" = range 0 0;
183 "1" = range 1 1;
184 "1-10" = range 1 10;
185 "11-100" = range 11 100;
186 "101-500" = range 101 500;
187 "501-1000" = range 501 1000;
188 "501+" = range 501 null;
189 "1001-2500" = range 1001 2500;
190 "2501-5000" = range 2501 5000;
191 "5001+" = range 5001 null;
192 }
193 ) rebuildCountByKernel
194 );
195}