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