Skip to content

Commit 0c1fdfc

Browse files
committed
Move modules for using plan.json install-plan into separate files.
1 parent 1254ba4 commit 0c1fdfc

File tree

5 files changed

+90
-95
lines changed

5 files changed

+90
-95
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{pkgs, lib, config, ...}: {
2+
nonReinstallablePkgs = ["rts" "base" "ghc-prim" "integer-gmp" "integer-simple"]
3+
++ lib.optionals (builtins.compareVersions config.compiler.version "8.11" >= 0) [
4+
"ghc-bignum"]
5+
++ lib.optionals (builtins.compareVersions config.compiler.version "9.9" >= 0) [
6+
"ghc-internal"]
7+
++ lib.optionals (pkgs.stdenv.hostPlatform.isGhcjs) ([
8+
# ghci and its dependencies
9+
"ghci" "binary" "bytestring" "containers" "template-haskell" "array" "deepseq" "filepath" "ghc-boot" "ghc-boot-th" "ghc-heap" "transformers" "unix" "directory" "time" "ghc-platform" "os-string"]
10+
++ lib.optionals (builtins.compareVersions config.compiler.version "8.11" < 0) [
11+
"ghcjs-prim" "ghcjs-th"]);
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Map overrides of the form `packages.${pkg-name}` to all the matching
2+
# packages in the plan.
3+
{pkgs, config, options, ...}: {
4+
use-package-keys = true;
5+
package-keys = map (p: p.pkg-name) config.plan-json.install-plan ++ map (p: p.id) config.plan-json.install-plan;
6+
packages = pkgs.lib.listToAttrs (map (p: {
7+
name = p.id;
8+
value = pkgs.lib.modules.mkAliasDefinitions (options.packages.${p.pkg-name});
9+
}) (pkgs.lib.filter (p: p.id != p.pkg-name) config.plan-json.install-plan));
10+
}

modules/install-plan/redirect.nix

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Add `hsPkgs.${pkg-name}` based on the available targets in the plan.
2+
{pkgs, lib, config, ...}: {
3+
hsPkgs = builtins.removeAttrs (builtins.mapAttrs (packageName: packageTargets:
4+
let
5+
byVersion = builtins.groupBy (x: x.pkg-version) packageTargets;
6+
versions = builtins.attrNames byVersion;
7+
in if builtins.length versions != 1
8+
then let
9+
err = throw "Multiple versions for ${packageName} ${builtins.toJSON versions}";
10+
in {
11+
isRedirect = true;
12+
identifier = { name = packageName; version = err; };
13+
components = err;
14+
checks = err;
15+
}
16+
else let
17+
componentsByName = builtins.listToAttrs (map (x: { name = x.component-name; value = x.available; }) packageTargets);
18+
lookupComponent = collectionName: name: available:
19+
let attrPath =
20+
if collectionName == ""
21+
then "${packageName}.components.library"
22+
else "${packageName}.components.${collectionName}.${name}";
23+
in if builtins.length available != 1
24+
then throw "Multiple avaialble targets for ${attrPath}"
25+
else if builtins.isString (builtins.head available)
26+
then throw "${builtins.head available} looking for ${attrPath}"
27+
else if collectionName == ""
28+
then config.hsPkgs.${(builtins.head available).id}.components.library
29+
else config.hsPkgs.${(builtins.head available).id}.components.${collectionName}.${name};
30+
componentsWithPrefix = collectionName: prefix:
31+
lib.listToAttrs (lib.concatLists (lib.mapAttrsToList (n: available:
32+
lib.optional (lib.hasPrefix "${prefix}:" n && (builtins.length available != 1 || !builtins.elem (builtins.head available) ["TargetNotBuildable" "TargetNotLocal"])) (
33+
let
34+
name = lib.removePrefix "${prefix}:" n;
35+
value = lookupComponent collectionName name available;
36+
in { inherit name value; }
37+
)) componentsByName));
38+
in rec {
39+
isRedirect = true;
40+
identifier = rec { name = packageName; version = builtins.head versions; id = "${name}-${version}"; };
41+
components =
42+
lib.mapAttrs componentsWithPrefix pkgs.haskell-nix.haskellLib.componentPrefix
43+
// lib.optionalAttrs (componentsByName ? lib) {
44+
library = lookupComponent "" "" componentsByName.lib;
45+
};
46+
checks = pkgs.recurseIntoAttrs (builtins.mapAttrs
47+
(_: d: pkgs.haskell-nix.haskellLib.check d)
48+
(lib.filterAttrs (_: d: d.config.doCheck) components.tests));
49+
})
50+
(builtins.groupBy (x: x.pkg-name) config.plan-json.targets)) config.preExistingPkgs;
51+
}

