Skip to content

Commit 7b3b449

Browse files
committed
Add comments
1 parent ffd40a3 commit 7b3b449

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

overlays/haskell.nix

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,14 @@ final: prev: {
642642
inherit (config) compiler-nix-name compilerSelection evalPackages;
643643
selectedCompiler = (compilerSelection final.buildPackages).${compiler-nix-name};
644644

645+
# Read the plan.json file `plan-nix` derivation
645646
plan-json = builtins.fromJSON (
646647
builtins.unsafeDiscardStringContext (
647648
builtins.readFile (callProjectResults.projectNix + "/plan.json")));
649+
# All the units in the plan indexed by unit ID.
648650
by-id = final.lib.listToAttrs (map (x: { name = x.id; value = x; }) plan-json.install-plan);
651+
# Find the names of all the pre-existing packages used by a list of dependencies
652+
# (includes transitive dependencies)
649653
lookupPreExisting = depends:
650654
final.lib.concatMap (d: builtins.attrNames pre-existing-depends.${d}) depends;
651655
pre-existing-depends =
@@ -655,22 +659,28 @@ final: prev: {
655659
final.lib.listToAttrs (
656660
map (dname: { name = dname; value = null; }) (lookupPreExisting (p.depends or p.components.lib.depends)));
657661
}) plan-json.install-plan);
662+
# Lookup a dependency in `hsPkgs`
658663
lookupDependency = hsPkgs: d:
659664
final.lib.optional (by-id.${d}.type != "pre-existing") (
660665
if by-id.${d}.component-name or "lib" == "lib"
661666
then hsPkgs.${d} or hsPkgs.${by-id.${d}.pkg-name}
662667
else hsPkgs.${d}.components.sublibs.${final.lib.removePrefix "lib:" by-id.${d}.component-name});
668+
# Lookup an executable dependency in `hsPkgs.pkgsBuildBuild`
663669
lookupExeDependency = hsPkgs: d:
664670
# Try to lookup by ID, but if that fails use the name (currently a different plan is used by pkgsBuildBuild when cross compiling)
665671
(hsPkgs.pkgsBuildBuild.${d} or hsPkgs.pkgsBuildBuild.${by-id.${d}.pkg-name}).components.exes.${final.lib.removePrefix "exe:" by-id.${d}.component-name};
672+
# Populate `depends`, `pre-existing` and `build-tools`
666673
lookupDependencies = hsPkgs: depends: exe-depends: {
667674
depends = final.lib.concatMap (lookupDependency hsPkgs) depends;
668675
pre-existing = lookupPreExisting depends;
669676
build-tools = map (lookupExeDependency hsPkgs) exe-depends;
670677
};
678+
# Calculate the packages for a component
671679
getComponents = cabal2nixComponents: hsPkgs: p:
672680
let
673681
components = p.components or { ${p.component-name or "lib"} = { inherit (p) depends; exe-depends = p.exe-depends or []; }; };
682+
# Other than the `lib` and `setup` components, component names
683+
# have a prefix based on their type.
674684
componentsWithPrefix = collectionName: prefix:
675685
final.lib.listToAttrs (final.lib.concatLists (final.lib.mapAttrsToList (n: c:
676686
final.lib.optional (final.lib.hasPrefix "${prefix}:" n) (
@@ -696,49 +706,62 @@ final: prev: {
696706
nixFilesDir = callProjectResults.projectNix + callProjectResults.src.origSubDir or "";
697707
plan-pkgs = if !builtins.pathExists (callProjectResults.projectNix + "/plan.json")
698708
then
709+
# If there is no `plan.json` file assume this is a materialized
710+
# `plan-nix` and use the old code path.
699711
# TODO remove this once all the materialized files are updated
700712
importAndFilterProject {
701713
inherit (callProjectResults) projectNix sourceRepos src;
702714
}
703715
else {
716+
# This replaces the `plan-nix/default.nix`
704717
pkgs = (hackage: {
705718
packages = final.lib.listToAttrs (
719+
# Include entries for the `pre-existing` packages, but leave them as `null`
706720
final.lib.concatMap (p:
707721
final.lib.optional (p.type == "pre-existing") {
708722
name = p.id;
709723
value.revision = null;
710724
}) plan-json.install-plan
725+
# The other packages that are not part of the project itself.
711726
++ final.lib.concatMap (p:
712727
final.lib.optional (p.type == "configured" && (p.style == "global" || p.style == "inplace") ) {
713728
name = p.id;
714729
value.revision =
715730
{hsPkgs, ...}@args:
716-
let cabal2nix = (
717-
if builtins.pathExists (nixFilesDir + "/cabal-files/${p.pkg-name}.nix")
718-
then import (nixFilesDir + "/cabal-files/${p.pkg-name}.nix")
719-
else if builtins.pathExists (nixFilesDir + "/.plan.nix/${p.pkg-name}.nix")
720-
then import (nixFilesDir + "/.plan.nix/${p.pkg-name}.nix")
721-
else (((hackage.${p.pkg-name}).${p.pkg-version}).revisions).default) (args // { hsPkgs = {}; });
731+
let
732+
# Read the output of `Cabal2Nix.hs`. We need it for information not
733+
# in the `plan.json` file.
734+
cabal2nix = (
735+
if builtins.pathExists (nixFilesDir + "/cabal-files/${p.pkg-name}.nix")
736+
then import (nixFilesDir + "/cabal-files/${p.pkg-name}.nix")
737+
else if builtins.pathExists (nixFilesDir + "/.plan.nix/${p.pkg-name}.nix")
738+
then import (nixFilesDir + "/.plan.nix/${p.pkg-name}.nix")
739+
else (((hackage.${p.pkg-name}).${p.pkg-version}).revisions).default) (args // { hsPkgs = {}; });
722740
in final.lib.optionalAttrs (p ? pkg-src-sha256) {
723741
sha256 = p.pkg-src-sha256;
724742
} // final.lib.optionalAttrs (p.pkg-src.type or "" == "source-repo") {
743+
# Replace the source repository packages with versions created when
744+
# parsing the `cabal.project` file.
725745
src = final.lib.lists.elemAt callProjectResults.sourceRepos (final.lib.strings.toInt p.pkg-src.source-repo.location) + "/${p.pkg-src.source-repo.subdir}";
726746
} // final.lib.optionalAttrs (cabal2nix ? package-description-override && p.pkg-version == cabal2nix.package.identifier.version) {
747+
# Use the `.cabal` file from the `Cabal2Nix` if it for the matching
748+
# version of the package (the one in the plan).
727749
inherit (cabal2nix) package-description-override;
728750
} // {
729-
flags = p.flags;
751+
flags = p.flags; # Use the flags from `plan.json`
730752
components = getComponents cabal2nix.components hsPkgs p;
731753
package = cabal2nix.package // {
732754
identifier = { name = p.pkg-name; version = p.pkg-version; id = p.id; };
733755
isProject = false;
734-
setup-depends = [];
756+
setup-depends = []; # The correct setup depends will be in `components.setup.depends`
735757
};
736758
};
737759
}) plan-json.install-plan);
738760
compiler = {
739761
inherit (selectedCompiler) version;
740762
};
741763
});
764+
# Packages in the project (those that are both configure and local)
742765
extras = (_hackage: {
743766
packages = final.lib.listToAttrs (
744767
final.lib.concatMap (p:
@@ -752,9 +775,11 @@ final: prev: {
752775
} // final.lib.optionalAttrs (p.pkg-src.type or "" == "local" && cabal2nix ? cabal-generator) {
753776
inherit (cabal2nix) cabal-generator;
754777
} // final.lib.optionalAttrs (p.pkg-src.type or "" == "local") {
778+
# Find the `src` location based on `p.pkg-src.path`
755779
src = if final.lib.hasPrefix "/" p.pkg-src.path
756-
then p.pkg-src.path
780+
then p.pkg-src.path # Absolute path
757781
else haskellLib.appendSubDir {
782+
# Relative to the project path
758783
inherit (callProjectResults) src;
759784
subDir = final.lib.removePrefix "./" (final.lib.removePrefix "/" (final.lib.removeSuffix "/." (final.lib.removeSuffix "/." (
760785
if final.lib.hasPrefix ".${callProjectResults.src.origSubDir or ""}/" (p.pkg-src.path + "/")
@@ -765,12 +790,12 @@ final: prev: {
765790
# reference project directories not in the package subDir.
766791
};
767792
} // {
768-
flags = p.flags;
793+
flags = p.flags; # Use the flags from `plan.json`
769794
components = getComponents cabal2nix.components hsPkgs p;
770795
package = cabal2nix.package // {
771796
identifier = { name = p.pkg-name; version = p.pkg-version; id = p.id; };
772797
isProject = true;
773-
setup-depends = [];
798+
setup-depends = []; # The correct setup depends will be in `components.setup.depends`
774799
};
775800
};
776801
}) plan-json.install-plan);

0 commit comments

Comments
 (0)