Skip to content

Commit

Permalink
Add component-identifier to components metadata (#1993)
Browse files Browse the repository at this point in the history
* Add component-identifier to components metadata

* Use component-id in mkFlakeApps and mkFlakePackages

* Lint
  • Loading branch information
andreabedini authored Jul 13, 2023
1 parent 50e7b67 commit 5b1e10c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
21 changes: 12 additions & 9 deletions builder/comp-builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ let
) ++ [ "$(cat $configFiles/configure-flags)"
] ++ commonConfigureFlags);

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

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

passthru = {
inherit (package) identifier;
identifier = package.identifier // {
component-id = "${package.identifier.name}:${componentId.ctype}:${componentId.cname}";
component-name = componentId.cname;
component-type = componentId.ctype;
};
config = component;
srcSubDir = cleanSrc.subDir;
srcSubDirPath = cleanSrc.root + cleanSrc.subDir;
Expand Down Expand Up @@ -393,7 +396,7 @@ let
# These only need to be propagated for library components (otherwise they
# will be in `buildInputs`)
++ lib.optionals (haskellLib.isLibrary componentId) configFiles.libDeps # libDeps is already deduplicated
++ lib.optionals (stdenv.hostPlatform.isWindows)
++ lib.optionals stdenv.hostPlatform.isWindows
(haskellLib.uniqueWithName (lib.flatten component.libs)));

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

prePatch =
# emcc is very slow if it cannot cache stuff in $HOME
(lib.optionalString (stdenv.hostPlatform.isGhcjs) ''
(lib.optionalString stdenv.hostPlatform.isGhcjs ''
export HOME=$(mktemp -d)
'') +
(lib.optionalString (!canCleanSource) ''
Expand Down
56 changes: 22 additions & 34 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ in {
];

foldrAttrVals = f: z: attrs:
lib.foldr (g: acc: g acc) z (lib.mapAttrsToList (_name: f) attrs);
lib.foldr f z (builtins.attrValues attrs);

foldComponents = tys: f: z: conf:
let
Expand Down Expand Up @@ -91,18 +91,17 @@ in {
isExe componentId
|| isTest componentId
|| isBenchmark componentId;
mayHaveExecutable = componentId:
isExecutableType componentId;
mayHaveExecutable = isExecutableType;

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

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

# Format a componentId as it should appear as a target on the
# command line of the setup script.
Expand Down Expand Up @@ -133,7 +132,7 @@ in {
## flatLibDepends :: Component -> [Package]
flatLibDepends = component:
let
makePairs = map (p: rec { key=val.name; val=(p.components.library or p); });
makePairs = map (p: rec { key=val.name; val=p.components.library or p; });
closure = builtins.genericClosure {
startSet = makePairs component.depends;
operator = {val,...}: makePairs val.config.depends;
Expand Down Expand Up @@ -427,35 +426,24 @@ in {
# Flake package names that are flat and match the cabal component names.
mkFlakePackages = haskellPackages: builtins.listToAttrs (
lib.concatLists (lib.mapAttrsToList (packageName: package:
lib.optional (package.components ? library)
{ name = "${packageName}:lib:${packageName}"; value = package.components.library; }
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:lib:${n}"; value = v; })
(package.components.sublibs)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:exe:${n}"; value = v; })
(package.components.exes)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = v; })
(package.components.tests)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:bench:${n}"; value = v; })
(package.components.benchmarks)
) haskellPackages));
builtins.groupBy
(c: c.passthru.identifier.component-id)
((lib.optional (package.components ? library) package.components.library)
++ package.components.sublibs
++ package.components.exes
++ package.components.tests
++ package.components.benchmarks)
) haskellPackages));

# Flake package names that are flat and match the cabal component names.
mkFlakeApps = haskellPackages: builtins.listToAttrs (
lib.concatLists (lib.mapAttrsToList (packageName: package:
lib.mapAttrsToList (n: v:
{ name = "${packageName}:exe:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.exes)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:test:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.tests)
++ lib.mapAttrsToList (n: v:
{ name = "${packageName}:benchmark:${n}"; value = { type = "app"; program = v.exePath; }; })
(package.components.benchmarks)
) haskellPackages));
builtins.groupBy
(c: c.passthru.identifier.component-id)
(package.components.exes
++ package.components.tests
++ package.components.benchmarks)
) haskellPackages));

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

removeRecurseForDerivations = x:
Expand Down Expand Up @@ -494,11 +482,11 @@ in {
}
# Build the plan-nix and check it if materialized
// lib.optionalAttrs (checkedProject ? plan-nix) {
plan-nix = checkedProject.plan-nix;
inherit (checkedProject) plan-nix;
}
# Build the stack-nix and check it if materialized
// lib.optionalAttrs (checkedProject ? stack-nix) {
stack-nix = checkedProject.stack-nix;
inherit (checkedProject) stack-nix;
};

mkFlake = project: {
Expand Down

0 comments on commit 5b1e10c

Please sign in to comment.