-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
225 lines (197 loc) · 8 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{
pkgs ? import <nixpkgs> {},
hasktagsOptions ? "--ctags --follow-symlinks",
suffixes ? ["hs" "lhs" "hsc"],
compiler ? "ghc8104",
hasktagsCompiler ? compiler,
hasktagsPackage ? null,
}:
let
inherit (pkgs.lib) take drop;
htGhc = pkgs.haskell.packages.${hasktagsCompiler}.override {
overrides = self: super: {};
};
hasktags =
if hasktagsPackage == null
then htGhc.hasktags
else hasktagsPackage;
# Create a string from the suffixes.
concatSuffixes =
sep: f:
builtins.concatStringsSep sep (map f suffixes);
# Format rsync filter rules for haskell files with the desired `suffixes`.
suffixesFilter = concatSuffixes "\n" (s: "+ *.${s}");
rsyncFilterGhc = ''
- compiler/
- utils/
- Cabal/
'';
testsFilter = ''
- test/
- tests/
'';
# rsync filter rule file that leaves only haskell library sources, selected by the global parameter `suffixes`.
rsyncFilter = target: ghc: pkgs.writeText "tags-rsync-filter" ''
- examples/
- benchmarks/
${if target then "" else testsFilter}
${if ghc then rsyncFilterGhc else ""}
- Setup.hs
+ */
${suffixesFilter}
- *
'';
# hasktags takes a haskell list for the --suffixes option for some reason.
suffixesOption =
let sxs = toString (concatSuffixes ", " (s: ''".${s}"''));
in "'[${sxs}]'";
# Takes a haskell derivation and produces another derivation that reuses the `src` attribute to create tags.
# The sources are unpacked and filtered so only non-test etc. haskell files (selected by the global parameter
# `suffixes`) are present.
# The remaining files are processed by `hasktags` and stored in the file `tags`.
# The sources need to remain in the derivation's output for editors to find the tags' locations.
# If `hasktags` fails, we don't want want the whole process to be unusable just because there is some weird code in
# one of the dependencies, so we just print an error message and output an empty tag file.
# If the flag `relative` is true, the package is treated as being in `cwd`. When developing a project, it wouldn't be
# very ergonomical to have the tags pointing to the store, so we use relative paths in the tag file.
packageTags = { relative ? false, tagsPrefix ? "", isGhc ? false, target ? false, name, src, ... }:
let
absoluteOption = if relative then "" else "--tags-absolute";
options = "${hasktagsOptions} ${absoluteOption}";
hasktagsCmd = "${hasktags}/bin/hasktags ${options} --suffixes ${suffixesOption} --output $out/tags .";
# hasktags sometimes produces lines with only an identifier
garbageFilter = "^\\S*$";
in
pkgs.stdenv.mkDerivation {
name = "${name}-tags";
inherit src;
buildInputs = [hasktags pkgs.rsync];
phases = ["unpackPhase" "buildPhase"];
buildPhase = ''
fail() {
echo "tags failed for ${name}"
rm -f $out/tags
touch $out/tags
}
package=$out/package/${tagsPrefix}
mkdir -p $package
rsync --recursive --prune-empty-dirs --filter='. ${rsyncFilter target isGhc}' . $package/
cat > $out/hasktags-cmd <<'EOF'
${hasktagsCmd}
EOF
cd $out/package
${hasktagsCmd} &> $out/hasktagsLog || fail
sed -i '/${garbageFilter}/d' $out/tags
'';
};
# Takes a list of haskell derivations and produces a list of tag derivations.
packageTagss =
{ relative ? true, targets }:
map (p: packageTags ({ inherit relative; target = true; } // p)) targets;
# Obtain the dependencies of a haskell derivation.
inputs = p: p.getBuildInputs.haskellBuildInputs;
# Call a function propagating seen elements and merge its results with the accumulator.
accumulateSeen = f: z: a:
let
sub = f z.seen a;
in
{ inherit (sub) seen; result = z.result ++ sub.result; };
# Call a function propagating seen elements for each element in the list and accumulate all results.
foldSeen = f: seen: as:
builtins.foldl' (accumulateSeen f) { inherit seen; result = []; } as;
# recursion delegate for `subTree`.
depTree = seen: package:
foldSeen subTree seen (inputs package);
srcSeen = package:
builtins.any (s: s.src == package.src);
# Takes a list of package names and a package and produces a list of tag derivations.
# The `seen` list is used to skip packages that have been processed before, since packages may occur multiple times
# in a dependency tree.
# Folds over the dependencies, accumulating the `seen` list.
subTree = seen: package:
let
this = packageTags package;
deps = depTree ([this] ++ seen) package;
result = [this] ++ deps.result;
in
if srcSeen package seen
then { inherit seen; result = []; }
else { seen = [package] ++ deps.seen; inherit result; };
# Takes a list of haskell derivations and creates tags for them and all of their dependencies.
# Returns a list of tag file derivations.
packageTrees = { targets, relative ? true, base ? true }:
let
targetTags = packageTagss { inherit targets relative; };
subTags = foldSeen depTree targets targets;
baseTags = if base then [(packageTags pkgs.haskell.compiler.${compiler} // { isGhc = true; })] else [];
in
targetTags ++ subTags.result ++ baseTags;
# Takes a list of haskell derivations and produces a list of tag derivations for only the dependencies of the
# arguments.
depTagss =
foldSeen depTree [];
header = pkgs.writeText "tags-header" ''
!_TAG_FILE_FORMAT 2
!_TAG_FILE_SORTED 1
!_TAG_PROGRAM_NAME hasktags
!_TAG_STORE_PATH '';
# cat all package tags into one file.
# Removes individual files' headers and adds a global one.
merge = packages:
let
tags = map (p: "${p}/tags") packages;
in
pkgs.stdenv.mkDerivation {
name = "project-tags";
phases = ["buildPhase"];
buildPhase = ''
mkdir -p $out
cat ${header} > $out/tags
echo $out >> $out/tags
sort --unique ${toString tags} | grep -v '^!_TAG' >> $out/tags
echo "${builtins.concatStringsSep "\n" tags}" > $out/parts
'';
};
# cat all package tags into one file, splitting the argument list so that the shell's argument list length limit
# isn't triggered.
safeMerge = packages:
let
limit = 1024;
mergePart = ps:
if (builtins.length ps > limit)
then [(merge (take limit ps))] ++ (mergePart (drop limit ps))
else if (builtins.length ps == 1)
then ps
else [(merge ps)];
merged = mergePart packages;
in
if (builtins.length merged == 1)
then builtins.head merged
else merge (mergePart packages);
in rec {
inherit safeMerge packageTags hasktags;
# Produce lists of single-package tag file derivations.
# Each derivation's output contains a directory named `package` for the sources and a file named `tags`.
individual = {
# Tag only dependencies of the derivation argument.
deps = { targets }: (depTagss targets).result;
# Tag only the derivation arguments' sources.
# `relative` determines whether the targets should be tagged with relative paths.
packages = args@{ targets, relative ? true }: packageTagss args;
# Tag the arguments' sources and their dependencies.
# `relative` determines whether the targets should be tagged with relative paths.
all = args@{ targets, relative ? true, base ? true }: packageTrees args;
};
# Produces a derivation of a combined tag file.
# The output of the derivation contains one file named `tags`.
combined = {
# Tag only dependencies of the derivation argument.
deps = { targets }: safeMerge (individual.deps { inherit targets; });
# Tag only the derivation arguments' sources.
# `relative` determines whether the targets should be tagged with relative paths.
packages = args@{ targets, relative ? true }: safeMerge (individual.packages args);
# Tag the arguments' sources and their dependencies.
# `relative` determines whether the targets should be tagged with relative paths.
all = args@{ targets, relative ? true, base ? true }: safeMerge (individual.all args);
};
}