-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
default.nix
270 lines (215 loc) · 9.29 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
let
# TODO: Remove when https://github.com/NixOS/cabal2nix/pull/360 is merged and available
cabal2nix-fix-overlay = final: previous:
with final.haskell.lib; {
haskellPackages = previous.haskellPackages.override (old: {
overrides = final.lib.composeExtensions (old.overrides or (_: _: {})) (
self: super: {
cabal2nix = overrideCabal super.cabal2nix (old: {
src = pkgs.fetchFromGitHub {
owner = "nh2";
repo = "cabal2nix";
rev = "5721bed2a598a018119413bfe868bd286735cb15";
sha256 = "1436ri6nlfcgd263byb596dcx6g4l9fx47hm11vfh34x849r2kcy";
};
});
}
);
});
};
normalPkgs = import <nixpkgs> {};
pkgs = (import <nixpkgs> {
config.allowUnfree = true;
config.allowBroken = true;
# config.permittedInsecurePackages = [
# "webkitgtk-2.4.11"
# ];
overlays = [ cabal2nix-fix-overlay ];
}).pkgsMusl;
in
{ compiler ? "ghc843" }:
let
normalHaskellPackages = pkgs.haskellPackages;
lib = pkgs.lib;
# Function that tells us if a given Haskell package has an executable.
isExecutable = pkg:
(pkgs.haskell.lib.overrideCabal pkg (drv: {
passthru.isExecutable = drv.isExecutable or false;
})).isExecutable;
# Function that tells us if a given Haskell package is marked as broken.
isBroken = pkg:
(pkgs.haskell.lib.overrideCabal pkg (drv: {
passthru.broken = drv.broken or false;
})).broken;
# Function that for a given Haskell package tells us if any of
# its dependencies is marked as `broken`.
hasBrokenDeps = pkg:
let
libraryDepends =
(pkgs.haskell.lib.overrideCabal pkg (drv: {
passthru.libraryHaskellDepends = drv.libraryHaskellDepends or [];
})).libraryHaskellDepends;
in
lib.any (x:
let
res = builtins.tryEval (lib.isDerivation x && x ? env && isBroken x);
broken = res.success && res.value;
in
if broken
then builtins.trace "broken because of broken deps: ${pkg}" broken
else broken
) libraryDepends;
# Nixpkgs contains both Hackage and Stackage packages.
# We want to build only executables that are on Stackage because
# we know that those should build.
# Find all Stackage package names here so we can use them
# as a filter.
# Done by parsing the configuration file that contains
# which packages come from Stackage.
stackagePackages =
let
stackageInfoPath = <nixpkgs/pkgs/development/haskell-modules/configuration-hackage2nix.yaml>;
pythonWithYaml = pkgs.python2Packages.python.withPackages (pkgs: [pkgs.pyyaml]);
dont-distribute-packages-file = normalPkgs.runCommand "test" {} ''
${pythonWithYaml}/bin/python -c 'import yaml, json; x = yaml.load(open("${stackageInfoPath}")); print(json.dumps([line.split(" ")[0] for line in x["default-package-overrides"]]))' > $out
'';
dont-distribute-packages = builtins.fromJSON (builtins.readFile dont-distribute-packages-file);
in
dont-distribute-packages;
# Turns a list into a "set" (map where all keys are {}).
keySet = list: builtins.listToAttrs (map (name: lib.nameValuePair name {}) list);
# Making it a set for faster lookup
stackagePackagesSet = keySet stackagePackages;
isStackagePackage = name: builtins.hasAttr name stackagePackagesSet;
# Stackage package names we want to blacklist.
blacklist = [
];
# All Stackage executables who (and whose dependencies) are not marked
# as broken in nixpkgs.
stackageExecutables = lib.filterAttrs (name: x: isStackagePackage name && !(lib.elem name blacklist) && (
let
res = builtins.tryEval (
lib.isDerivation x
&& x ? env
&& isExecutable x
&& !(isBroken x)
&& !(hasBrokenDeps x)
);
in
res.success && res.value)
) normalHaskellPackages;
# Making it a set for faster lookup
stackageExecutablesSet = keySet (builtins.attrNames stackageExecutables);
isStackageExecutable = name: builtins.hasAttr name stackageExecutablesSet;
numStackageExecutables = lib.length (builtins.attrNames stackageExecutables);
# Just for debugging / statistics:
# Same thing with "-traced" suffix to nicely print
# which executables we're going to build.
stackageExecutablesNames = builtins.attrNames stackageExecutables;
stackageExecutables-traced =
builtins.trace
("selected stackage executables:\n"
+ lib.concatStringsSep "\n" stackageExecutablesNames
+ "\n---\n${toString (lib.length stackageExecutablesNames)} executables total"
)
stackageExecutables;
# Cherry-picking cabal fixes
# TODO Remove this when these fixes are available in nixpkgs:
# https://github.com/haskell/cabal/pull/5356 (-L flag deduplication)
# https://github.com/haskell/cabal/pull/5446 (--enable-executable-static)
# TODO do this via patches instead
cabal_patched_src = pkgs.fetchFromGitHub {
owner = "nh2";
repo = "cabal";
rev = "748f07b50724f2618798d200894f387020afc300";
sha256 = "1k559m291f6spip50rly5z9rbxhfgzxvaz64cx4jqpxgfhbh2gfs";
};
Cabal_patched_Cabal_subdir = pkgs.stdenv.mkDerivation {
name = "cabal-dedupe-src";
buildCommand = ''
cp -rv ${cabal_patched_src}/Cabal/ $out
'';
};
Cabal_patched = normalHaskellPackages.callCabal2nix "Cabal" Cabal_patched_Cabal_subdir {};
useFixedCabal = drv: pkgs.haskell.lib.overrideCabal drv (old: {
setupHaskellDepends = (if old ? setupHaskellDepends then old.setupHaskellDepends else []) ++ [ Cabal_patched ];
# TODO Check if this is necessary
libraryHaskellDepends = (if old ? libraryHaskellDepends then old.libraryHaskellDepends else []) ++ [ Cabal_patched ];
});
# Overriding system libraries that don't provide static libs
# (`.a` files) by default
sqlite_static = pkgs.sqlite.overrideAttrs (old: { dontDisableStatic = true; });
lzma_static = pkgs.lzma.overrideAttrs (old: { dontDisableStatic = true; });
# Overriding `haskellPackages` to fix *libraries* so that
# they can be used in statically linked binaries.
haskellPackagesWithLibsReadyForStaticLinking = with pkgs.haskell.lib; normalHaskellPackages.override (old: {
overrides = pkgs.lib.composeExtensions (old.overrides or (_: _: {})) (self: super: {
# Helpers for other packages
hpc-coveralls = appendPatch super.hpc-coveralls (builtins.fetchurl https://github.com/guillaume-nargeot/hpc-coveralls/pull/73/commits/344217f513b7adfb9037f73026f5d928be98d07f.patch);
persistent-sqlite = super.persistent-sqlite.override { sqlite = sqlite_static; };
lzma = super.lzma.override { lzma = lzma_static; };
# If we `useFixedCabal` on stack, we also need to use the
# it on hpack and hackage-security because otherwise
# stack depends on 2 different versions of Cabal.
hpack = useFixedCabal super.hpack;
hackage-security = useFixedCabal super.hackage-security;
# See https://github.com/hslua/hslua/issues/67
# It's not clear if it's safe to disable this as key functionality may be broken
hslua = dontCheck super.hslua;
});
});
statify = drv: with pkgs.haskell.lib; pkgs.lib.foldl appendConfigureFlag (disableLibraryProfiling (disableSharedExecutables (useFixedCabal drv))) [
# "--ghc-option=-fPIC"
"--enable-executable-static" # requires `useFixedCabal`
"--extra-lib-dirs=${pkgs.gmp6.override { withStatic = true; }}/lib"
# TODO These probably shouldn't be here but only for packages that actually need them
"--extra-lib-dirs=${pkgs.zlib.static}/lib"
"--extra-lib-dirs=${pkgs.ncurses.override { enableStatic = true; }}/lib"
];
# Package set where all "final" executables are statically linked.
#
# In this package set, if executable E depends on package LE
# which provides both a library and executables, then
# E is statically linked but the executables of LE are not.
#
# Of course we could also make a different package set instead,
# where executables from E and LE are all statically linked.
# Then we would not need to distinguish between
# `haskellPackages` and `haskellPackagesWithLibsReadyForStaticLinking`.
# But we don't do that in order to cause as little needed rebuilding
# of libraries vs cache.nixos.org as possible.
haskellPackages =
lib.mapAttrs (name: value:
if isExecutable value then statify value else value
) haskellPackagesWithLibsReadyForStaticLinking;
in
rec {
working = {
inherit (haskellPackages)
hello
stack
hlint
ShellCheck
cabal-install
bench
dhall
cachix
;
};
notWorking = {
inherit (haskellPackages)
xmonad
pandoc
;
};
all = working // notWorking;
# Tries to build all executables on Stackage.
allStackageExecutables =
lib.filterAttrs (name: x: isStackageExecutable name) haskellPackages;
inherit normalPkgs;
inherit normalHaskellPackages;
inherit haskellPackagesWithLibsReadyForStaticLinking;
inherit haskellPackages;
}
# TODO Update README to depend on nixpkgs master in use (instead of nh2's fork), and write something that picks nh2's patches I use on top here
# TODO Instead of picking https://github.com/NixOS/nixpkgs/pull/43713, use a Python script to dedupe `-L` flags from the NIX_*LDFLAGS variables