1# Install not only the Hoogle library and executable, but also a local Hoogle
2# database which provides "Source" links to all specified 'packages' -- or the
3# current Haskell Platform if no custom package set is provided.
4
5{
6 lib,
7 stdenv,
8 buildPackages,
9 haskellPackages,
10 writeText,
11 runCommand,
12}:
13
14# This argument is a function which selects a list of Haskell packages from any
15# passed Haskell package set.
16#
17# Example:
18# (hpkgs: [ hpkgs.mtl hpkgs.lens ])
19selectPackages:
20
21let
22 inherit (haskellPackages) ghc hoogle;
23 packages = selectPackages haskellPackages;
24
25 wrapper = ./hoogle-local-wrapper.sh;
26 isGhcjs = ghc.isGhcjs or false;
27 opts = lib.optionalString;
28 haddockExe = if !isGhcjs then "haddock" else "haddock-ghcjs";
29 ghcDocLibDir = if !isGhcjs then ghc.doc + "/share/doc/ghc*/html/libraries" else ghc + "/doc/lib";
30 # On GHCJS, use a stripped down version of GHC's prologue.txt
31 prologue =
32 if !isGhcjs then
33 "${ghcDocLibDir}/prologue.txt"
34 else
35 writeText "ghcjs-prologue.txt" ''
36 This index includes documentation for many Haskell modules.
37 '';
38
39 docPackages =
40 lib.closePropagation
41 # we grab the doc outputs
42 (map (lib.getOutput "doc") packages);
43
44 # Hoogle database path, relative to `$out`.
45 databasePath = "share/doc/hoogle/default.hoo";
46
47in
48buildPackages.stdenv.mkDerivation (finalAttrs: {
49 name = "hoogle-with-packages";
50 buildInputs = [
51 ghc
52 hoogle
53 ];
54
55 # compiling databases takes less time than copying the results
56 # between machines.
57 preferLocalBuild = true;
58
59 # we still allow substitutes because a database is relatively small and if it
60 # is already built downloading is probably faster. The substitution will only
61 # trigger for users who have already cached the database on a substituter and
62 # thus probably intend to substitute it.
63 allowSubstitutes = true;
64
65 inherit docPackages;
66
67 passAsFile = [ "buildCommand" ];
68
69 buildCommand = ''
70 ${
71 let
72 # Filter out nulls here to work around https://github.com/NixOS/nixpkgs/issues/82245
73 # If we don't then grabbing `p.name` here will fail.
74 packages' = lib.filter (p: p != null) packages;
75 in
76 lib.optionalString (packages' != [ ] -> docPackages == [ ]) (
77 "echo WARNING: localHoogle package list empty, even though"
78 + " the following were specified: "
79 + lib.concatMapStringsSep ", " (p: p.name) packages'
80 )
81 }
82 mkdir -p $out/share/doc/hoogle
83
84 echo importing builtin packages
85 for docdir in ${ghcDocLibDir}"/"*; do
86 name="$(basename $docdir)"
87 ${opts isGhcjs ''docdir="$docdir/html"''}
88 if [[ -d $docdir ]]; then
89 ln -sfn $docdir $out/share/doc/hoogle/$name
90 fi
91 done
92
93 echo importing other packages
94 ${lib.concatMapStringsSep "\n"
95 (el: ''
96 ln -sfn ${el.haddockDir} "$out/share/doc/hoogle/${el.name}"
97 '')
98 (
99 lib.filter (el: el.haddockDir != null) (
100 builtins.map (p: {
101 haddockDir = if p ? haddockDir then p.haddockDir p else null;
102 name = p.pname;
103 }) docPackages
104 )
105 )
106 }
107
108 databasePath="$out/"${lib.escapeShellArg databasePath}
109
110 echo building hoogle database
111 hoogle generate --database "$databasePath" --local=$out/share/doc/hoogle
112
113 echo building haddock index
114 # adapted from GHC's gen_contents_index
115 cd $out/share/doc/hoogle
116
117 args=
118 for hdfile in $(ls -1 *"/"*.haddock | grep -v '/ghc\.haddock' | sort); do
119 name_version=`echo "$hdfile" | sed 's#/.*##'`
120 args="$args --read-interface=$name_version,$hdfile"
121 done
122
123 ${ghc}/bin/${haddockExe} --gen-index --gen-contents -o . \
124 -t "Haskell Hierarchical Libraries" \
125 -p ${prologue} \
126 $args
127
128 echo finishing up
129 mkdir -p $out/bin
130 substitute ${wrapper} $out/bin/hoogle \
131 --subst-var-by shell ${stdenv.shell} \
132 --subst-var-by database "$databasePath" \
133 --subst-var-by hoogle ${hoogle}
134 chmod +x $out/bin/hoogle
135 '';
136
137 passthru = {
138 isHaskellLibrary = false; # for the filter in ./with-packages-wrapper.nix
139
140 # The path to the Hoogle database.
141 database = "${finalAttrs.finalPackage}/${databasePath}";
142
143 tests.can-search-database = runCommand "can-search-database" { } ''
144 # This succeeds even if no results are found, but `Prelude.map` should
145 # always be available.
146 ${finalAttrs.finalPackage}/bin/hoogle search Prelude.map > $out
147 '';
148 };
149
150 meta = {
151 description = "Local Hoogle database";
152 platforms = ghc.meta.platforms;
153 hydraPlatforms = with lib.platforms; none;
154 maintainers = with lib.maintainers; [ ttuegel ];
155 };
156})