-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
702 lines (617 loc) · 27 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
{
description = "Primer is a pedagogical functional programming language.";
inputs = {
haskell-nix.url = "github:input-output-hk/haskell.nix";
# We use this for some convenience functions only.
hacknix.url = "github:hackworthltd/hacknix";
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
# Pin pre-commit-hooks because newer versions need `cljfmt`, which
# isn't included in the current haskell.nix nixpkgs pin.
pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix/9d3d7e18c6bc4473d7520200d4ddab12f8402d38";
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix.url = "github:numtide/treefmt-nix";
# Let haskell.nix dictate the nixpkgs we use, as that will ensure
# better haskell.nix cache hits.
nixpkgs.follows = "haskell-nix/nixpkgs-unstable";
hacknix.inputs.nixpkgs.follows = "nixpkgs";
pre-commit-hooks-nix.inputs.nixpkgs.follows = "nixpkgs";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
ghc-wasm.url = "git+https://gitlab.haskell.org/ghc/ghc-wasm-meta";
};
outputs = inputs@ { flake-parts, ... }:
let
# A flake can get its git revision via `self.rev` if its working
# tree is clean and its index is empty, so we use that for the
# program version when it's available.
#
# When the working tree is modified or the index is not empty,
# evaluating `self.rev` is an error. However, we *can* use
# `self.lastModifiedDate` in that case, which is at least a bit
# more helpful than returning "unknown" or some other static
# value. (This should only happen when you `nix run` in a
# modified repo. Hydra builds will always be working from a
# clean git repo, of course.)
version =
let
v = inputs.self.rev or inputs.self.lastModifiedDate;
in
builtins.trace "Nix Primer version is ${v}" "git-${v}";
ghcVersion = "ghc9101";
# We must keep the weeder version in sync with the version of
# GHC we're using.
weederVersion = "2.8.0";
# Fourmolu updates often alter formatting arbitrarily, and we want to
# have more control over this.
fourmoluVersion = "0.16.2.0";
allOverlays = [
inputs.haskell-nix.overlay
inputs.self.overlays.default
];
# cabal-fmt needs an override for GHC > 9.8.1.
cabal-fmt-override = {
version = "latest";
cabalProject = ''
packages: .
allow-newer: cabal-fmt:base
'';
};
in
flake-parts.lib.mkFlake { inherit inputs; } {
debug = true;
imports = [
inputs.pre-commit-hooks-nix.flakeModule
inputs.treefmt-nix.flakeModule
];
systems = [ "x86_64-linux" "aarch64-darwin" ];
perSystem = { config, pkgs, system, ... }:
let
# haskell.nix does a lot of heavy lifiting for us and gives us a
# flake for our Cabal project with the following attributes:
# `checks`, `apps`, and `packages`.
primerFlake = pkgs.primer.flake { };
weeder =
let
weederTool = pkgs.haskell-nix.tool ghcVersion "weeder" weederVersion;
getLibHIE = package:
pkgs.lib.optional (package.components ? library)
{ name = "${package.identifier.name}-library"; path = package.components.library.hie; };
getHIE = package: component: pkgs.lib.lists.map
(cn: {
name = "${package.identifier.name}-${component}-${cn}";
path = package.components.${component}.${cn}.hie;
})
(builtins.attrNames package.components.${component});
getHIEs = package:
getLibHIE package
++ pkgs.lib.concatMap (getHIE package)
[ "benchmarks" "exes" "sublibs" "tests" ];
primer-packages = pkgs.haskell-nix.haskellLib.selectProjectPackages pkgs.primer;
in
pkgs.runCommand "weeder"
{
weederConfig = ./weeder.dhall;
allHieFiles = pkgs.linkFarm
"primer-hie-files"
(pkgs.lib.concatMap getHIEs (builtins.attrValues primer-packages));
}
''
export XDG_CACHE_HOME=$(mktemp -d)
${weederTool}/bin/weeder --config $weederConfig --hie-directory $allHieFiles
echo "No issues found."
touch $out
'';
openapi-validate = pkgs.runCommand "openapi-validate" { }
''
${pkgs.openapi-generator-cli}/bin/openapi-generator-cli validate --recommend -i ${pkgs.primer-openapi-spec}
echo "No issues found."
touch $out
'';
# This should go in `primer-sqitch.passthru.tests`, but
# those don't work well with flakes.
primer-sqitch-test-sqlite = pkgs.runCommand "primer-sqitch-sqlite-test" { } ''
${pkgs.primer-sqitch}/bin/primer-sqitch deploy --verify db:sqlite:primer.db
${pkgs.primer-sqitch}/bin/primer-sqitch revert db:sqlite:primer.db
${pkgs.primer-sqitch}/bin/primer-sqitch verify db:sqlite:primer.db | grep "No changes deployed"
${pkgs.primer-sqitch}/bin/primer-sqitch deploy --verify db:sqlite:primer.db
touch $out
'';
# Filter out any file in this repo that doesn't affect a Cabal
# build or Haskell-related check. (Note: this doesn't need to be
# 100% accurate, it's just an optimization to cut down on
# extraneous Nix builds.)
onlyHaskellSrc =
let
inherit (pkgs.haskell-nix) haskellSourceFilter;
inherit (pkgs.haskell-nix.haskellLib) cleanGit cleanSourceWith;
primerSourceFilter = name: type:
let baseName = baseNameOf (toString name);
in ! (
baseName == ".buildkite" ||
baseName == ".github" ||
baseName == "CODE_OF_CONDUCT.md" ||
baseName == "CONTRIBUTING.md" ||
baseName == "DCO.md" ||
baseName == "Makefile" ||
baseName == "README.md" ||
baseName == "SECURITY.md" ||
baseName == "bugreport.sh" ||
pkgs.lib.hasPrefix "cabal.project.local" baseName ||
baseName == "ci.nix" ||
baseName == "default.nix" ||
baseName == "docs" ||
baseName == "flake-compat.nix" ||
baseName == "flake.lock" ||
baseName == "flake.nix" ||
baseName == "nix" ||
baseName == "nixos-tests" ||
baseName == "shell.nix" ||
baseName == "sqitch"
);
in
cleanSourceWith {
filter = haskellSourceFilter;
name = "primer-src";
src = cleanSourceWith
{
filter = primerSourceFilter;
src = cleanGit
{
src = ./.;
};
};
};
in
{
# We need a `pkgs` that includes our own overlays within
# `perSystem`. This isn't done by default, so we do this
# workaround. See:
#
# https://github.com/hercules-ci/flake-parts/issues/106#issuecomment-1399041045
_module.args.pkgs = import inputs.nixpkgs
{
inherit system;
config = {
allowUnfree = true;
allowBroken = true;
};
overlays = allOverlays;
};
pre-commit = {
check.enable = true;
settings = {
src = ./.;
hooks = {
treefmt.enable = true;
actionlint.enable = true;
};
};
};
packages = {
inherit (pkgs) primer-service primer-client primer-openapi-spec;
inherit (pkgs) primer-benchmark;
inherit (pkgs)
run-primer-sqlite
primer-service-entrypoint
sqitch
primer-sqitch;
}
// (pkgs.lib.optionalAttrs (system == "x86_64-linux") {
inherit (pkgs) primer-service-docker-image;
inherit (pkgs) primer-benchmark-results-json;
inherit (pkgs) primer-criterion-results-github-action-benchmark;
inherit (pkgs) primer-benchmark-results-github-action-benchmark;
})
// primerFlake.packages;
checks = {
# Disabled, as it doesn't currently build with Nix.
#inherit weeder;
inherit openapi-validate;
inherit primer-sqitch-test-sqlite;
}
# Temporarily disabled due to a failure in nixpkgs.
# See:
# https://github.com/hackworthltd/primer/issues/1114
# // (pkgs.lib.optionalAttrs (system == "x86_64-linux")
# (inputs.hacknix.lib.testing.nixos.importFromDirectory ./nixos-tests
# {
# hostPkgs = pkgs;
# defaults.imports = [ inputs.self.nixosModules.default ];
# }
# )
# )
# Broken on NixOS. See:
# https://github.com/hackworthltd/primer/issues/632
// (pkgs.lib.optionalAttrs (system == "aarch64-darwin") {
# We're using `source-repository-package`, so we must
# disable this. See:
# https://github.com/hackworthltd/primer/issues/876
# Make sure HLS can typecheck our project.
# check-hls = pkgs.callPackage ./nix/pkgs/check-hls {
# src = onlyHaskellSrc;
# # Don't use the flake's version here; we only want to run
# # this HLS check when the Haskell source files have
# # changed, not on every commit to this repo.
# version = "1.0";
# # This is a bit of a hack, but we don't know a better way.
# inherit (primerFlake) devShell;
# };
})
// primerFlake.checks;
apps =
let
mkApp = pkg: script: {
type = "app";
program = "${pkg}/bin/${script}";
};
in
(pkgs.lib.mapAttrs (name: pkg: mkApp pkg name) {
inherit (pkgs) primer-client;
inherit (pkgs) primer-openapi-spec;
inherit (pkgs) primer-benchmark;
inherit (pkgs)
run-primer-sqlite
primer-service-entrypoint
primer-sqitch;
})
// primerFlake.apps;
treefmt.config =
let
haskellExcludes = [
"primer/test/outputs"
"primer-api/test/outputs"
"primer-service/test/outputs"
];
haskellNixTools = pkgs.haskell-nix.tools ghcVersion {
fourmolu = fourmoluVersion;
};
in
{
projectRootFile = "flake.nix";
programs.hlint = {
enable = true;
package = pkgs.hlint;
};
programs.cabal-fmt = {
enable = true;
package = pkgs.cabal-fmt;
};
programs.fourmolu = {
enable = true;
package = haskellNixTools.fourmolu;
};
programs.nixpkgs-fmt.enable = true;
programs.shellcheck.enable = true;
settings.formatter.hlint.excludes = haskellExcludes;
settings.formatter.fourmolu.excludes = haskellExcludes;
};
devShells = {
default = primerFlake.devShell // {
inputsFrom = [
config.treefmt.build.devShell
];
};
wasm = pkgs.mkShell {
packages = with inputs.ghc-wasm.packages.${system};
[
wasm32-wasi-ghc-9_10
wasm32-wasi-cabal-9_10
wasmtime
pkgs.gnumake
# We need to run native `tasty-discover` at compile
# time, because we can't do it via `wasmtime`.
(pkgs.haskell-nix.tool ghcVersion "tasty-discover" { })
];
};
};
};
flake =
let
# See above, we need to use our own `pkgs` within the flake.
pkgs = import inputs.nixpkgs
{
system = "x86_64-linux";
config = {
allowUnfree = true;
allowBroken = true;
};
overlays = allOverlays;
};
in
{
overlays.default = (final: prev:
let
ghc982Tools = final.haskell-nix.tools "ghc982" {
hlint = "latest";
cabal-fmt = "latest";
};
sqitch = final.callPackage ./nix/pkgs/sqitch {
sqliteSupport = true;
};
scripts = final.lib.recurseIntoAttrs (final.callPackage ./nix/pkgs/scripts {
sqitchDir = ./sqitch;
inherit version;
});
primer = final.haskell-nix.cabalProject {
compiler-nix-name = ghcVersion;
src = ./.;
modules = [
{
# We want -Werror for Nix builds (primarily for CI).
packages =
let
# Tell Tasty to detect missing golden tests,
# rather than silently ignoring them.
#
# Until upstream addresses the issue, this is a
# workaround for
# https://github.com/hackworthltd/primer/issues/298
preCheckTasty = ''
export TASTY_NO_CREATE=true
'';
in
{
primer = {
ghcOptions = [ "-Werror" ];
preCheck = preCheckTasty;
};
primer-api = {
ghcOptions = [ "-Werror" ];
preCheck = preCheckTasty;
};
primer-service = {
ghcOptions = [ "-Werror" ];
preCheck = preCheckTasty;
};
primer-benchmark = {
ghcOptions = [ "-Werror" ];
preCheck = preCheckTasty;
};
};
}
{
# Build everything with -O2.
configureFlags = [ "-O2" ];
# Generate HIE files for everything.
writeHieFiles = true;
# Generate nice Haddocks & a Hoogle index for
# everything.
doHaddock = true;
doHyperlinkSource = true;
doQuickjump = true;
doHoogle = true;
}
{
# Some packages are not visible to haskell.nix's planner, and need
# to be added manually.
#
# Ref:
# https://github.com/input-output-hk/haskell.nix/commit/61fbe408c01b6d61d010e6fb8e78bd19b5b025cc
package-keys = [
"bytestring-builder"
"diagrams"
"fail"
];
# These packages don't/can't generate HIE files. See:
# https://github.com/input-output-hk/haskell.nix/issues/1242
packages.mtl-compat.writeHieFiles = false;
packages.bytestring-builder.writeHieFiles = false;
packages.fail.writeHieFiles = false;
packages.diagrams.writeHieFiles = false;
packages.happy-lib.writeHieFiles = false;
}
{
#TODO This shouldn't be necessary - see the commented-out `build-tool-depends` in primer.cabal.
packages.primer.components.tests.primer-test.build-tools = [ (final.haskell-nix.tool ghcVersion "tasty-discover" { }) ];
packages.primer-api.components.tests.primer-api-test.build-tools = [ (final.haskell-nix.tool ghcVersion "tasty-discover" { }) ];
packages.primer-selda.components.tests.primer-selda-test.build-tools = [
(final.haskell-nix.tool ghcVersion "tasty-discover" { })
final.primer-sqitch
];
packages.primer-service.components.tests.primer-service-test.build-tools = [
(final.haskell-nix.tool ghcVersion "tasty-discover" { })
final.primer-sqitch
];
}
(
let
# This makes it a lot easier to see which test is the culprit when CI fails.
hide-successes = [ "--hide-successes" ];
#TODO Haskell.nix would ideally pick this up from `cabal.project`.
# See: https://github.com/input-output-hk/haskell.nix/issues/1149#issuecomment-946664684
size-cutoff = [ "--size-cutoff=32768" ];
in
{
packages.primer.components.tests.primer-test.testFlags = hide-successes ++ size-cutoff;
packages.primer-api.components.tests.primer-api-test.testFlags = hide-successes ++ size-cutoff;
packages.primer-service.components.tests.primer-service-test.testFlags = hide-successes ++ size-cutoff;
packages.primer-selda.components.tests.primer-selda-test.testFlags = hide-successes;
packages.primer-benchmark.components.tests.primer-benchmark-test.testFlags = hide-successes;
}
)
];
shell = {
# We're using a `source-repository-package`, so we must disable this.
# See:
# https://github.com/hackworthltd/primer/issues/876
#exactDeps = true;
withHoogle = true;
tools = {
ghcid = "latest";
haskell-language-server.src = pkgs.haskell-nix.sources."hls-2.9";
implicit-hie = "latest";
cabal = "latest";
# Disabled, as it doesn't currently build with Nix.
#weeder = weederVersion;
fourmolu = fourmoluVersion;
#TODO Explicitly requiring tasty-discover shouldn't be necessary - see the commented-out `build-tool-depends` in primer.cabal.
tasty-discover = "latest";
# Required until http-client-tls mess is resolved.
#
# Ref:
# https://github.com/input-output-hk/haskell.nix/issues/2277
hoogle.index-state = "2024-10-01T00:00:00Z";
};
buildInputs = (with final; [
nixpkgs-fmt
sqlite
openapi-generator-cli
hlint
cabal-fmt
# For Language Server support.
nodejs-18_x
# sqitch & related
nix-generate-from-cpan
sqitch
primer-sqitch
]);
shellHook = ''
export HIE_HOOGLE_DATABASE="$(cat $(${final.which}/bin/which hoogle) | sed -n -e 's|.*--database \(.*\.hoo\).*|\1|p')"
'';
};
};
primerFlake = primer.flake { };
# Generate the Primer service OpenAPI 3 spec file.
primer-openapi-spec = (final.runCommand "primer-openapi" { }
"${final.primer-openapi}/bin/primer-openapi > $out").overrideAttrs
(drv: {
meta.platforms = final.lib.platforms.all;
});
primer-service-docker-image = final.dockerTools.buildLayeredImage {
name = "primer-service";
tag = version;
contents = [
final.primer-service-entrypoint
]
++ (with final; [
# These are helpful for debugging broken images.
bashInteractive
coreutils
lsof
procps
# Required for `kubectl cp`, which is potentially
# useful for getting databases into and out of the
# pod.
gnutar
]);
config =
let port = final.lib.primer.defaultServicePort;
in
{
Entrypoint = [ "/bin/primer-service-entrypoint" ];
# Note that we can't set
# "org.opencontainers.image.created" here because
# it would introduce an impurity. If we want to
# set it, we'll need to set it when we push to a
# registry.
Labels = {
"org.opencontainers.image.source" =
"https://github.com/hackworthltd/primer";
"org.opencontainers.image.documentation" =
"https://github.com/hackworthltd/primer";
"org.opencontainers.image.title" = "primer-service";
"org.opencontainers.image.description" =
"The Primer API service.";
"org.opencontainers.image.version" = version;
"org.opencontainers.image.authors" =
"org.opencontainers.image.licenses" = "AGPL-3.0";
"org.opencontainers.image.vendor" = "Hackworth Ltd";
"org.opencontainers.image.url" =
"https://github.com/hackworthltd/primer";
"org.opencontainers.image.revision" = inputs.self.rev or "dirty";
};
ExposedPorts = { "${toString port}/tcp" = { }; };
Env = [
# Environment variables required by the
# entrypoint with reasonable default values.
#
# Note that we do not provide default values or
# otherwise set SQLITE_DB, which is required by
# the entrypoint script, so you must set this
# yourself in the container environment.
"SERVICE_PORT=${toString port}"
"PRIMER_VERSION=${version}"
# Needed for the `primer-service` banner.
"LANG=C.UTF-8"
# Sqitch will fail in a container if these are not
# set. Their specific values are not important.
"SQITCH_EMAIL=root@localhost"
"SQITCH_FULLNAME=Primer User"
];
};
};
# Note: these benchmarks should only be run (in CI) on a
# "benchmark" machine. This is enforced for our CI system
# via Nix's `requiredSystemFeatures`.
#
# The `lastEnvChange` value is an impurity that we can
# modify when we want to force a new benchmark run
# despite the benchmarking code not having changed, as
# otherwise Nix will cache the results. It's intended to
# be used to track changes to the benchmarking
# environment, such as changes to hardware, that Nix
# doesn't know about.
#
# The value should be formatted as an ISO date, followed
# by a "." and a 2-digit monotonic counter, to allow for
# multiple changes on the same date. We store this value
# in a `lastEnvChange` file in the derivation output, so
# that we can examine results in the Nix store and know
# which benchmarking environment was used to generate
# them.
benchmarks =
let
lastEnvChange = "20240408.02";
in
final.callPackage ./nix/pkgs/benchmarks {
inherit lastEnvChange;
};
in
{
lib = (prev.lib or { }) // {
primer = (prev.lib.primer or { }) // {
defaultServicePort = 8081;
inherit version;
};
};
inherit sqitch;
inherit (scripts)
primer-sqitch
run-primer-sqlite
primer-service-entrypoint;
inherit primer;
primer-service = primerFlake.packages."primer-service:exe:primer-service";
primer-client = primerFlake.packages."primer-service:exe:primer-client";
primer-openapi = primerFlake.packages."primer-service:exe:primer-openapi";
primer-benchmark = primerFlake.packages."primer-benchmark:bench:primer-benchmark";
inherit primer-service-docker-image;
inherit primer-openapi-spec;
inherit (benchmarks) primer-benchmark-results-json;
inherit (benchmarks) primer-criterion-results-github-action-benchmark;
inherit (benchmarks) primer-benchmark-results-github-action-benchmark;
inherit (ghc982Tools) cabal-fmt hlint;
}
);
nixosModules.default = {
nixpkgs.overlays = allOverlays;
};
hydraJobs = {
inherit (inputs.self) packages;
inherit (inputs.self) checks;
inherit (inputs.self) devShells;
required-ci = pkgs.releaseTools.aggregate {
name = "required-ci";
constituents = builtins.map builtins.attrValues (with inputs.self.hydraJobs; [
packages.x86_64-linux
packages.aarch64-darwin
checks.x86_64-linux
checks.aarch64-darwin
]);
meta.description = "Required CI builds";
};
};
ciJobs = inputs.hacknix.lib.flakes.recurseIntoHydraJobs inputs.self.hydraJobs;
};
};
}