-
Notifications
You must be signed in to change notification settings - Fork 19
/
flake.nix
593 lines (536 loc) · 24.1 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
{
description = "Hercules CI Agent";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.flake-parts.url = "github:hercules-ci/flake-parts";
inputs.flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
inputs.haskell-flake.url = "github:srid/haskell-flake/0.3.0";
# Optional. Omit to use nixpkgs' nix
# inputs.nix = {
# url = "github:NixOS/nix/master";
# inputs.nixpkgs.follows = "nixpkgs";
# };
outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
let
inherit (nixpkgs.legacyPackages.x86_64-linux) emptyFile;
debug = false;
ifDebug = f:
if debug then f else x: x;
addDebug = ifDebug (pkg:
pkg.overrideAttrs (o: {
dontStrip = true;
enableDebugging = true;
separateDebugInfo = false;
})
);
agentFromFlakeConfig = cfg: opts: pkgs: lib:
let
mkIfNotNull = x: lib.mkIf (x != null) x;
in
{
package = self.packages.${pkgs.system}.hercules-ci-agent; # defaultPriority below
settings.labels.agent.source = "flake";
settings.labels.agent.revision =
mkIfNotNull (
if (self?rev
&& opts.package.highestPrio == lib.modules.defaultOverridePriority or lib.modules.defaultPriority
)
then self.rev
else if cfg.package ? rev
then cfg.package.rev
else null
);
};
agentFromFlakeModule = { config, lib, options, pkgs, ... }: {
_file = "${toString ./flake.nix}";
config.services.hercules-ci-agent =
agentFromFlakeConfig
config.services.hercules-ci-agent
options.services.hercules-ci-agent
pkgs
lib;
};
agentFromFlakeModule_multi = { config, lib, options, pkgs, ... }: {
_file = "${toString ./flake.nix}";
options =
let
mkIfNotNull = x: lib.mkIf (x != null) x;
inherit (lib) types mkOption;
in
{
services.hercules-ci-agents =
mkOption {
type = types.attrsOf (
types.submoduleWith {
modules = [
({ options, config, ... }: {
config = agentFromFlakeConfig config options pkgs lib;
})
];
}
);
};
};
};
suffixAttrs = suf: inputs.nixpkgs.lib.mapAttrs' (n: v: { name = n + suf; value = v; });
in
flake-parts.lib.mkFlake { inherit inputs; } (flakeArgs@{ config, lib, inputs, ... }: {
imports = [
inputs.flake-parts.flakeModules.easyOverlay
inputs.haskell-flake.flakeModule
./nix/variants.nix
./nix/flake-private-dev-inputs.nix
./docs/flake-docs-render.nix
];
config = {
privateDevInputSubflakePath = "dev/private";
partitionedAttrs.checks = "dev";
partitionedAttrs.devShells = "dev";
partitionedAttrs.herculesCI = "dev";
partitions.dev.settings = { inputs, ... }: {
imports = [
./nix/development.nix
./nix/flake-update-pre-commit.nix
inputs.hercules-ci-effects.flakeModule
inputs.pre-commit-hooks-nix.flakeModule
];
};
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
];
flake = {
overlay = config.flake.overlays.default;
# A module like the one in Nixpkgs
nixosModules.agent-service =
{ lib, pkgs, ... }:
{
_file = "${toString ./flake.nix}#nixosModules.agent-service";
imports = [
agentFromFlakeModule
./internal/nix/nixos/default.nix
];
# This module replaces what's provided by NixOS
disabledModules = [ "services/continuous-integration/hercules-ci-agent/default.nix" ];
config = {
services.hercules-ci-agent.settings.labels.module = "nixos-service";
};
};
# An opinionated module for configuring an agent machine
nixosModules.agent-profile =
{ lib, pkgs, ... }:
{
_file = "${toString ./flake.nix}#nixosModules.agent-profile";
imports = [
agentFromFlakeModule
./internal/nix/nixos/default.nix
./internal/nix/deploy-keys.nix
./internal/nix/gc.nix
];
# This module replaces what's provided by NixOS
disabledModules = [ "services/continuous-integration/hercules-ci-agent/default.nix" ];
config = {
services.hercules-ci-agent.settings.labels.module = "nixos-profile";
};
};
# A module for configuring multiple agents on a single machine
nixosModules.multi-agent-service =
{ lib, pkgs, ... }:
{
_file = "${toString ./flake.nix}#nixosModules.multi-agent-service";
imports = [
agentFromFlakeModule_multi
./internal/nix/nixos/multi.nix
];
# Existence of the original module could cause confusion, even if they
# can technically coexist.
disabledModules = [ "services/continuous-integration/hercules-ci-agent/default.nix" ];
options = let inherit (lib) types mkOption; in
{
services.hercules-ci-agents =
mkOption {
type = types.attrsOf (
types.submoduleWith {
modules = [{ config.settings.labels.module = "nixos-multi-service"; }];
}
);
};
};
};
# A nix-darwin module
darwinModules.agent-service =
{ lib, pkgs, ... }:
{
_file = "${toString ./flake.nix}#darwinModules.agent-service";
imports = [
agentFromFlakeModule
./internal/nix/nix-darwin/default.nix
];
# This module replaces what's provided by nix-darwin
disabledModules = [ "services/hercules-ci-agent" ];
config = {
services.hercules-ci-agent.settings.labels.module = "darwin-service";
};
};
# A nix-darwin module with more defaults set for machines that serve as agents
darwinModules.agent-profile =
{ lib, pkgs, ... }:
{
_file = "${toString ./flake.nix}#darwinModules.agent-profile";
imports = [
agentFromFlakeModule
./internal/nix/nix-darwin/default.nix
./internal/nix/gc.nix
];
# This module replaces what's provided by nix-darwin
disabledModules = [ "services/hercules-ci-agent" ];
config = {
services.hercules-ci-agent.settings.labels.module = "darwin-profile";
};
};
defaultApp = lib.mapAttrs (k: v: { program = v.hercules-ci-cli + "/bin/hci"; type = "app"; }) self.packages;
defaultTemplate = self.templates.nixos;
templates = {
nixos = {
path = ./templates/nixos;
description = "A NixOS configuration with Hercules CI Agent";
};
};
};
perSystem = { config, pkgs, system, ... }:
let
dev-and-test-overlay = self: pkgs:
{
testSuitePkgs = pkgs; # TODO: reuse pkgs via self so we don't build a variant
nix =
if inputs?nix
then
inputs.nix.packages.${pkgs.stdenv.hostPlatform.system}.default or (
lib.warn
"The `nix` flake does not define a package for system ${pkgs.stdenv.hostPlatform.system}. Using nixpkgs' `nix` instead."
pkgs.nix
)
else if pkgs.nix.version == "2.24.10"
then
pkgs.nix.overrideAttrs
(o: {
# We need to use the nix package from the flake, because the test runner
# will use the nix package from the flake.
patches = o.patches or [ ] ++ [
# https://github.com/NixOS/nix/pull/11821
(pkgs.fetchpatch2 {
name = "nix-git-cache-fix-empty-dir.patch";
url = "https://github.com/NixOS/nix/commit/388271e8ec6f5057bc8d39865fcc280e044b2844.diff";
hash = "sha256-9RomRFX4Kt7qv9ERJhcISdW1QHs24uuTcwuxHt0hTd8=";
})
];
})
else
pkgs.nix;
};
h = pkgs.haskell.lib.compose;
addCompactUnwind =
if pkgs.stdenv.hostPlatform.isDarwin
then h.appendConfigureFlags [ "--ghc-option=-fcompact-unwind" ]
else x: x;
nix = pkgs.nix;
in
{
config = {
# public overlay
overlayAttrs = {
inherit (config.packages) hercules-ci-cli;
hercules-ci-agent = config.packages.hercules-ci-agent // lib.optionalAttrs (self.sourceInfo?rev) { rev = self.sourceInfo.rev; };
hci = config.packages.hercules-ci-cli;
};
# internal pkgs
_module.args.pkgs =
import config.nixpkgsSource {
overlays = [
dev-and-test-overlay
flakeArgs.config.extraOverlay
];
config = { };
inherit system;
};
apps.hci = config.apps.internal-hci;
apps.hercules-ci-agent = config.apps.internal-hercules-ci-agent;
haskellProjects = {
internal = {
devShell.extraLibraries = hp: {
inherit (hp) releaser
ascii-progress
hnix-store-core
hnix-store-nar
# enable only when working on expr and up
# hercules-ci-cnix-store
# cachix
;
# Cachix deps (cachix was excluded because of its dependency on cnix-store)
} //
lib.listToAttrs (
map
(p: lib.nameValuePair p.pname p)
(lib.filter
(p: p?pname && p.pname != "hercules-ci-cnix-store")
hp.cachix_saved.getCabalDeps.libraryHaskellDepends
)
);
overrides = self: super: {
openapi3 = h.dontCheck (h.unmarkBroken super.openapi3);
cachix_saved = super.cachix;
cachix =
if flakeArgs.config.isForDevShell
then
# For the shell we omit cachix from the dependency search
# so as not to introduce a nix-built dependency on cnix-store
null
else if builtins.compareVersions super.cachix.version "1.5" >= 0
then
super.cachix.override
(o: {
inherit nix;
})
else
((super.cachix_1_3_3 or super.cachix).override (o: {
inherit nix;
})).overrideAttrs (o: {
postPatch = ''
${o.postPatch or ""}
# jailbreak pkgconfig deps
cp cachix.cabal cachix.cabal.backup
sed -i cachix.cabal -e 's/\(nix-[a-z]*\) *(==[0-9.]* *|| *>[0-9.]*) *&& *<[0-9.]*/\1/g'
sed -i cachix.cabal -e 's/pkgconfig-depends:.*/pkgconfig-depends: nix-main, nix-store/'
echo
echo Applied:
diff -U5 cachix.cabal.backup cachix.cabal ||:
echo
rm cachix.cabal.backup
'';
});
hercules-ci-optparse-applicative =
super.callPackage ./nix/hercules-ci-optparse-applicative.nix { };
inline-c-cpp =
# https://github.com/fpco/inline-c/pull/132
assert
lib.versionAtLeast super.ghc.version "9.4"
|| lib.any
(patch: lib.hasSuffix "inline-c-cpp-pr-132-1.patch" (baseNameOf patch))
super.inline-c-cpp.patches;
super.inline-c-cpp;
hie-bios =
h.appendPatch ./dev/hie-bios-fml.diff super.hie-bios;
servant-auth-server =
if (super.servant-auth-server.version == "0.4.8.0"
|| super.servant-auth-server.version == "0.4.9.0")
&& super.servant-auth-server.meta.broken or true
then
# It's probably fine. We only use it in a test.
h.dontCheck (h.unmarkBroken super.servant-auth-server)
else super.servant-auth-server;
hercules-ci-agent = lib.pipe super.hercules-ci-agent [
h.justStaticExecutables
(h.addBuildTool pkgs.makeBinaryWrapper)
addCompactUnwind
h.enableDWARFDebugging
(h.addBuildDepends [ pkgs.boost ])
(h.overrideCabal (o: {
postCompileBuildDriver = ''
echo Setup version:
./Setup --version
'';
postInstall = ''
${o.postInstall or ""}
mkdir -p $out/libexec
mv $out/bin/hercules-ci-agent $out/libexec
makeWrapper $out/libexec/hercules-ci-agent $out/bin/hercules-ci-agent --prefix PATH : ${lib.makeBinPath
([ pkgs.gnutar pkgs.gzip pkgs.git pkgs.openssh ]
++ lib.optional pkgs.stdenv.isLinux pkgs.crun)}
'';
passthru = o.passthru or { } // {
inherit nix;
};
}))
(self.generateOptparseApplicativeCompletions [ "hercules-ci-agent" ])
];
hercules-ci-agent_lib = lib.pipe self.hercules-ci-agent
# Don't override this when making a devShell, because Nixpkgs shellFor does not recognize it as a local dependency.
(lib.optionals (!flakeArgs.config.isForDevShell) [
(h.overrideCabal (o: {
isLibrary = true;
isExecutable = false;
postInstall = "";
postFixup = "";
enableSharedExecutables = false;
buildTarget = "lib:hercules-ci-agent hercules-ci-agent-unit-tests";
configureFlags = o.configureFlags or [ ] ++ [
"--bindir=${pkgs.emptyDirectory}/hercules-ci-built-without-binaries/no-bin"
];
# We're undoing justStaticExecutables, to make it a library again,
# so it's ok to have library references, including ghc.
disallowGhcReference = false;
}))
])
;
hercules-ci-cli = lib.pipe super.hercules-ci-cli [
(x: x.override (o: {
hercules-ci-agent = self.hercules-ci-agent_lib;
}))
(h.addBuildTool pkgs.makeBinaryWrapper)
addCompactUnwind
h.disableLibraryProfiling
h.justStaticExecutables
(self.generateOptparseApplicativeCompletions [ "hci" ])
(h.overrideCabal (o:
let binPath = lib.optionals pkgs.stdenv.isLinux [ pkgs.crun ];
in
{
postInstall =
o.postInstall or ""
+ lib.optionalString (binPath != [ ]) ''
wrapProgram $out/bin/hci --prefix PATH : ${lib.makeBinPath binPath}
'';
}
))
];
# FIXME: https://github.com/hercules-ci/hercules-ci-agent/pull/443/files
hercules-ci-cnix-expr = lib.pipe super.hercules-ci-cnix-expr [
(x: x.override (o: { inherit nix; }))
(h.addBuildTool pkgs.git)
];
hercules-ci-cnix-store = lib.pipe super.hercules-ci-cnix-store [
(x: x.override (o: { inherit nix; }))
(x: x.overrideAttrs (o: {
passthru = o.passthru // { nixPackage = nix; };
}))
];
# # Dodge build failures of components we don't need.
# haskell-language-server = h.appendConfigureFlags [ "-f-fourmolu" ] (
# super.haskell-language-server.override {
# hls-fourmolu-plugin = null;
# }
# );
ghcid = (
if system == "aarch64-darwin"
then h.overrideCabal (drv: { enableSeparateBinOutput = false; })
else x: x
) super.ghcid;
ormolu = (
if system == "aarch64-darwin"
then h.overrideCabal (drv: { enableSeparateBinOutput = false; })
else x: x
) super.ormolu;
# make sure to update haskell-flake before re-enabling this (haskell-flake#271)
# releaser = super.callCabal2nix "releaser" (builtins.getFlake "github:hercules-ci/haskell-releaser?rev=e50360ec896fcb6ad724566aece6625973419e8d") { };
};
};
};
packages.hercules-ci-api-swagger =
pkgs.callPackage ./hercules-ci-api/swagger.nix { hercules-ci-api = config.packages.internal-hercules-ci-api; };
packages.hercules-ci-api-openapi3 =
pkgs.callPackage ./nix/openapi3.nix { hercules-ci-api = config.packages.internal-hercules-ci-api; };
packages.hercules-ci-cli = config.packages.internal-hercules-ci-cli;
packages.hci = config.packages.hercules-ci-cli;
packages.hercules-ci-agent = config.packages.internal-hercules-ci-agent;
# packages.hercules-ci-agent-nixUnstable = config.variants.nixUnstable.packages.hercules-ci-agent;
# packages.hercules-ci-cli-nixUnstable = config.variants.nixUnstable.packages.hercules-ci-cli;
devShells.default =
let
# TODO haskell-flake final packages
inherit (pkgs) haskellPackages;
shellWithHaskell = true;
baseShell =
if shellWithHaskell
then config.devShells.internal
else pkgs.mkShell { };
shell = baseShell.overrideAttrs (o: {
NIX_PATH = "nixpkgs=${pkgs.path}";
NIXPKGSBALL = pkgs.callPackage ./tests/nixpkgsball.nix { };
nativeBuildInputs =
o.nativeBuildInputs or [ ] ++ [
pkgs.jq
pkgs.nix-prefetch-git
pkgs.nixpkgs-fmt
# pkgs.haskell.packages.ghc8107.stack
pkgs.haskellPackages.stack
pkgs.pre-commit
# pkgs.valgrind (broken on x86_64-darwin)
] ++ lib.optionals shellWithHaskell [
haskellPackages.haskell-language-server
pkgs.haskellPackages.implicit-hie # gen-hie
pkgs.haskellPackages.ghcid
];
shellHook = ''
${o.shellHook or ""}
if [[ -z "''${IN_LORRI_SHELL:-}" ]]; then
${config.pre-commit.installationScript}
fi
'';
});
in
shell;
checks = config.checkSet
# // suffixAttrs "-nixUnstable" config.variants.nixUnstable.checkSet
;
checkSet =
let
multi-example = pkgs.callPackage ./tests/multi-example.nix { };
in
{
cli = pkgs.callPackage ./tests/cli.nix { hci = config.packages.hercules-ci-cli; };
}
# isx86_64: Don't run the VM tests on aarch64 to save time
// lib.optionalAttrs (pkgs.stdenv.isLinux && pkgs.stdenv.isx86_64)
{
agent-functional-test = pkgs.nixosTest (import ./tests/agent-test.nix { flake = self; daemonIsNixUnstable = false; trusted = true; });
agent-functional-test-untrusted = pkgs.nixosTest (import ./tests/agent-test.nix { flake = self; daemonIsNixUnstable = false; trusted = false; });
# agent-functional-test-daemon-nixUnstable = pkgs.nixosTest (import ./tests/agent-test.nix { flake = self; daemonIsNixUnstable = true; });
multi-example-eq = multi-example.eq;
multi-example-multi = multi-example.multi;
} // lib.optionalAttrs pkgs.stdenv.isDarwin {
nix-darwin-example =
pkgs.callPackage ./tests/nix-darwin-example.nix { flake = self // { inherit inputs; }; };
}
// lib.optionalAttrs (system == "x86_64-linux") {
evalTests =
(import ./tests/default-herculesCI-for-flake-test.nix
{ inherit (nixpkgs) lib; }).test
pkgs.emptyFile;
};
};
options = {
nixpkgsSource = lib.mkOption {
default = inputs.nixpkgs;
};
checkSet = lib.mkOption {
description = "All tests, excluding those from variants.";
};
};
};
# variants.nixUnstable.extraOverlay = final: prev: {
# nix = addDebug inputs.nix.defaultPackage.${prev.stdenv.hostPlatform.system};
# };
variants.forDevShell.isForDevShell = true;
# Take the devShells from the dev variant
flake.devShells = lib.mkIf (!config.isForDevShell) (
lib.mkOverride ((lib.mkForce { }).priority - 1) (
config.variants.forDevShell.flake.devShells
)
);
};
options = {
# Set by variants
extraOverlay = lib.mkOption {
default = _: _: { };
};
isForDevShell = lib.mkOption {
default = false;
description = ''
Whether we're producing a development attribute.
We apply some overrides to fix things up in the context of devShell.
'';
};
};
});
}