Skip to content

Add missing deps to devshell overlay, remove cabalWrapper #1549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 19 additions & 97 deletions lib/project-overlays.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,35 @@
lib
, haskellLib
}: {
# Provide a devshell profile (https://github.com/numtide/devshell),
# adapted from the project normal shell.
devshell = final: prev: {
devshell = let
cabal-install = final.pkgs.buildPackages.haskell-nix.cabal-install.${final.args.compiler-nix-name};
inherit (final.pkgs.buildPackages) runCommand runtimeShell;
cabalWrapper = runCommand "cabal" { inherit (cabal-install) meta; } ''
mkdir -p $out/bin
cat << EOF > $out/bin/cabal
#!${runtimeShell}
set -euo pipefail

find_up() {
while [[ \$PWD != / ]] ; do
if [[ -e "\$1" ]]; then
echo "\$PWD"
return
fi
cd ..
done
}

toplevel=\$(find_up "cabal.project")

if [[ -n "\$toplevel" ]]; then
cabal_project="\$toplevel/cabal.project"
nix_cabal_project=\$toplevel/.nix-cabal.project
extra_cabal_opts=("--project-file=\$nix_cabal_project")
awk '
# Add comment with explanation of file
BEGIN {
print "-- Generated from '"\$cabal_project"' by the wrapper script"
print "-- ${placeholder "out"}/cabal"
print "-- Add this file to your .gitignore\n"
}

# Matches all section starts
/^[^ ]/ {
# Remember the section name (they can span multiple lines)
section = \$0
}
# Matches every line
// {
# Only print the line if it is not in the section we want to omit
if (section != "source-repository-package")
print \$0
}
' "\$cabal_project" > "\$nix_cabal_project"
else
extra_cabal_opts=()
fi

cabal=${placeholder "out"}/bin/.cabal
>&2 echo "\$cabal \''${extra_cabal_opts[@]} \$@"
exec "\$cabal" "\''${extra_cabal_opts[@]}" "\$@"
EOF
cp -rn ${cabal-install}/* $out/
cp ${cabal-install}/bin/cabal $out/bin/.cabal
chmod +x $out/bin/*
'';
in {
packages = final.shell.nativeBuildInputs;
packages = final.shell.nativeBuildInputs
# devshell does not use pkgs.mkShell / pkgs.stdenv.mkDerivation,
# so we need to explicit required dependencies which
# are provided implicitely when using the normal shell:
++ (with final.pkgs; [ binutils git gcc glibc gawk gnugrep bash coreutils pkg-config ]);
env = lib.mapAttrsToList lib.nameValuePair {
inherit (final.shell) CABAL_CONFIG NIX_GHC_LIBDIR;
};
commands = [
{
package = cabalWrapper;
category = "development";
}
];
};
};

projectComponents = final: prev: let
# Provides easily accessible attrset for each type of
# components belonging to the project packages.
projectComponents = final: prev: {
# local project packages:
packages = haskellLib.selectProjectPackages final.hsPkgs;
# used to materialize `packages-exes.nix` (project packages and exes list) to avoid some evaluations:
packagesExes = lib.genAttrs
(lib.attrNames final.packages)
(name: lib.attrNames final.hsPkgs.${name}.components.exes);
collectExes = packagesExes: lib.listToAttrs (lib.concatLists (lib.mapAttrsToList
(p: map (exe: lib.nameValuePair exe final.hsPkgs.${p}.components.exes.${exe})) packagesExes));
in {
# local project packages:
packages = haskellLib.selectProjectPackages final.hsPkgs;
# set of all exes (as first level entries):
exes = collectExes packagesExes;
# set of all exes (as first level entries), mapped from a materialized set of packages and exes set (generated via `generatePackagesExes`):
exesFrom = packagesExesMat: collectExes (import packagesExesMat);
# `tests` are the test suites which have been built.
tests = haskellLib.collectComponents' "tests" final.packages;
# `benchmarks` (only built, not run).
benchmarks = haskellLib.collectComponents' "benchmarks" final.packages;
# `checks` collect results of executing the tests:
checks = haskellLib.collectChecks' final.packages;

generatePackagesExesMat = let
nixFile = builtins.toFile "packages-exes.nix" (lib.generators.toPretty {} packagesExes);
in final.pkgs.buildPackages.writeShellScript "generate-packages-exes" ''
set -euo pipefail

TARGET="$1"
TARGET_DIR="$(dirname "$TARGET")"

mkdir -p "$TARGET_DIR"
rm -rf "$TARGET"
cp -r ${nixFile} "$TARGET"
chmod -R +w "$TARGET"
'';
# set of all exes (as first level entries):
exes = lib.foldl' lib.mergeAttrs { } (map (p: p.components.exes) (lib.attrValues final.packages));
# `tests` are the test suites which have been built.
tests = haskellLib.collectComponents' "tests" final.packages;
# `benchmarks` (only built, not run).
benchmarks = haskellLib.collectComponents' "benchmarks" final.packages;
# `checks` collect results of executing the tests:
checks = haskellLib.collectChecks' final.packages;
};

}