Skip to content

Commit fe0ca50

Browse files
authored
better pkg config path for devshell + access to variants from project (#1760)
* devshell: better definition of PKG_CONFIG_PATH that manually going through pkg-config of all packages. Also filter out non-derivation from nativeBuildInputs. * Add project.projectVariants that apply flake.variants modules * Move projectOverlays.devhshell to haskellLib.devshellFor
1 parent a59f6fc commit fe0ca50

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

docs/reference/library.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Then feeding its result into [mkCabalProjectPkgSet](#mkcabalprojectpkgset) passi
166166
| `ghcWithHoogle` | Function | [`ghcWithHoogle`](#ghcwithhoogle) |
167167
| `ghcWithPackages` | Function | [`ghcWithPackages`](#ghcwithpackages) |
168168
| `projectCross` | Attrset | Like `pkgs.pkgsCross.<system>` from nixpkgs `p.projectCross.<system>` returns the project results for cross compilation (where system is a member of nixpkgs lib.systems.examples). So `p.projectCross.ghcjs.hsPkgs` is the same as `hsPkgs` but compiled with ghcjs |
169+
| `projectVariants` | Attrset | Attribute set of variant for the project, mapped from `flake.variants` config values |
169170
| `appendModule` | Function | Re-eval the project with an extra module (or module list). |
170171
| `extend` and `appendOverlays` | Function | Modify a project, or add attributes, through overlays: `p.extend(final: prev: { })`. The overlays are carried-over `projectCross` and `appendModule` invocations. |
171172

lib/default.nix

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ in {
343343
projectOverlays = import ./project-overlays.nix {
344344
inherit lib haskellLib;
345345
};
346-
346+
347347
# Use by `prefixFlake` to add a prefix to every attribute
348348
prefixAttrs = prefix: x:
349349
__listToAttrs (map (n:{
@@ -540,4 +540,28 @@ in {
540540
devShells
541541
devShell; # TODO remove devShell once everyone has nix that supports `devShells.default`
542542
};
543+
544+
# Adapt a standard project shell (`project.shell` or `haskell-nix.shellFor`)
545+
# into a devshell module (https://github.com/numtide/devshell)
546+
# that should provide the same environnement.
547+
devshellFor = shell: {
548+
packages = lib.filter lib.isDerivation (shell.nativeBuildInputs
549+
# devshell does not use pkgs.mkShell / pkgs.stdenv.mkDerivation,
550+
# so we need to explicit required dependencies which
551+
# are provided implicitely by stdenv when using the normal shell:
552+
++ shell.stdenv.defaultNativeBuildInputs)
553+
++ [shell.stdenv.cc.bintools];
554+
# We need to expose all the necessary env variables:
555+
env = [
556+
{
557+
name = "PKG_CONFIG_PATH";
558+
value = lib.makeSearchPath "lib/pkgconfig" shell.buildInputs;
559+
}
560+
] ++ lib.mapAttrsToList lib.nameValuePair ({
561+
inherit (shell) NIX_GHC_LIBDIR;
562+
# CABAL_CONFIG is only set if the shell was built with exactDeps=true
563+
} // lib.optionalAttrs (shell ? CABAL_CONFIG) {
564+
inherit (shell) CABAL_CONFIG;
565+
});
566+
};
543567
}

lib/project-overlays.nix

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,11 @@
22
lib
33
, haskellLib
44
}: {
5-
# Provide a devshell profile (https://github.com/numtide/devshell),
6-
# adapted from the project normal shell.
5+
6+
# TODO: remove by end of 2022.
77
devshell = final: prev: {
8-
devshell = let
9-
in {
10-
packages = final.shell.nativeBuildInputs
11-
# Cannot add the whole final.shell.buildInputs list because many collide with each other when fused.
12-
# So we only add what is really used (by pkg-config):
13-
++ map (p: p.dev or p) (lib.concatLists (lib.concatMap (p: p.components.library.pkgconfig or [] ++ p.components.setup.pkgconfig or [] ++ lib.concatMap (c: lib.concatMap (a: a.pkgconfig) (lib.attrValues c)) (lib.attrValues (removeAttrs p.components ["library" "setup"])))
14-
(lib.attrValues final.pkg-set.config.packages)))
15-
# devshell does not use pkgs.mkShell / pkgs.stdenv.mkDerivation,
16-
# so we need to explicit required dependencies which
17-
# are provided implicitely by stdenv when using the normal shell:
18-
++ (lib.filter lib.isDerivation final.shell.stdenv.defaultNativeBuildInputs)
19-
++ lib.optional final.shell.stdenv.targetPlatform.isGnu final.pkgs.buildPackages.binutils;
20-
# We need to expose all the necessary env variables:
21-
env = [
22-
{
23-
name = "PKG_CONFIG_PATH";
24-
# devshell fuse every all `packages` into a single directory ($DEVSHELL_DIR), so we use it:
25-
prefix = "$DEVSHELL_DIR/lib/pkgconfig";
26-
}
27-
] ++ lib.mapAttrsToList lib.nameValuePair ({
28-
inherit (final.shell) NIX_GHC_LIBDIR;
29-
# CABAL_CONFIG is only set if the shell was built with exactDeps=true
30-
} // lib.optionalAttrs (final.shell ? CABAL_CONFIG) {
31-
inherit (final.shell) CABAL_CONFIG;
32-
});
33-
};
8+
devshell = builtins.trace "WARNING: `projectOverlays.devshell` is deprecated in favor of `haskellLib.devshellFor`"
9+
(haskellLib.devshellFor final.shell);
3410
};
3511

3612
# Provides easily accessible attrset for each type of

overlays/haskell.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ final: prev: {
624624
.extend project.__overlay__
625625
) final.pkgsCross) // { recurseForDerivations = false; };
626626

627+
# attribute set of variant (with an extra module applied) for the project,
628+
# mapped from `flake.variants` config values.
629+
projectVariants = final.lib.mapAttrs (_: project.appendModule) project.args.flake.variants;
630+
627631
# re-eval this project with an extra module (or module list).
628632
appendModule = extraProjectModule: (rawProject.projectFunction final.haskell-nix
629633
((if builtins.isList rawProject.projectModule
@@ -763,8 +767,8 @@ final: prev: {
763767
forAllVariants =
764768
forAllCrossCompilers "default" project
765769
++ final.lib.concatLists (final.lib.mapAttrsToList
766-
(name: projectModule: forAllCrossCompilers name (project.appendModule projectModule))
767-
project.args.flake.variants);
770+
(name: projectVariant: forAllCrossCompilers name projectVariant)
771+
project.projectVariants);
768772
in haskellLib.combineFlakes ":" (builtins.foldl' (a: b: a // b) {} forAllVariants);
769773
flake = args: (project.appendModule { flake = args; }).flake';
770774

0 commit comments

Comments
 (0)