@@ -642,10 +642,14 @@ final: prev: {
642
642
inherit ( config ) compiler-nix-name compilerSelection evalPackages ;
643
643
selectedCompiler = ( compilerSelection final . buildPackages ) . ${ compiler-nix-name } ;
644
644
645
+ # Read the plan.json file `plan-nix` derivation
645
646
plan-json = builtins . fromJSON (
646
647
builtins . unsafeDiscardStringContext (
647
648
builtins . readFile ( callProjectResults . projectNix + "/plan.json" ) ) ) ;
649
+ # All the units in the plan indexed by unit ID.
648
650
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)
649
653
lookupPreExisting = depends :
650
654
final . lib . concatMap ( d : builtins . attrNames pre-existing-depends . ${ d } ) depends ;
651
655
pre-existing-depends =
@@ -655,22 +659,28 @@ final: prev: {
655
659
final . lib . listToAttrs (
656
660
map ( dname : { name = dname ; value = null ; } ) ( lookupPreExisting ( p . depends or p . components . lib . depends ) ) ) ;
657
661
} ) plan-json . install-plan ) ;
662
+ # Lookup a dependency in `hsPkgs`
658
663
lookupDependency = hsPkgs : d :
659
664
final . lib . optional ( by-id . ${ d } . type != "pre-existing" ) (
660
665
if by-id . ${ d } . component-name or "lib" == "lib"
661
666
then hsPkgs . ${ d } or hsPkgs . ${ by-id . ${ d } . pkg-name }
662
667
else hsPkgs . ${ d } . components . sublibs . ${ final . lib . removePrefix "lib:" by-id . ${ d } . component-name } ) ;
668
+ # Lookup an executable dependency in `hsPkgs.pkgsBuildBuild`
663
669
lookupExeDependency = hsPkgs : d :
664
670
# Try to lookup by ID, but if that fails use the name (currently a different plan is used by pkgsBuildBuild when cross compiling)
665
671
( 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`
666
673
lookupDependencies = hsPkgs : depends : exe-depends : {
667
674
depends = final . lib . concatMap ( lookupDependency hsPkgs ) depends ;
668
675
pre-existing = lookupPreExisting depends ;
669
676
build-tools = map ( lookupExeDependency hsPkgs ) exe-depends ;
670
677
} ;
678
+ # Calculate the packages for a component
671
679
getComponents = cabal2nixComponents : hsPkgs : p :
672
680
let
673
681
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.
674
684
componentsWithPrefix = collectionName : prefix :
675
685
final . lib . listToAttrs ( final . lib . concatLists ( final . lib . mapAttrsToList ( n : c :
676
686
final . lib . optional ( final . lib . hasPrefix "${ prefix } :" n ) (
@@ -696,49 +706,62 @@ final: prev: {
696
706
nixFilesDir = callProjectResults . projectNix + callProjectResults . src . origSubDir or "" ;
697
707
plan-pkgs = if ! builtins . pathExists ( callProjectResults . projectNix + "/plan.json" )
698
708
then
709
+ # If there is no `plan.json` file assume this is a materialized
710
+ # `plan-nix` and use the old code path.
699
711
# TODO remove this once all the materialized files are updated
700
712
importAndFilterProject {
701
713
inherit ( callProjectResults ) projectNix sourceRepos src ;
702
714
}
703
715
else {
716
+ # This replaces the `plan-nix/default.nix`
704
717
pkgs = ( hackage : {
705
718
packages = final . lib . listToAttrs (
719
+ # Include entries for the `pre-existing` packages, but leave them as `null`
706
720
final . lib . concatMap ( p :
707
721
final . lib . optional ( p . type == "pre-existing" ) {
708
722
name = p . id ;
709
723
value . revision = null ;
710
724
} ) plan-json . install-plan
725
+ # The other packages that are not part of the project itself.
711
726
++ final . lib . concatMap ( p :
712
727
final . lib . optional ( p . type == "configured" && ( p . style == "global" || p . style == "inplace" ) ) {
713
728
name = p . id ;
714
729
value . revision =
715
730
{ 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 = { } ; } ) ;
722
740
in final . lib . optionalAttrs ( p ? pkg-src-sha256 ) {
723
741
sha256 = p . pkg-src-sha256 ;
724
742
} // 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.
725
745
src = final . lib . lists . elemAt callProjectResults . sourceRepos ( final . lib . strings . toInt p . pkg-src . source-repo . location ) + "/${ p . pkg-src . source-repo . subdir } " ;
726
746
} // 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).
727
749
inherit ( cabal2nix ) package-description-override ;
728
750
} // {
729
- flags = p . flags ;
751
+ flags = p . flags ; # Use the flags from `plan.json`
730
752
components = getComponents cabal2nix . components hsPkgs p ;
731
753
package = cabal2nix . package // {
732
754
identifier = { name = p . pkg-name ; version = p . pkg-version ; id = p . id ; } ;
733
755
isProject = false ;
734
- setup-depends = [ ] ;
756
+ setup-depends = [ ] ; # The correct setup depends will be in `components.setup.depends`
735
757
} ;
736
758
} ;
737
759
} ) plan-json . install-plan ) ;
738
760
compiler = {
739
761
inherit ( selectedCompiler ) version ;
740
762
} ;
741
763
} ) ;
764
+ # Packages in the project (those that are both configure and local)
742
765
extras = ( _hackage : {
743
766
packages = final . lib . listToAttrs (
744
767
final . lib . concatMap ( p :
@@ -752,9 +775,11 @@ final: prev: {
752
775
} // final . lib . optionalAttrs ( p . pkg-src . type or "" == "local" && cabal2nix ? cabal-generator ) {
753
776
inherit ( cabal2nix ) cabal-generator ;
754
777
} // final . lib . optionalAttrs ( p . pkg-src . type or "" == "local" ) {
778
+ # Find the `src` location based on `p.pkg-src.path`
755
779
src = if final . lib . hasPrefix "/" p . pkg-src . path
756
- then p . pkg-src . path
780
+ then p . pkg-src . path # Absolute path
757
781
else haskellLib . appendSubDir {
782
+ # Relative to the project path
758
783
inherit ( callProjectResults ) src ;
759
784
subDir = final . lib . removePrefix "./" ( final . lib . removePrefix "/" ( final . lib . removeSuffix "/." ( final . lib . removeSuffix "/." (
760
785
if final . lib . hasPrefix ".${ callProjectResults . src . origSubDir or "" } /" ( p . pkg-src . path + "/" )
@@ -765,12 +790,12 @@ final: prev: {
765
790
# reference project directories not in the package subDir.
766
791
} ;
767
792
} // {
768
- flags = p . flags ;
793
+ flags = p . flags ; # Use the flags from `plan.json`
769
794
components = getComponents cabal2nix . components hsPkgs p ;
770
795
package = cabal2nix . package // {
771
796
identifier = { name = p . pkg-name ; version = p . pkg-version ; id = p . id ; } ;
772
797
isProject = true ;
773
- setup-depends = [ ] ;
798
+ setup-depends = [ ] ; # The correct setup depends will be in `components.setup.depends`
774
799
} ;
775
800
} ;
776
801
} ) plan-json . install-plan ) ;
0 commit comments