Skip to content

Commit 5b1e10c

Browse files
authored
Add component-identifier to components metadata (input-output-hk#1993)
* Add component-identifier to components metadata * Use component-id in mkFlakeApps and mkFlakePackages * Lint
1 parent 50e7b67 commit 5b1e10c

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

builder/comp-builder.nix

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ let
171171
) ++ [ "$(cat $configFiles/configure-flags)"
172172
] ++ commonConfigureFlags);
173173

174-
commonConfigureFlags = ([
174+
commonConfigureFlags = [
175175
# GHC
176176
"--with-ghc=${ghc.targetPrefix}ghc"
177177
"--with-ghc-pkg=${ghc.targetPrefix}ghc-pkg"
@@ -208,7 +208,7 @@ let
208208
++ lib.optional (enableLibraryProfiling || enableProfiling) "--profiling-detail=${profilingDetail}"
209209
++ lib.optional stdenv.hostPlatform.isLinux (enableFeature enableDeadCodeElimination "split-sections")
210210
++ lib.optionals haskellLib.isCrossHost (
211-
map (arg: "--hsc2hs-option=" + arg) (["--cross-compile"] ++ lib.optionals (stdenv.hostPlatform.isWindows) ["--via-asm"])
211+
map (arg: "--hsc2hs-option=" + arg) (["--cross-compile"] ++ lib.optionals stdenv.hostPlatform.isWindows ["--via-asm"])
212212
++ lib.optional (package.buildType == "Configure") "--configure-option=--host=${
213213
# Older ghcjs patched config.sub to support "js-unknown-ghcjs" (not "javascript-unknown-ghcjs")
214214
if stdenv.hostPlatform.isGhcjs && builtins.compareVersions defaults.ghc.version "9" < 0
@@ -222,23 +222,22 @@ let
222222
++ lib.optionals useLLVM [
223223
"--ghc-option=-fPIC" "--gcc-option=-fPIC"
224224
]
225-
++ map (o: ''--ghc${lib.optionalString (stdenv.hostPlatform.isGhcjs) "js"}-options="${o}"'') ghcOptions
225+
++ map (o: ''--ghc${lib.optionalString stdenv.hostPlatform.isGhcjs "js"}-options="${o}"'') ghcOptions
226226
++ lib.optional (
227227
# GHC 9.2 cross compiler built with older versions of GHC seem to have problems
228228
# with unique conters. Perhaps because the name changed for the counters.
229229
# TODO This work around to use `-j1` should be removed once we are able to build 9.2 with 9.2.
230230
haskellLib.isCrossHost
231231
&& builtins.compareVersions defaults.ghc.version "9.2.1" >= 0
232232
&& builtins.compareVersions defaults.ghc.version "9.3" < 0)
233-
"--ghc-options=-j1"
234-
);
233+
"--ghc-options=-j1";
235234

236235
# the build-tools version might be depending on the version of the package, similarly to patches
237236
executableToolDepends =
238237
(lib.concatMap (c: if c.isHaskell or false
239238
then builtins.attrValues (c.components.exes or {})
240239
else [c])
241-
(builtins.filter (x: !(isNull x)
240+
(builtins.filter (x: x != null
242241
# We always exclude hsc2hs from build-tools because it is unecessary as it is provided by ghc
243242
# and hsc2hs from ghc is first in PATH so the one from build-tools is never used.
244243
&& x.identifier.name or "" != "hsc2hs")
@@ -347,7 +346,11 @@ let
347346
inherit dontPatchELF dontStrip;
348347

349348
passthru = {
350-
inherit (package) identifier;
349+
identifier = package.identifier // {
350+
component-id = "${package.identifier.name}:${componentId.ctype}:${componentId.cname}";
351+
component-name = componentId.cname;
352+
component-type = componentId.ctype;
353+
};
351354
config = component;
352355
srcSubDir = cleanSrc.subDir;
353356
srcSubDirPath = cleanSrc.root + cleanSrc.subDir;
@@ -393,7 +396,7 @@ let
393396
# These only need to be propagated for library components (otherwise they
394397
# will be in `buildInputs`)
395398
++ lib.optionals (haskellLib.isLibrary componentId) configFiles.libDeps # libDeps is already deduplicated
396-
++ lib.optionals (stdenv.hostPlatform.isWindows)
399+
++ lib.optionals stdenv.hostPlatform.isWindows
397400
(haskellLib.uniqueWithName (lib.flatten component.libs)));
398401

399402
buildInputs = haskellLib.checkUnique "${ghc.targetPrefix}${fullName} buildInputs" (
@@ -414,7 +417,7 @@ let
414417

415418
prePatch =
416419
# emcc is very slow if it cannot cache stuff in $HOME
417-
(lib.optionalString (stdenv.hostPlatform.isGhcjs) ''
420+
(lib.optionalString stdenv.hostPlatform.isGhcjs ''
418421
export HOME=$(mktemp -d)
419422
'') +
420423
(lib.optionalString (!canCleanSource) ''

lib/default.nix

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ in {
3535
];
3636

3737
foldrAttrVals = f: z: attrs:
38-
lib.foldr (g: acc: g acc) z (lib.mapAttrsToList (_name: f) attrs);
38+
lib.foldr f z (builtins.attrValues attrs);
3939

4040
foldComponents = tys: f: z: conf:
4141
let
@@ -91,18 +91,17 @@ in {
9191
isExe componentId
9292
|| isTest componentId
9393
|| isBenchmark componentId;
94-
mayHaveExecutable = componentId:
95-
isExecutableType componentId;
94+
mayHaveExecutable = isExecutableType;
9695

9796
# Was there a reference to the package source in the `cabal.project` or `stack.yaml` file.
9897
# This is used to make the default `packages` list for `shellFor`.
9998
isLocalPackage = p: p.isLocal or false;
100-
selectLocalPackages = ps: lib.filterAttrs (n: p: p != null && isLocalPackage p) ps;
99+
selectLocalPackages = lib.filterAttrs (n: p: p != null && isLocalPackage p);
101100

102101
# if it's a project package it has a src attribute set with an origSubDir attribute.
103102
# project packages are a subset of localPackages
104103
isProjectPackage = p: p.isProject or false;
105-
selectProjectPackages = ps: lib.filterAttrs (n: p: p != null && isLocalPackage p && isProjectPackage p) ps;
104+
selectProjectPackages = lib.filterAttrs (n: p: p != null && isLocalPackage p && isProjectPackage p);
106105

107106
# Format a componentId as it should appear as a target on the
108107
# command line of the setup script.
@@ -133,7 +132,7 @@ in {
133132
## flatLibDepends :: Component -> [Package]
134133
flatLibDepends = component:
135134
let
136-
makePairs = map (p: rec { key=val.name; val=(p.components.library or p); });
135+
makePairs = map (p: rec { key=val.name; val=p.components.library or p; });
137136
closure = builtins.genericClosure {
138137
startSet = makePairs component.depends;
139138
operator = {val,...}: makePairs val.config.depends;
@@ -427,35 +426,24 @@ in {
427426
# Flake package names that are flat and match the cabal component names.
428427
mkFlakePackages = haskellPackages: builtins.listToAttrs (
429428
lib.concatLists (lib.mapAttrsToList (packageName: package:
430-
lib.optional (package.components ? library)
431-
{ name = "${packageName}:lib:${packageName}"; value = package.components.library; }
432-
++ lib.mapAttrsToList (n: v:
433-
{ name = "${packageName}:lib:${n}"; value = v; })
434-
(package.components.sublibs)
435-
++ lib.mapAttrsToList (n: v:
436-
{ name = "${packageName}:exe:${n}"; value = v; })
437-
(package.components.exes)
438-
++ lib.mapAttrsToList (n: v:
439-
{ name = "${packageName}:test:${n}"; value = v; })
440-
(package.components.tests)
441-
++ lib.mapAttrsToList (n: v:
442-
{ name = "${packageName}:bench:${n}"; value = v; })
443-
(package.components.benchmarks)
444-
) haskellPackages));
429+
builtins.groupBy
430+
(c: c.passthru.identifier.component-id)
431+
((lib.optional (package.components ? library) package.components.library)
432+
++ package.components.sublibs
433+
++ package.components.exes
434+
++ package.components.tests
435+
++ package.components.benchmarks)
436+
) haskellPackages));
445437

446438
# Flake package names that are flat and match the cabal component names.
447439
mkFlakeApps = haskellPackages: builtins.listToAttrs (
448440
lib.concatLists (lib.mapAttrsToList (packageName: package:
449-
lib.mapAttrsToList (n: v:
450-
{ name = "${packageName}:exe:${n}"; value = { type = "app"; program = v.exePath; }; })
451-
(package.components.exes)
452-
++ lib.mapAttrsToList (n: v:
453-
{ name = "${packageName}:test:${n}"; value = { type = "app"; program = v.exePath; }; })
454-
(package.components.tests)
455-
++ lib.mapAttrsToList (n: v:
456-
{ name = "${packageName}:benchmark:${n}"; value = { type = "app"; program = v.exePath; }; })
457-
(package.components.benchmarks)
458-
) haskellPackages));
441+
builtins.groupBy
442+
(c: c.passthru.identifier.component-id)
443+
(package.components.exes
444+
++ package.components.tests
445+
++ package.components.benchmarks)
446+
) haskellPackages));
459447

460448
# Flatten the result of collectChecks or collectChecks' for use in flake `checks`
461449
mkFlakeChecks = allChecks: builtins.listToAttrs (
@@ -464,7 +452,7 @@ in {
464452
lib.optionals (lib.isAttrs checks) (
465453
lib.mapAttrsToList (n: v:
466454
{ name = "${packageName}:test:${n}"; value = v; })
467-
(lib.filterAttrs (_: v: lib.isDerivation v) checks))
455+
(lib.filterAttrs (_: lib.isDerivation) checks))
468456
) allChecks));
469457

470458
removeRecurseForDerivations = x:
@@ -494,11 +482,11 @@ in {
494482
}
495483
# Build the plan-nix and check it if materialized
496484
// lib.optionalAttrs (checkedProject ? plan-nix) {
497-
plan-nix = checkedProject.plan-nix;
485+
inherit (checkedProject) plan-nix;
498486
}
499487
# Build the stack-nix and check it if materialized
500488
// lib.optionalAttrs (checkedProject ? stack-nix) {
501-
stack-nix = checkedProject.stack-nix;
489+
inherit (checkedProject) stack-nix;
502490
};
503491

504492
mkFlake = project: {

0 commit comments

Comments
 (0)