forked from IntersectMBO/cardano-db-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
388 lines (350 loc) · 15 KB
/
flake.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
{
description = "cardano-db-sync";
inputs = {
nixpkgs.follows = "haskellNix/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
haskellNix = {
url = "github:input-output-hk/haskell.nix";
inputs.hackage.follows = "hackageNix";
};
hackageNix = {
url = "github:input-output-hk/hackage.nix";
flake = false;
};
iohkNix = {
url = "github:input-output-hk/iohk-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = {
url = "github:input-output-hk/flake-compat/fixes";
flake = false;
};
CHaP = {
url = "github:IntersectMBO/cardano-haskell-packages?ref=repo";
flake = false;
};
};
outputs = { self, ... }@inputs:
let
supportedSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
in
inputs.utils.lib.eachSystem supportedSystems (system:
let
nixpkgs = import inputs.nixpkgs {
inherit system;
inherit (inputs.haskellNix) config;
overlays =
builtins.attrValues inputs.iohkNix.overlays ++
[ inputs.haskellNix.overlay
(final: prev: with final; {
# Required in order to build postgresql for musl
postgresql = (final.postgresql_11
.overrideAttrs (_: lib.optionalAttrs (stdenv.hostPlatform.isMusl) {
dontDisableStatic = true;
NIX_LDFLAGS = "--push-state --as-needed -lstdc++ --pop-state";
# without this collate.icu.utf8, and foreign_data will fail.
LC_CTYPE = "C";
}))
.override {
enableSystemd = stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isMusl;
gssSupport = stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isMusl;
};
})
(final: prev: {
inherit (project.hsPkgs.cardano-node.components.exes) cardano-node;
inherit (project.hsPkgs.cardano-cli.components.exes) cardano-cli;
})
(final: prev: {
# The cardano-db-sync NixOS module (nix/nixos/cardano-db-sync-service.nix)
# expects these to be here
inherit (project.exes)
cardano-db-sync
cardano-db-tool
cardano-smash-server;
schema = ./schema;
})
(final: prev: {
# HLint 3.2.x requires GHC >= 8.10 && < 9.0
hlint = final.haskell-nix.tool "ghc8107" "hlint" {
version = "3.2.7";
};
# Fourmolu 0.10.x requires GHC >= 9.0 && < 9.6
fourmolu = final.haskell-nix.tool "ghc928" "fourmolu" {
version = "0.10.1.0";
};
# Weeder 2.2.0 requires GHC >= 8.10 && < 9.0
weeder = final.haskell-nix.tool "ghc8107" "weeder" {
version = "2.2.0";
};
})
];
};
# Set up and start Postgres before running database tests
preCheck = ''
echo pre-check
initdb --encoding=UTF8 --locale=en_US.UTF-8 --username=postgres $NIX_BUILD_TOP/db-dir
postgres -D $NIX_BUILD_TOP/db-dir -c listen_addresses="" -k $TMP &
PSQL_PID=$!
echo $PSQL_PID > postgres.pid
sleep 10
if (echo '\q' | psql -h $TMP postgres postgres); then
echo "PostgreSQL server is verified to be started."
else
echo "Failed to connect to local PostgreSQL server."
exit 2
fi
ls -ltrh $NIX_BUILD_TOP
DBUSER=$(whoami)
DBNAME=$DBUSER
export PGPASSFILE=$NIX_BUILD_TOP/pgpass
echo "$TMP:5432:$DBUSER:$DBUSER:*" > $PGPASSFILE
cp -vir ${./schema} ../schema
chmod 600 $PGPASSFILE
psql -h $TMP postgres postgres <<EOF
create role $DBUSER with createdb login password '$DBPASS';
alter user $DBUSER with superuser;
create database $DBNAME with owner = $DBUSER;
\\connect $DBNAME
ALTER SCHEMA public OWNER TO $DBUSER;
EOF
'';
# Clean up Postgres after running database tests
postCheck = ''
echo post-check
DBNAME=$(whoami)
NAME=db_schema.sql
mkdir -p $out/nix-support
echo "Dumping schema to db_schema.sql"
pg_dump -h $TMP -s $DBNAME > $out/$NAME
kill $(cat postgres.pid)
echo "Adding to build products..."
echo "file binary-dist $out/$NAME" > $out/nix-support/hydra-build-products
'';
project = (nixpkgs.haskell-nix.cabalProject' ({ config, lib, pkgs, ... }: rec {
src = ./.;
name = "cardano-db-sync";
compiler-nix-name =
if system == "x86_64-linux"
then lib.mkDefault "ghc810"
else lib.mkDefault "ghc963";
flake.variants =
let
compilers = lib.optionals (system == "x86_64-linux") ["ghc963"];
in
lib.genAttrs compilers (c: { compiler-nix-name = c; });
inputMap = {
"https://chap.intersectmbo.org/" = inputs.CHaP;
};
shell.tools = {
cabal = "3.10.1.0";
ghcid = "0.8.8";
haskell-language-server = {
src = nixpkgs.haskell-nix.sources."hls-1.10";
};
};
# Now we use pkgsBuildBuild, to make sure that even in the cross
# compilation setting, we don't run into issues where we pick tools
# for the target.
shell.buildInputs = with nixpkgs.pkgsBuildBuild; [
gitAndTools.git
fourmolu
hlint
] ++ lib.optionals (config.compiler-nix-name == "ghc8107") [
# Weeder requires the GHC version to match HIE files
weeder
];
shell.withHoogle = true;
modules = [
({ lib, pkgs, ... }: {
# Ignore version bounds
packages.katip.doExactConfig = true;
# Split data to reduce closure size
packages.ekg.components.library.enableSeparateDataOutput = true;
# Systemd can't be statically linked
packages.cardano-node.flags.systemd =
!pkgs.stdenv.hostPlatform.isMusl;
})
({
# Override extra-source-files
packages.cardano-db-sync.package.extraSrcFiles =
[ "../schema/*.sql" ];
packages.cardano-db.package.extraSrcFiles =
["../config/pgpass-testnet"];
packages.cardano-db.components.tests.schema-rollback.extraSrcFiles =
[ "src/Cardano/Db/Schema.hs" "src/Cardano/Db/Delete.hs" ];
packages.cardano-db.components.tests.test-db.extraSrcFiles =
[ "../config/pgpass-mainnet" ];
packages.cardano-chain-gen.package.extraSrcFiles =
[ "../schema/*.sql" ];
})
({ lib, config, ... }:
# Disable haddock on 8.x
lib.mkIf (lib.versionOlder config.compiler.version "9") {
packages.cardano-ledger-alonzo.doHaddock = false;
packages.cardano-ledger-allegra.doHaddock = false;
packages.cardano-ledger-api.doHaddock = false;
packages.cardano-ledger-babbage.doHaddock = false;
packages.cardano-ledger-conway.doHaddock = false;
packages.cardano-ledger-shelley.doHaddock = false;
packages.cardano-protocol-tpraos.doHaddock = false;
packages.ouroboros-network-framework.doHaddock = false;
})
({ lib, pkgs, config, ... }:
lib.mkIf (lib.versionAtLeast config.compiler.version "9.4") {
# lib:ghc is a bit annoying in that it comes with it's own build-type:Custom, and then tries
# to call out to all kinds of silly tools that GHC doesn't really provide.
# For this reason, we try to get away without re-installing lib:ghc for now.
reinstallableLibGhc = false;
})
({ pkgs, ... }:
# Database tests
let
postgresTest = {
build-tools = [ pkgs.pkgsBuildHost.postgresql_12 ];
inherit preCheck;
inherit postCheck;
};
in {
packages.cardano-db.components.tests.test-db = postgresTest;
packages.cardano-chain-gen.components.tests.cardano-chain-gen =
postgresTest;
})
];
})).appendOverlays [
# Collect local package `exe`s
nixpkgs.haskell-nix.haskellLib.projectOverlays.projectComponents
# Add git revision to `exe`s
(final: prev: {
hsPkgs = final.pkgs.setGitRevForPaths (self.rev or "dirty") [
"cardano-db-sync.components.exes.cardano-db-sync"
"cardano-smash-server.components.exes.cardano-smash-server"
"cardano-db-tool.components.exes.cardano-db-tool"
] prev.hsPkgs;
})
];
staticChecks =
let
inherit (project.args) src compiler-nix-name;
in
with nixpkgs; {
hlint = callPackage hlintCheck { inherit src hlint; };
fourmolu = callPackage fourmoluCheck { inherit src fourmolu; };
shellcheck = callPackage shellCheck { inherit src; };
};
flake = project.flake (
nixpkgs.lib.optionalAttrs (system == "x86_64-linux") {
crossPlatforms = p: [p.musl64];
}
);
in with nixpkgs; lib.recursiveUpdate flake (
let
mkDist = platform: project:
let
exes = lib.collect lib.isDerivation project.exes;
name = "cardano-db-sync-${version}-${platform}";
version = project.exes.cardano-db-sync.identifier.version;
env = {
nativeBuildInputs = with nixpkgs; [ haskellBuildUtils bintools ];
};
in
nixpkgs.runCommand name env
''
mkdir -p $out release
cd release
# Copy exes to intermediate dir
cp \
--no-clobber \
--remove-destination \
--verbose \
${lib.concatMapStringsSep " " (exe: "${exe}/bin/*") exes} \
./
# Rewrite libs on macos (from iohk-utils)
${lib.optionalString
(platform == "macos")
(lib.concatMapStrings
(exe: "rewrite-libs . ${exe}/bin/*")
exes)}
# Package distribution
dist_file=${name}.tar.gz
tar -czf $out/$dist_file .
# Write summary file
mkdir $out/nix-support
echo "file binary-dist $out/$dist_file" > $out/nix-support/hydra-build-products
'';
cardano-db-sync-linux = mkDist "linux" project.projectCross.musl64;
cardano-db-sync-macos = mkDist "macos" project;
cardano-smash-server-no-basic-auth = (project.appendModule {
modules = [{packages.cardano-smash-server.flags.disable-basic-auth = true;}];
}).exes.cardano-smash-server;
docker = callPackage ./nix/docker.nix {
inherit (inputs.iohkNix.lib) evalService;
};
inherit (docker) cardano-db-sync-docker cardano-smash-server-docker;
# TODO: macOS builders are resource-constrained and cannot run the detabase
# integration tests. Add these back when we get beefier builders.
nonRequiredMacOSPaths = [
"checks.cardano-chain-gen:test:cardano-chain-gen"
"checks.cardano-db:test:test-db"
"ghc963.checks.cardano-chain-gen:test:cardano-chain-gen"
"ghc963.checks.cardano-db:test:test-db"
];
nonRequiredPaths =
if hostPlatform.isMacOS then
nonRequiredMacOSPaths
else [];
in rec {
checks = staticChecks;
hydraJobs = callPackages inputs.iohkNix.utils.ciJobsAggregates {
ciJobs = flake.hydraJobs;
nonRequiredPaths = map lib.hasPrefix nonRequiredPaths;
} // lib.optionalAttrs (system == "x86_64-linux") {
inherit
cardano-db-sync-linux
cardano-db-sync-docker
cardano-smash-server-docker;
} // lib.optionalAttrs (system == "x86_64-darwin") {
inherit cardano-db-sync-macos;
} // {
inherit cardano-smash-server-no-basic-auth;
checks = staticChecks;
};
legacyPackages = pkgs;
packages = lib.optionalAttrs (system == "x86_64-linux") {
inherit
cardano-db-sync-linux
cardano-db-sync-docker
cardano-smash-server-docker
project;
default = flake.packages."cardano-db-sync:exe:cardano-db-sync";
} // lib.optionalAttrs (system == "x86_64-darwin") {
inherit cardano-db-sync-macos;
} // {
inherit cardano-smash-server-no-basic-auth;
};
})) // {
nixosModules = {
cardano-db-sync = { pkgs, lib, ... }: {
imports = [ ./nix/nixos/cardano-db-sync-service.nix ];
services.cardano-db-sync.dbSyncPkgs =
let
pkgs' = self.legacyPackages.${pkgs.system};
in {
inherit (pkgs') cardanoLib schema cardano-db-sync;
# cardano-db-tool
cardanoDbSyncHaskellPackages.cardano-db-tool.components.exes.cardano-db-tool =
pkgs'.cardano-db-tool;
};
};
};
};
nixConfig = {
extra-substituters = [ "https://cache.iog.io" ];
extra-trusted-public-keys = [ "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" ];
allow-import-from-derivation = true;
experimental-features = [ "nix-command" "flakes" "fetch-closure" ];
};
}