-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.nix
254 lines (233 loc) · 7.21 KB
/
project.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
{ pkgs, haskellPackages, }:
let
pname = "nix-binary-cache";
binaryLibrary = if builtins.getEnv "USE_CEREAL" != ""
then "cereal" else "binary";
version = "0.0.1";
# Haskell packages the library depends on (in addition to above). We
# use names here because for some reason some of these are null in
# the haskell package set, but still work as dependencies...
dependencies = [
"aeson"
"attoparsec"
"base"
binaryLibrary
"bytestring"
"bytestring-conversion"
"base64-bytestring"
"classy-prelude"
"directory"
"filepath"
"http-client"
"http-client-openssl"
"http-client-tls"
"http-media"
"http-types"
"lifted-async"
"lifted-base"
"lzma"
"lucid"
"mtl"
"parsec"
"pcre-heavy"
"process"
"process-extras"
"servant-client"
"servant-lucid"
"servant-server"
"servant"
"sqlite-simple"
"text"
"transformers"
"unordered-containers"
"vector"
"wai"
"wai-extra"
"warp"
"zlib"
];
# Haskell packages the tests depend on (in addition to above).
testDependencies = [
"QuickCheck"
"hspec"
"microtimer"
"random-strings"
];
# Names of extensions that the library uses.
extensions = [
"ConstraintKinds"
"CPP"
"DataKinds"
"DeriveGeneric"
"FlexibleContexts"
"FlexibleInstances"
"GADTs"
"GeneralizedNewtypeDeriving"
"LambdaCase"
"MultiParamTypeClasses"
"NoImplicitPrelude"
"OverloadedStrings"
"QuasiQuotes"
"RecordWildCards"
"ScopedTypeVariables"
"TypeFamilies"
"TypeOperators"
"TypeSynonymInstances"
"ViewPatterns"
];
# Derivations needed to use in the nix shell.
shellRequires = with pkgs; [
curl
nix.out
less
nmap
silver-searcher
which
];
# Given a list of strings, look all of them up in the haskell package set.
toHaskellPkgs = map (pname: haskellPackages."${pname}");
inherit (builtins) compareVersions;
inherit (pkgs.lib) filter concatStringsSep isDerivation optional;
joinLines = builtins.concatStringsSep "\n";
joinCommas = builtins.concatStringsSep ", ";
joinSpaces = builtins.concatStringsSep " ";
# Options for ghc when both testing and building the library.
ghc-options = [
# Warn on everything, including tabs.
"-Wall" "-fwarn-tabs"
# Don't warn on unused do-binding.
"-fno-warn-unused-do-bind"
# Don't warn on name shadowing. This is why lexical scoping exists...
"-fno-warn-name-shadowing"
# Enable threading.
"-threaded" "-rtsopts" "-with-rtsopts=-N"
];
# Options for ghc when just building the library.
ghc-build-options = ghc-options ++ [
# Enable optimization
"-O3"
# Turn warnings into errors.
# "-Werror"
];
# Options for ghc when just testing.
ghc-test-options = ghc-options ++ [
"-fno-warn-orphans"
];
# Inspect the servant derivation to see if it's an old version; if
# so define a cpp flag.
cpp-options = optional
(compareVersions haskellPackages.servant.version "0.7" < 0)
"-DOLD_SERVANT" ++
optional (binaryLibrary == "cereal") "-DUSE_CEREAL";
# .ghci file text.
dotGhci = pkgs.writeText "${pname}.ghci" (joinLines (
map (ext: ":set -X${ext}") extensions ++
[
":set prompt \"λ> \""
"import Data.Text (Text)"
"import qualified Servant"
"import qualified Data.Text as T"
"import qualified Data.Text.Encoding as T"
"import qualified Data.HashMap.Strict as H"
"import ClassyPrelude"
"import Control.Concurrent.Async.Lifted"
""
]
));
# Cabal file text.
cabalFile = pkgs.writeText "${pname}.cabal" ''
-- This cabal file is generated by a nix expression (see project.nix).
-- It is not meant to be modified by hand.
name: ${pname}
version: ${version}
license: MIT
license-file: LICENSE
author: Allen Nelson
maintainer: [email protected]
build-type: Simple
cabal-version: >=1.10
data-files: sql/tables.sql
-- Define the executable
executable nix-client
main-is: Nix/Cache/Client/Main.hs
build-depends: ${joinCommas dependencies}
hs-source-dirs: src
default-language: Haskell2010
default-extensions: ${joinCommas extensions}
ghc-options: -O3 ${joinSpaces ghc-build-options}
cpp-options: ${joinSpaces cpp-options}
${if false then "" else ''
executable ref-cache
main-is: Nix/ReferenceCache.hs
build-depends: ${joinCommas dependencies}
hs-source-dirs: src
default-language: Haskell2010
default-extensions: ${joinCommas extensions}
ghc-options: -O3 ${joinSpaces ghc-build-options}
cpp-options: ${joinSpaces cpp-options}
''}
${if true then "" else ''
executable nix-server
main-is: Server.hs
build-depends: ${joinCommas dependencies}
hs-source-dirs: src
default-language: Haskell2010
default-extensions: ${joinCommas extensions}
ghc-options: -O3 ${joinSpaces ghc-build-options}
''}
-- Define a unit test suite
test-suite unit-tests
type: exitcode-stdio-1.0
hs-source-dirs: src, tests
main-is: Unit.hs
build-depends: ${joinCommas (dependencies ++ testDependencies)}
ghc-options: ${joinSpaces ghc-test-options}
cpp-options: -DUNIT_TESTS ${joinSpaces cpp-options}
default-language: Haskell2010
default-extensions: ${joinCommas extensions}
'';
in
haskellPackages.mkDerivation rec {
inherit pname version;
src = let
inherit (builtins) filterSource all match;
# It'd be nice to make this a whitelist, but filterSource is kind
# of terrible.
blacklist = map (r: "^${r}$") [
"${pname}\\.cabal" "init_db\\.sh" ".*\\.nix" "dist"
"\\.git" "#.*" "\\.#.*" ".*~" "\\.ghci" "\\.gitignore"
];
check = path: _:
all (regex: match regex (baseNameOf path) == null) blacklist;
in filterSource check ./.;
isExecutable = true;
buildTools = [haskellPackages.cabal-install];
testHaskellDepends = toHaskellPkgs testDependencies;
testDepends = shellRequires;
checkPhase = ''
export HOME=$TMPDIR USER=$(whoami)
dist/build/unit-tests/unit-tests
'';
libraryHaskellDepends = toHaskellPkgs dependencies;
executableHaskellDepends = toHaskellPkgs dependencies;
preConfigure = ''
cp -f ${cabalFile} ${pname}.cabal
'';
shellHook = ''
export CURL_CA_BUNDLE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
# Make sure we're in the project directory, and do initialization.
if [[ -e project.nix ]] && grep -q ${pname} project.nix; then
PROJECT_DIR=$PWD
# Alias for entering REPL for unit tests.
alias testr='(cd $PROJECT_DIR && cabal repl unit-tests)'
# Define a function which uses ghci to run unit tests.
runtests() ( cd $PROJECT_DIR && echo ':main' | cabal repl unit-tests; )
cp -f ${dotGhci} .ghci
eval "${preConfigure}"
cabal clean
cabal configure --enable-tests
fi
'';
description = "A web server";
license = pkgs.lib.licenses.unfree;
}