-
Notifications
You must be signed in to change notification settings - Fork 212
/
flake.nix
466 lines (437 loc) · 17.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
{
description = "Cardano Wallet";
############################################################################
#
# Cardano Wallet Flake Nix build
#
# Nix Flake support instruction: https://nixos.wiki/wiki/Flakes
#
# Derivation attributes of this file can be build with "nix build .#<attribute>"
# Discover attribute names using tab-completion in your shell.
#
# Interesting top-level attributes:
#
# - cardano-wallet - cli executable
# - tests - attrset of test-suite executables
# - cardano-wallet.unit
# - cardano-wallet.integration
# - etc (layout is PACKAGE.COMPONENT)
# - checks - attrset of test-suite results
# - cardano-wallet.unit
# - cardano-wallet.integration
# - etc
# - benchmarks - attret of benchmark executables
# - cardano-wallet.db
# - cardano-wallet.latency
# - etc
# - dockerImage - tarball of the docker image
#
# Other documentation:
# https://input-output-hk.github.io/cardano-wallet/dev/Building#nix-build
#
############################################################################
############################################################################
# Continuous Integration (CI)
#
# This flake contains a few outputs useful for continous integration.
# These outputs come in two flavors:
#
# outputs.packages."<system>".ci.* - build a test, benchmark, …
# outputs.apps."<system>".ci.* - run a test, benchmark, …
#
# For building, say all tests, use `nix build`:
#
# nix build .#ci.tests.all
#
# For running, say the unit tests use `nix run`:
#
# nix run .#ci.tests.unit
#
# (Running an item will typically also build it if it has not been built
# in the nix store already.)
#
# (In some cases, building or running fails with a segmentation fault;
# the nix garbage collection is likely to blame.
# Use `GC_DONT_GC=1 nix build …` as workaround.)
#
#
# The CI-related outputs are
#
# - outputs.packages."<system>".ci.
# - tests
# - all - build all test executables
# - benchmarks
# - all - build all benchmarks
# - restore - build individual benchmark
# - …
# - artifacts - artifacts by platform
# - linux64.release
# - win64
# - release
# - tests - bundle of executables for testing on Windows
# - macos-intel.release
# - macos-silicon.release
# - dockerImage
# - outputs.apps."<system>".ci.
# - tests
# - unit - run the unit tests on this system
# - integration - run the integration tests on this system
# - benchmarks
# - restore
# - …
#
# Recommended granularity:
#
# after each commit:
# on x86_64-linux:
# nix build .#ci.tests.all
# nix build .#ci.benchmarks.all
# nix build .#ci.artifacts.linux64.release
# nix build .#ci.artifacts.win64.release
# nix build .#ci.artifacts.win64.tests
#
# nix run .#ci.tests.unit
#
# before each pull request merge:
# on each supported system:
# nix build .#ci.benchmarks.all
# nix build .#ci.tests.all
#
# nix run .#ci.tests.unit
# nix run .#ci.tests.integration
#
# nightly:
# on x86_64-linux:
# nix build .#ci.artifacts.dockerImage
#
# nix run .#ci.benchmarks.restore
# nix run .#ci.benchmarks.…
#
# on x65_64-darwin: (macos)
# nix build .#ci.artifacts.win64.release
#
############################################################################
############################################################################
# TODO Continuous Integration (CI)
#
# Make the flake.nix file *itself* part of continuous integration,
# i.e. check that `devShells`, `scripts` or `nixosTests` evalute and build
# correctly.
############################################################################
inputs = {
nixpkgs.follows = "haskellNix/nixpkgs-unstable";
hostNixpkgs.follows = "nixpkgs";
CHaP = {
url = "github:input-output-hk/cardano-haskell-packages?ref=repo";
flake = false;
};
haskellNix = {
url = "github:input-output-hk/haskell.nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.hackage.follows = "hackage";
};
hackage = {
url = "github:input-output-hk/hackage.nix";
flake = false;
};
flake-utils.url = "github:numtide/flake-utils";
iohkNix = {
url = "github:input-output-hk/iohk-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = {
url = "github:input-output-hk/flake-compat";
flake = false;
};
customConfig.url = "github:input-output-hk/empty-flake";
emanote = {
url = "github:srid/emanote";
};
ema = {
url = "github:srid/ema";
};
};
outputs = { self, nixpkgs, hostNixpkgs, flake-utils, haskellNix, iohkNix, CHaP, customConfig, emanote, ... }:
let
inherit (nixpkgs) lib;
config = import ./nix/config.nix lib customConfig;
inherit (flake-utils.lib) eachSystem mkApp flattenTree;
removeRecurse = lib.filterAttrsRecursive (n: _: n != "recurseForDerivations");
inherit (iohkNix.lib) evalService;
supportedSystems = import ./nix/supported-systems.nix;
defaultSystem = lib.head supportedSystems;
overlay = final: prev: {
cardanoWalletHaskellProject = self.legacyPackages.${final.system};
inherit (final.cardanoWalletHaskellProject.hsPkgs.cardano-wallet.components.exes) cardano-wallet;
};
nixosModule = { pkgs, lib, ... }: {
imports = [ ./nix/nixos/cardano-wallet-service.nix ];
services.cardano-node.package = lib.mkDefault self.defaultPackage.${pkgs.system};
};
nixosModules.cardano-wallet = nixosModule;
# Which exes should be put in the release archives.
releaseContents = jobs: map (exe: jobs.${exe}) [
"cardano-wallet"
"bech32"
"cardano-address"
"cardano-cli"
"cardano-node"
];
# Helper functions for separating unit and integration tests
setEmptyAttrsWithCondition = cond:
lib.mapAttrsRecursiveCond
(value: !(lib.isDerivation value)) # do not modify attributes of derivations
(path: value: if cond path then {} else value);
keepIntegrationChecks =
setEmptyAttrsWithCondition
(path: !lib.any (lib.hasPrefix "integration") path);
keepUnitChecks =
setEmptyAttrsWithCondition
(path: !lib.any (name: name == "unit" || name == "test") path);
# Define flake outputs for a particular system.
mkOutputs = system:
let
pkgs = import nixpkgs {
inherit system;
inherit (haskellNix) config;
overlays = [
haskellNix.overlay
iohkNix.overlays.utils
iohkNix.overlays.crypto
iohkNix.overlays.haskell-nix-extra
iohkNix.overlays.cardano-lib
# Cardano deployments
(import ./nix/overlays/cardano-deployments.nix)
# Other packages overlay
(import ./nix/overlays/pkgs.nix)
# Our own utils (cardanoWalletLib)
(import ./nix/overlays/common-lib.nix)
overlay
];
};
inherit (pkgs.stdenv) buildPlatform;
inherit (pkgs.haskell-nix.haskellLib)
isProjectPackage
collectComponents
collectChecks
check;
project = (import ./nix/haskell.nix CHaP pkgs.haskell-nix).appendModule [{
gitrev =
if config.gitrev != null
then config.gitrev
else self.rev or "0000000000000000000000000000000000000000";
}
config.haskellNix];
mkPackages = project:
let
coveredProject = project.appendModule { coverage = true; };
self = {
# Cardano wallet
cardano-wallet = import ./nix/release-build.nix {
inherit pkgs;
exe = project.hsPkgs.cardano-wallet.components.exes.cardano-wallet;
backend = self.cardano-node;
};
# Local test cluster and mock metadata server
inherit (project.hsPkgs.cardano-wallet.components.exes) local-cluster mock-token-metadata-server;
# Adrestia tool belt
inherit (project.hsPkgs.bech32.components.exes) bech32;
inherit (project.hsPkgs.cardano-addresses-cli.components.exes) cardano-address;
# Cardano
inherit (project.hsPkgs.cardano-cli.components.exes) cardano-cli;
cardano-node = project.hsPkgs.cardano-node.components.exes.cardano-node // {
deployments = pkgs.cardano-node-deployments;
};
# Provide db-converter, so daedalus can ship it without needing to
# pin an ouroborus-network rev.
inherit (project.hsPkgs.ouroboros-consensus-byron.components.exes) db-converter;
# Combined project coverage report
testCoverageReport = coveredProject.projectCoverageReport;
# `tests` are the test suites which have been built.
tests = removeRecurse (collectComponents "tests" isProjectPackage coveredProject.hsPkgs);
# `checks` are the result of executing the tests.
checks = removeRecurse (
lib.recursiveUpdate
(collectChecks isProjectPackage coveredProject.hsPkgs)
# Run the integration tests in the previous era too:
(
let
integrationCheck = check coveredProject.hsPkgs.cardano-wallet.components.tests.integration;
integrationPrevEraCheck = integrationCheck.overrideAttrs (prev: {
preCheck = prev.preCheck + ''
export LOCAL_CLUSTER_ERA=alonzo
'';
});
in
if integrationCheck.doCheck == true
then
{
cardano-wallet.integration-prev-era = integrationPrevEraCheck;
}
else
{}
)
);
# `benchmarks` are only built, not run.
benchmarks = removeRecurse (collectComponents "benchmarks" isProjectPackage project.hsPkgs);
};
in
self;
# nix run .#<network>/wallet
mkScripts = project: flattenTree (import ./nix/scripts.nix {
inherit project evalService;
customConfigs = [ config ];
});
# See the imported file for how to use the docker build.
mkDockerImage = packages: pkgs.callPackage ./nix/docker.nix {
exes = with packages; [ cardano-wallet local-cluster ];
base = with packages; [
bech32
cardano-address
cardano-cli
cardano-node
(pkgs.linkFarm "docker-config-layer" [{ name = "config"; path = pkgs.cardano-node-deployments; }])
];
};
mkDevShells = project: rec {
profiled = (project.appendModule { profiling = true; }).shell;
cabal = import ./nix/cabal-shell.nix {
haskellProject = project;
inherit (config) withCabalCache ghcVersion;
};
docs = pkgs.mkShell {
name = "cardano-wallet-docs";
nativeBuildInputs = [ emanote.packages.${system}.default pkgs.yq ];
# allow building the shell so that it can be cached
phases = [ "installPhase" ];
installPhase = "echo $nativeBuildInputs > $out";
};
};
# One ${system} can cross-compile artifacts for other platforms.
mkReleaseArtifacts = project:
lib.optionalAttrs buildPlatform.isLinux {
linux64.release =
let
# compiling with musl gives us a statically linked executable
linuxPackages = mkPackages project.projectCross.musl64;
in
import ./nix/release-package.nix {
inherit pkgs;
exes = releaseContents linuxPackages;
platform = "linux64";
format = "tar.gz";
};
win64 =
let
# windows is cross-compiled from linux
windowsPackages = mkPackages project.projectCross.mingwW64;
in {
release = import ./nix/release-package.nix {
inherit pkgs;
exes = releaseContents windowsPackages;
platform = "win64";
format = "zip";
};
# Testing on Windows is done using a collection of executables.
tests = import ./nix/windows-testing-bundle.nix {
inherit pkgs;
cardano-wallet = windowsPackages.cardano-wallet;
cardano-node = windowsPackages.cardano-node;
cardano-cli = windowsPackages.cardano-cli;
tests = lib.collect lib.isDerivation windowsPackages.tests;
benchmarks = lib.collect lib.isDerivation windowsPackages.benchmarks;
};
};
}
# macos is never cross-compiled
// lib.optionalAttrs buildPlatform.isMacOS {
macos-intel = lib.optionalAttrs buildPlatform.isx86_64 {
release = import ./nix/release-package.nix {
inherit pkgs;
exes = releaseContents (mkPackages project);
platform = "macos-intel";
format = "tar.gz";
};
};
macos-silicon = lib.optionalAttrs buildPlatform.isAarch64 {
release = import ./nix/release-package.nix {
inherit pkgs;
exes = releaseContents (mkPackages project);
platform = "macos-silicon";
format = "tar.gz";
};
};
};
in
rec {
legacyPackages = project;
# Built by `nix build .`
defaultPackage = packages.cardano-wallet;
# Run by `nix run .`
defaultApp = apps.cardano-wallet;
packages = mkPackages project // mkScripts project // rec {
dockerImage = mkDockerImage (mkPackages project.projectCross.musl64);
pushDockerImage = import ./.buildkite/docker-build-push.nix {
hostPkgs = import hostNixpkgs { inherit system; };
inherit dockerImage;
inherit (config) dockerHubRepoName;
};
inherit (pkgs) checkCabalProject cabalProjectRegenerate;
} // (lib.optionalAttrs buildPlatform.isLinux {
nixosTests = import ./nix/nixos/tests {
inherit pkgs project;
};
}) // {
# Continuous integration builds
ci.tests.all = pkgs.releaseTools.aggregate {
name = "cardano-wallet-tests";
meta.description = "Build (all) tests";
constituents =
lib.collect lib.isDerivation packages.tests;
};
ci.benchmarks = packages.benchmarks.cardano-wallet // {
all = pkgs.releaseTools.aggregate {
name = "cardano-wallet-benchmarks";
meta.description = "Build all benchmarks";
constituents =
lib.collect lib.isDerivation packages.benchmarks;
};
};
ci.artifacts = mkReleaseArtifacts project // {
dockerImage = packages.dockerImage;
};
};
# Heinrich: I don't quite understand the 'checks' attribute. See also
# https://www.reddit.com/r/NixOS/comments/x5cjmz/comment/in0qqm6/?utm_source=share&utm_medium=web2x&context=3
checks = packages.checks;
mkApp = name: pkg: {
type = "app";
program = pkg.exePath or "${pkg}/bin/${pkg.name or name}";
};
apps = lib.mapAttrs mkApp packages;
devShell = project.shell;
devShells = mkDevShells project;
ci.tests.run.unit = pkgs.releaseTools.aggregate
{
name = "tests.run.unit";
meta.description = "Run unit tests";
constituents =
lib.collect lib.isDerivation
(keepUnitChecks packages.checks);
};
ci.tests.run.integration = pkgs.releaseTools.aggregate
{
name = "tests.run.integration";
meta.description = "Run integration tests";
constituents =
lib.collect lib.isDerivation
(keepIntegrationChecks packages.checks);
};
};
systems = eachSystem supportedSystems mkOutputs;
in
lib.recursiveUpdate systems {
inherit overlay nixosModule nixosModules;
}
;
}