modules/plan.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ in
2929
# combined or replaced. We seed the package Options with an empty set forcing the
3030
# default values.
3131
options = {
32+
plan-json = mkOption {
33+
type = attrsOf unspecified;
34+
};
3235
use-package-keys = mkOption {
3336
type = bool;
3437
default = false;

overlays/haskell.nix

Lines changed: 14 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,14 @@ final: prev: {
655655
final.lib.listToAttrs (
656656
map (dname: { name = dname; value = null; }) (lookupPreExisting (p.depends or p.components.lib.depends)));
657657
}) plan-json.install-plan);
658-
to-key = p: p.id;
659658
lookupDependency = hsPkgs: d:
660659
final.lib.optional (by-id.${d}.type != "pre-existing") (
661660
if by-id.${d}.component-name or "lib" == "lib"
662-
then hsPkgs.${to-key by-id.${d}} or hsPkgs.${by-id.${d}.pkg-name}
663-
else hsPkgs.${to-key by-id.${d}}.components.sublibs.${final.lib.removePrefix "lib:" by-id.${d}.component-name});
661+
then hsPkgs.${d} or hsPkgs.${by-id.${d}.pkg-name}
662+
else hsPkgs.${d}.components.sublibs.${final.lib.removePrefix "lib:" by-id.${d}.component-name});
664663
lookupExeDependency = hsPkgs: d:
665664
# Try to lookup by ID, but if that fails use the name (currently a different plan is used by pkgsBuildBuild when cross compiling)
666-
(hsPkgs.pkgsBuildBuild.${to-key by-id.${d}} or hsPkgs.pkgsBuildBuild.${by-id.${d}.pkg-name}).components.exes.${final.lib.removePrefix "exe:" by-id.${d}.component-name};
665+
(hsPkgs.pkgsBuildBuild.${d} or hsPkgs.pkgsBuildBuild.${by-id.${d}.pkg-name}).components.exes.${final.lib.removePrefix "exe:" by-id.${d}.component-name};
667666
lookupDependencies = hsPkgs: depends: exe-depends: {
668667
depends = final.lib.concatMap (lookupDependency hsPkgs) depends;
669668
pre-existing = lookupPreExisting depends;
@@ -679,6 +678,7 @@ final: prev: {
679678
name = final.lib.removePrefix "${prefix}:" n;
680679
value = (if cabal2nixComponents == null then {} else cabal2nixComponents.${collectionName}.${name}) // {
681680
buildable = true;
681+
planned = final.lib.mkOverride 900 true;
682682
} // lookupDependencies hsPkgs c.depends c.exe-depends;
683683
in { inherit name value; }
684684
)) components));
@@ -687,10 +687,12 @@ final: prev: {
687687
// final.lib.optionalAttrs (components ? lib) {
688688
library = (if cabal2nixComponents == null then {} else cabal2nixComponents.library) // {
689689
buildable = true;
690+
planned = final.lib.mkOverride 900 true;
690691
} // lookupDependencies hsPkgs components.lib.depends components.lib.exe-depends;
691692
} // final.lib.optionalAttrs (components ? setup) {
692693
setup = {
693694
buildable = true;
695+
planned = final.lib.mkOverride 900 true;
694696
} // lookupDependencies hsPkgs.pkgsBuildBuild (components.setup.depends or []) (components.setup.exe-depends or []);
695697
};
696698
callProjectResults = callCabalProjectToNix config;
@@ -706,12 +708,12 @@ final: prev: {
706708
packages = final.lib.listToAttrs (
707709
final.lib.concatMap (p:
708710
final.lib.optional (p.type == "pre-existing") {
709-
name = to-key p;
711+
name = p.id;
710712
value.revision = null;
711713
}) plan-json.install-plan
712714
++ final.lib.concatMap (p:
713715
final.lib.optional (p.type == "configured" && (p.style == "global" || p.style == "inplace") ) {
714-
name = to-key p;
716+
name = p.id;
715717
value.revision =
716718
{hsPkgs, ...}@args:
717719
let cabal2nix = (
@@ -744,7 +746,7 @@ final: prev: {
744746
packages = final.lib.listToAttrs (
745747
final.lib.concatMap (p:
746748
final.lib.optional (p.type == "configured" && p.style == "local") {
747-
name = to-key p;
749+
name = p.id;
748750
value =
749751
{hsPkgs, ...}@args:
750752
let cabal2nix = import (nixFilesDir + "/.plan.nix/${p.pkg-name}.nix") (args // { hsPkgs = {}; });
@@ -776,94 +778,11 @@ final: prev: {
776778
};
777779
}) plan-json.install-plan);
778780
});
779-
modules = [({config, pkgs, ...}: {
780-
nonReinstallablePkgs = ["rts" "base" "ghc-prim" "integer-gmp" "integer-simple"]
781-
++ final.lib.optionals (builtins.compareVersions config.compiler.version "8.11" >= 0) [
782-
"ghc-bignum"]
783-
++ final.lib.optionals (builtins.compareVersions config.compiler.version "9.9" >= 0) [
784-
"ghc-internal"]
785-
++ final.lib.optionals (pkgs.stdenv.hostPlatform.isGhcjs) ([
786-
# ghci and its dependencies
787-
"ghci" "binary" "bytestring" "containers" "template-haskell" "array" "deepseq" "filepath" "ghc-boot" "ghc-boot-th" "ghc-heap" "transformers" "unix" "directory" "time" "ghc-platform" "os-string"]
788-
++ final.lib.optionals (builtins.compareVersions config.compiler.version "8.11" < 0) [
789-
"ghcjs-prim" "ghcjs-th"]);
790-
})
791-
({config, options, ...}: {
792-
use-package-keys = true;
793-
package-keys = map (p: p.pkg-name) plan-json.install-plan ++ map (p: to-key p) plan-json.install-plan;
794-
packages = final.lib.listToAttrs (map (p: {
795-
name = to-key p;
796-
value = final.lib.modules.mkAliasDefinitions (options.packages.${p.pkg-name});
797-
}) (final.lib.filter (p: to-key p != p.pkg-name) plan-json.install-plan));
798-
})
799-
({lib, ...}: {
800-
packages = final.lib.listToAttrs (map (p:
801-
let components =
802-
if p ? component-name
803-
then { ${p.component-name} = { inherit (p) depends exe-depends; }; }
804-
else p.components or {};
805-
in {
806-
name = to-key p;
807-
value.components = final.lib.mapAttrs (type: x:
808-
if type == "library" || type == "setup"
809-
then { planned = lib.mkOverride 900 true; }
810-
else
811-
final.lib.mapAttrs (_: _: {
812-
planned = lib.mkOverride 900 true;
813-
}) x
814-
) (getComponents null {} p);
815-
}) plan-json.install-plan);
816-
})
817-
({config, ...}: {
818-
hsPkgs = builtins.removeAttrs (builtins.mapAttrs (packageName: packageTargets:
819-
let
820-
byVersion = builtins.groupBy (x: x.pkg-version) packageTargets;
821-
versions = builtins.attrNames byVersion;
822-
in if builtins.length versions != 1
823-
then let
824-
err = throw "Multiple versions for ${packageName} ${builtins.toJSON versions}";
825-
in {
826-
isRedirect = true;
827-
identifier = { name = packageName; version = err; };
828-
components = err;
829-
checks = err;
830-
}
831-
else let
832-
componentsByName = builtins.listToAttrs (map (x: { name = x.component-name; value = x.available; }) packageTargets);
833-
lookupComponent = collectionName: name: available:
834-
let attrPath =
835-
if collectionName == ""
836-
then "${packageName}.components.library"
837-
else "${packageName}.components.${collectionName}.${name}";
838-
in if builtins.length available != 1
839-
then throw "Multiple avaialble targets for ${attrPath}"
840-
else if builtins.isString (builtins.head available)
841-
then throw "${builtins.head available} looking for ${attrPath}"
842-
else if collectionName == ""
843-
then config.hsPkgs.${(builtins.head available).id}.components.library
844-
else config.hsPkgs.${(builtins.head available).id}.components.${collectionName}.${name};
845-
componentsWithPrefix = collectionName: prefix:
846-
final.lib.listToAttrs (final.lib.concatLists (final.lib.mapAttrsToList (n: available:
847-
final.lib.optional (final.lib.hasPrefix "${prefix}:" n && (builtins.length available != 1 || !builtins.elem (builtins.head available) ["TargetNotBuildable" "TargetNotLocal"])) (
848-
let
849-
name = final.lib.removePrefix "${prefix}:" n;
850-
value = lookupComponent collectionName name available;
851-
in { inherit name value; }
852-
)) componentsByName));
853-
in rec {
854-
isRedirect = true;
855-
identifier = rec { name = packageName; version = builtins.head versions; id = "${name}-${version}"; };
856-
components =
857-
final.lib.mapAttrs componentsWithPrefix haskellLib.componentPrefix
858-
// final.lib.optionalAttrs (componentsByName ? lib) {
859-
library = lookupComponent "" "" componentsByName.lib;
860-
};
861-
checks = final.recurseIntoAttrs (builtins.mapAttrs
862-
(_: d: haskellLib.check d)
863-
(final.lib.filterAttrs (_: d: d.config.doCheck) components.tests));
864-
})
865-
(builtins.groupBy (x: x.pkg-name) plan-json.targets)) config.preExistingPkgs;
866-
})
781+
modules = [
782+
{ inherit plan-json; }
783+
(import ../modules/install-plan/non-reinstallable.nix)
784+
(import ../modules/install-plan/override-package-by-name.nix)
785+
(import ../modules/install-plan/redirect.nix)
867786
];
868787
};
869788
buildProject = if final.stdenv.hostPlatform != final.stdenv.buildPlatform

0 commit comments

Comments
 (0)