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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
|
# Test scenarios for populate: every combination of each source type's
# options, deployed to the local machine, to a remote target as root, and to
# a remote target using sudo, plus scenarios for features spanning sources.
#
# A scenario prepares the target directory, then runs its phases. Each phase
# runs populate, or writeDeploy with a stand-in for nixos-rebuild, expecting
# it to succeed or to fail with a message, and checks the result on the
# target. Phases expecting evaluation to fail get checked here and don't run.
{
# nixpkgs' lib.
lib,
# krops' lib.
krops,
# Function from populate's arguments to the command running populate.
populate,
# krops' writeDeploy.
writeDeploy,
# Where the deploying machine serves /fixtures/www via HTTP.
url,
# URL of the Git repository /fixtures/git/gitsrc.git.
gitUrl,
# Host name of the remote target.
remoteHost,
# Absolute path of age on the deploying machine.
agePath,
# HEAD of /fixtures/gitsrc.
gitHead,
}:
let
inherit (lib)
attrNames concatMap concatStringsSep foldl' isBool mapAttrsToList
optional optionalAttrs optionals optionalString;
prelude = ". ${./lib.sh}\n";
cartesian = attrs: foldl'
(acc: name: concatMap
(combination: map
(value: combination // { ${name} = value; })
attrs.${name})
acc)
[{}]
(attrNames attrs);
flagsName = flags: concatStringsSep "-" (mapAttrsToList
(name: value: name + (if isBool value then (if value then "1" else "0") else value))
flags);
targets = {
local = path: krops.mkTarget "" // { host = "localhost"; inherit path; };
remote = path: {
user = "root"; host = remoteHost; port = null; inherit path;
sudo = false; extraOptions = [];
};
sudo = path: {
user = "deploy"; host = remoteHost; port = null; inherit path;
sudo = true; extraOptions = [];
};
};
alt = { alt.file = "/fixtures/alt"; };
# Builds a scenario from a name, a target kind, and phases with sources.
# Isolated scenarios start with no target directories and a collected store
# on both machines, so no other scenario's store paths or roots stand in for
# missing ones.
scenario = {
name, targetKind, node ? null, isolate ? false,
prepare ? "", prepareDeployer ? null, phases,
}: let
path = "/var/krops/${name}";
target = targets.${targetKind} path;
vars = "T=${path}\n";
# A phase expecting evaluation to fail has a control: sources that
# differ only in what makes evaluation fail, or a function returning them
# for the phase's sources, and that must evaluate. So the phase doesn't
# pass for failing for an unrelated reason, e.g. a misspelt option.
phase = {
sources, prune ? false, deploy ? null,
expect ? "success", control ? null,
check ? "", mutate ? null, checkDeployer ? null, mutateDeployer ? null,
}: let
command = sources: let
args = {
source = krops.evalSource [ sources ];
inherit prune target;
force = true;
};
in
if deploy == null then populate args
else writeDeploy "deploy" (args // { useNixOutputMonitor = false; } // deploy);
evaluates = sources:
(builtins.tryEval (builtins.seq (toString (command sources)) true)).success;
script = text: if text == null then null else prelude + vars + text;
in
if expect == "eval-error" then
if control == null then
throw "scenario ${name}: expecting evaluation to fail needs a control"
else if evaluates sources then
throw "scenario ${name}: populate evaluates, but is expected to fail"
else if !evaluates (if lib.isFunction control then control sources else control) then
throw "scenario ${name}: the control doesn't evaluate either"
else
null
else {
populate = toString (command sources);
expect = if expect == "success" then null else expect.fail;
check = script check;
mutate = script mutate;
checkDeployer = script checkDeployer;
mutateDeployer = script mutateDeployer;
};
in {
inherit name isolate;
node =
if node != null then node
else if targetKind == "local" then "deployer"
else "target";
prepare = prelude + vars + ''
rm -fR "$T"
'' + prepare;
prepareDeployer =
if prepareDeployer == null then null else prelude + vars + prepareDeployer;
phases = lib.filter (x: x != null) (map phase phases);
};
# Same sources twice, so the second phase checks redeploying.
twice = phase: [ phase phase ];
targetKinds = [ "local" "remote" "sudo" ];
# Scenarios for all combinations of options and target kinds. f gets the
# options and the scenario's name, target kind, and target directory, and
# returns the scenario's arguments, or null for invalid combinations.
matrix = type: options: f:
concatMap
(targetKind: lib.filter (x: x != null) (map
(flags: let
name = "${type}-${targetKind}" +
optionalString (flags != {}) "-${flagsName flags}";
args = f flags { inherit name targetKind; path = "/var/krops/${name}"; };
in
if args == null then null
else scenario ({ inherit name targetKind; } // args))
(cartesian options)))
targetKinds;
# Checks that the source at $T/x is a link to a store path, or to the
# subdirectory sub therein, and evaluates the attribute attr of the flake
# there, through the link, or of its generated flake, after collecting
# garbage.
checkFlake = { attr, expected, subdir ? false, generated ? false }: ''
link=$(readlink "$T"/x)
${if subdir then ''
expect_eq subdirectory "$(basename "$link")" sub
link=$(dirname "$link")
'' else ""}
expect_eq "store directory" "$(dirname "$link")" /nix/store
test ! -e "$T"/.populate/flake-uploads || fail "upload directory remains"
gc
expect_eq ${attr} "$(nix_eval ${
if generated then ''"$T"/.populate/flakes/x'' else ''"$T"/x''
}#${attr})" ${lib.escapeShellArg expected}
'';
copyingWithSudo = flags: ctx:
ctx.targetKind == "sudo" && !(flags.fetchOnTarget or false);
# The sources with fetchOnTarget set for all flake and flake-input sources,
# the control for copying flakes to targets using sudo.
withFetchOnTarget = lib.mapAttrs (_: lib.mapAttrs (type: options:
if lib.elem type [ "flake" "flake-input" ]
then options // { fetchOnTarget = true; }
else options));
in
# The remote target must not count as local wherever this gets evaluated.
assert !(krops.isLocalTarget (targets.remote "/"));
{
derivation = matrix "derivation" {} (_: _: {
phases = twice {
sources.x.derivation = ''
derivation {
name = "krops-test";
system = builtins.currentSystem;
builder = "/bin/sh";
args = [ "-c" "echo built > $out" ];
}
'';
check = ''
gc
expect_eq x "$(cat "$T"/x)" built
'';
};
});
file = let
f = flags: _: {
prepare = ''
mkdir -p "$T"/x
echo old > "$T"/x/skip-ex
echo old > "$T"/x/skip-f
echo stale > "$T"/x/stale
echo BBBB > "$T"/x/same
touch -d @1000000000 "$T"/x/same
'';
phases = twice {
sources = {
x.file = {
path = "/fixtures/filesrc";
useChecksum = flags.checksum;
inherit (flags) deleteExcluded;
exclude = optional (flags.exclude or false) "skip-ex";
filters = optional flags.filters { type = "exclude"; pattern = "skip-f"; };
inputs = optionalAttrs flags.inputs { dep.follows = "alt"; };
};
} // optionalAttrs flags.inputs alt;
check = let
excluded = file: set: ''
${if !set then ''
expect_eq ${file} "$(cat "$T"/x/${file})" src
'' else if flags.deleteExcluded then ''
test ! -e "$T"/x/${file} || fail "${file} not deleted"
'' else ''
expect_eq ${file} "$(cat "$T"/x/${file})" old
''}
'';
in ''
expect_eq keep "$(cat "$T"/x/keep)" keep
${excluded "skip-ex" (flags.exclude or false)}
${excluded "skip-f" flags.filters}
${if flags.deleteExcluded then ''
test ! -e "$T"/x/stale || fail "stale not deleted"
'' else ''
expect_eq stale "$(cat "$T"/x/stale)" stale
''}
expect_eq same "$(cat "$T"/x/same)" ${if flags.checksum then "AAAA" else "BBBB"}
'' + optionalString flags.inputs ''
# Inputs not drawn from sources aren't delivered, so this needs the
# network and can't collect garbage before.
expect_eq msg "$(nix_eval_online "$T"/.populate/flakes/x#msg)" top:alt,sub
'';
};
};
in
matrix "file" {
checksum = [ false true ];
deleteExcluded = [ false true ];
filters = [ false true ];
inputs = [ false true ];
} f
++ [
# The deprecated exclude option, once, because it warns.
(scenario (let
args = f {
checksum = false;
deleteExcluded = true;
exclude = true;
filters = false;
inputs = false;
} null;
in args // {
name = "file-local-exclude";
targetKind = "local";
phases = [ (builtins.head args.phases) ];
}))
];
flake = matrix "flake" {
kind = [ "git" "path" "subdir" "remote" ];
fetchOnTarget = [ false true ];
inputs = [ false true ];
} (flags: ctx: let
flake = {
git = "/fixtures/top";
path = "path:/fixtures/top";
subdir = "git+file:/fixtures/mono?dir=sub";
remote = "${url}/remote.tar.gz";
}.${flags.kind};
msg = {
git = "top:${if flags.inputs then "alt" else "dep+inner"},sub";
path = msg.git;
remote = "remote:${if flags.inputs then "alt" else "dep+inner"}";
};
in
# The flake in a subdirectory has no inputs to draw from sources.
if flags.kind == "subdir" && flags.inputs then null else {
isolate = true;
phases = twice ({
sources = {
x.flake = {
inherit flake;
inherit (flags) fetchOnTarget;
inputs = optionalAttrs flags.inputs { dep.follows = "alt"; };
};
} // optionalAttrs flags.inputs alt;
} // (if copyingWithSudo flags ctx then {
expect = "eval-error";
control = withFetchOnTarget;
} else {
check = if flags.kind == "subdir" then checkFlake {
attr = "who"; expected = "sub"; subdir = true;
} else checkFlake {
attr = "msg"; expected = msg.${flags.kind}; generated = flags.inputs;
};
}));
});
flake-input = concatMap
(name: matrix "flake-input-${lib.replaceStrings ["/"] ["."] name}" ({
fetchOnTarget = [ false true ];
} // optionalAttrs (name == "dep") {
inputs = [ false true ];
}) (flags: ctx: let
inputs = flags.inputs or false;
in {
isolate = true;
phases = twice ({
sources = {
x.flake-input = {
flake = "/fixtures/top";
inherit name;
inherit (flags) fetchOnTarget;
inputs = optionalAttrs inputs { inner.follows = "alt"; };
};
} // optionalAttrs inputs alt;
} // (if copyingWithSudo flags ctx then {
expect = "eval-error";
control = withFetchOnTarget;
} else {
check = {
dep = checkFlake {
attr = "greeting";
expected = if inputs then "dep+alt" else "dep+inner";
generated = inputs;
};
"dep/inner" = checkFlake { attr = "who"; expected = "inner"; };
sub = checkFlake { attr = "who"; expected = "sub"; subdir = true; };
}.${name};
}));
}))
[ "dep" "dep/inner" "sub" ];
git = matrix "git" {
shallow = [ false true ];
fetchAlways = [ false true ];
cleanExclude = [ false true ];
inputs = [ false true ];
} (flags: _: let
check = ''
expect_eq HEAD "$(git -C "$T"/x rev-parse HEAD)" ${gitHead}
expect_eq commits "$(git -C "$T"/x rev-list --count HEAD)" ${
if flags.shallow then "1" else "2"
}
test -e "$T"/x/two || fail "two missing"
'' + optionalString flags.inputs ''
gc
expect_eq msg "$(nix_eval "$T"/.populate/flakes/x#msg)" git:alt
'';
sources = {
x.git = {
url = gitUrl;
ref = gitHead;
inherit (flags) shallow fetchAlways;
clean.exclude = optional flags.cleanExclude "keepme";
inputs = optionalAttrs flags.inputs { dep.follows = "alt"; };
};
} // optionalAttrs flags.inputs alt;
in {
phases = [
{
inherit sources check;
mutate = ''
echo junk > "$T"/x/junk
echo keepme > "$T"/x/keepme
'';
}
{
inherit sources;
check = check + ''
test ! -e "$T"/x/junk || fail "junk not cleaned"
${if flags.cleanExclude then ''
test -e "$T"/x/keepme || fail "keepme cleaned"
'' else ''
test ! -e "$T"/x/keepme || fail "keepme not cleaned"
''}
'';
}
];
});
pass = matrix "pass" {
store = [ "pass" "pass-git" ];
} (flags: _: {
phases = twice {
sources.x.pass = { dir = "/fixtures/${flags.store}"; name = "x"; };
check = ''
expect_eq a "$(cat "$T"/x/a)" secret-a
'';
};
});
passage = matrix "passage" {
age = [ "default" "path" ];
dir = [ "store" "subdir" ];
} (flags: _: {
phases = twice {
sources.x.passage = {
dir = "/fixtures/passage/store" + optionalString (flags.dir == "subdir") "/x";
identities_file = "/fixtures/passage/identities";
} // optionalAttrs (flags.age == "path") {
age = agePath;
};
check = ''
expect_eq a "$(cat "$T"/x/${optionalString (flags.dir == "store") "x/"}a)" secret-a
'';
};
});
pipe = matrix "pipe" {} (_: _: {
phases = twice {
sources.x.pipe = "/fixtures/pipe";
check = ''
expect_eq x "$(cat "$T"/x)" piped
'';
};
});
symlink = matrix "symlink" {
absolute = [ false true ];
inputs = [ false true ];
} (flags: ctx: let
target = optionalString flags.absolute "${ctx.path}/" + "topcopy";
in {
phases = twice {
sources = {
x.symlink = {
inherit target;
inputs = optionalAttrs flags.inputs { dep.follows = "alt"; };
};
topcopy.file = "/fixtures/filesrc";
} // optionalAttrs flags.inputs alt;
check = ''
expect_eq link "$(readlink "$T"/x)" ${target}
test -e "$T"/x/flake.nix || fail "flake.nix missing"
'' + optionalString flags.inputs ''
expect_eq msg "$(nix_eval_online "$T"/.populate/flakes/x#msg)" top:alt,sub
'';
};
});
# writeDeploy's arguments to nixos-rebuild, which a stand-in records.
deploy = matrix "deploy" {
flake = [ "none" "source" "generated" ];
etcFlake = [ false true ];
buildTarget = [ false true ];
} (flags: ctx: let
target = targets.${ctx.targetKind} ctx.path;
buildTarget = targets.local "${ctx.path}-build";
build = if flags.buildTarget then buildTarget else target;
expected = [ "switch" ]
++ optionals flags.buildTarget [
"--build-host" "${buildTarget.user}@${buildTarget.host}"
"--target-host" "${target.user}@${target.host}"
]
++ optional (flags.buildTarget && target.sudo) "--use-remote-sudo"
++ {
none = [];
source = [ "--flake" "${build.path}/x" ];
# Generated flakes as path:, which an enclosing Git work tree doesn't
# capture.
generated = [ "--flake" "path:${build.path}/.populate/flakes/x" ];
}.${flags.flake}
++ optional (flags.etcFlake && flags.flake == "none") "--no-flake";
in
# The local target can't have another build target.
if flags.buildTarget && ctx.targetKind == "local" then null else {
node = if flags.buildTarget then "deployer" else null;
# Each scenario creates or removes /etc/nixos/flake.nix.
prepare = ''
rm -f /tmp/nixos-rebuild.log
${if flags.etcFlake then ''
mkdir -p /etc/nixos
touch /etc/nixos/flake.nix
'' else ''
rm -f /etc/nixos/flake.nix
''}
'';
phases = twice {
sources =
if flags.flake == "none" then {
x.file = "/fixtures/alt";
} else alt // {
x.flake = {
flake = "/fixtures/top";
fetchOnTarget = ctx.targetKind == "sudo";
inputs = optionalAttrs (flags.flake == "generated") {
dep.follows = "alt";
};
};
};
deploy = {
flake = if flags.flake == "none" then null else "x";
} // optionalAttrs flags.buildTarget {
inherit buildTarget;
};
check = ''
expect_eq user "$(sed -n 1p /tmp/nixos-rebuild.log)" 0
expect_eq NIX_PATH "$(sed -n 2p /tmp/nixos-rebuild.log)" ${build.path}
expect_eq arguments "$(sed -n 3p /tmp/nixos-rebuild.log)" \
${lib.escapeShellArg (concatStringsSep " " expected)}
'';
};
});
# Features spanning several sources.
features = concatMap (targetKind: let
s = name: args: scenario ({
name = "${name}-${targetKind}";
inherit targetKind;
isolate = true;
} // args);
copying = targetKind != "sudo";
fetchOnTarget = !copying;
in [
# A generated flake draws an input from a source with a generated flake.
(s "chain" {
phases = twice {
sources = alt // {
a.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; inputs.dep.follows = "b"; };
b.flake-input = {
flake = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
inputs.inner.follows = "alt";
};
};
check = ''
gc
expect_eq msg "$(nix_eval "$T"/.populate/flakes/a#msg)" top:dep+alt,sub
'';
};
})
# An input of an input drawn from a source gets overridden at that source.
(s "nested" {
phases = twice {
sources = alt // {
x.flake = {
flake = "/fixtures/top"; inherit fetchOnTarget;
inputs.dep = { follows = "b"; inputs.inner.follows = "alt"; };
};
b.flake-input = { flake = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
};
check = ''
gc
expect_eq msg "$(nix_eval "$T"/.populate/flakes/x#msg)" top:dep+alt,sub
'';
};
})
# A generated flake gets removed once its source draws no inputs anymore.
(s "removal" {
phases = [
{
sources = alt // {
x.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; inputs.dep.follows = "alt"; };
};
check = ''
test -e "$T"/.populate/flakes/x/flake.lock || fail "generated flake missing"
'';
}
{
sources = alt // {
x.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; };
};
check = ''
test ! -e "$T"/.populate/flakes/x || fail "generated flake remains"
'';
}
];
})
# Roots follow the delivered input.
(s "retarget" {
phases = [
{
sources.x.flake-input = { flake = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
check = ''
expect_eq roots "$(ls "$T"/.populate/flake-inputs/x | wc -l)" 1
'';
}
{
sources.x.flake-input = { flake = "/fixtures/top"; name = "sub"; inherit fetchOnTarget; };
check = ''
test ! -e "$T"/.populate/flake-inputs/x || fail "roots remain"
'' + checkFlake { attr = "who"; expected = "sub"; subdir = true; };
}
];
})
# Pruning removes everything sources don't deliver anymore, including
# garbage collector roots, and keeps everything else.
(s "prune" (let
sources = {
a.file = "/fixtures/alt";
b.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; };
c.flake-input = { flake = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
d.symlink = "a";
};
in {
prepare = ''
mkdir -p "$T"
echo other > "$T"/other
'';
phases = [
{
inherit sources;
check = ''
test -e "$T"/.populate/flake-inputs/b || fail "roots of b missing"
test -e "$T"/.populate/flake-inputs/c || fail "roots of c missing"
'';
}
{
# Without pruning, nothing gets removed.
sources = { inherit (sources) a; };
check = ''
for name in b c d other .populate/flake-inputs/b .populate/flake-inputs/c; do
test -e "$T"/$name || fail "$name removed"
done
'';
}
{
# b changes its type, so its roots go, while b stays.
sources = { inherit (sources) a; b.symlink = "a"; };
prune = true;
check = ''
test -e "$T"/a/flake.nix || fail "a removed"
expect_eq b "$(readlink "$T"/b)" a
test -d "$T"/.populate || fail ".populate removed"
for name in c d other .populate/flake-inputs; do
test ! -e "$T"/$name || fail "$name not pruned"
done
if nix-store --gc --print-roots 2>/dev/null | grep -F "$T/"; then
fail "garbage collector roots remain"
fi
'';
}
];
}))
(s "cycle" {
phases = [{
sources = {
a.file = { path = "/fixtures/alt"; inputs.foo.follows = "b"; };
b.file = { path = "/fixtures/alt"; inputs.foo.follows = "a"; };
};
expect = "eval-error";
control = {
a.file = { path = "/fixtures/alt"; inputs.foo.follows = "b"; };
b.file = "/fixtures/alt";
};
}];
})
(s "self" {
phases = [{
sources.a.file = { path = "/fixtures/alt"; inputs.foo.follows = "a"; };
expect = "eval-error";
control = alt // {
a.file = { path = "/fixtures/alt"; inputs.foo.follows = "alt"; };
};
}];
})
(s "missing" {
phases = [{
sources.a.file = { path = "/fixtures/alt"; inputs.foo.follows = "nope"; };
expect = "eval-error";
control = {
a.file = { path = "/fixtures/alt"; inputs.foo.follows = "nope"; };
nope.file = "/fixtures/alt";
};
}];
})
(s "noflake" {
phases = [{
sources = alt // {
x.file = { path = "/fixtures/noflake"; inputs.foo.follows = "alt"; };
};
expect.fail = "error: source x draws flake inputs from other sources, but contains no flake.nix";
}];
})
(s "noinput" {
phases = [{
sources.x.flake-input = { flake = "/fixtures/top"; name = "nope"; inherit fetchOnTarget; };
expect.fail = "error: flake /fixtures/top has no input nope";
}];
})
# An input locked to a path the target doesn't have.
(s "gone" {
phases = [{
sources.x.flake = { flake = "/fixtures/usegone"; fetchOnTarget = true; };
expect.fail = "error: flake /fixtures/usegone: input gone refers to /fixtures/gone, which does not exist on the target";
}];
})
]) targetKinds;
# Edge cases of the flake source types and of generated flakes: unusual lock
# files, references, and changes between deploys.
edge-cases = concatMap (targetKind: let
s = name: args: scenario ({
name = "${name}-${targetKind}";
inherit targetKind;
isolate = true;
} // args);
copying = targetKind != "sudo";
fetchOnTarget = !copying;
roots = n: ''
expect_eq roots "$(ls "$T"/.populate/flake-inputs/x 2>/dev/null | wc -l)" ${toString n}
'';
in [
# The registry entry kropsremote, which fixtures.sh adds on the deploying
# machine, points at ${url}/remote.tar.gz. The deploying machine has no
# global registry, which it couldn't download.
(s "indirect" {
phases = twice {
sources.x.flake = { flake = "kropsremote"; inherit fetchOnTarget; };
check = checkFlake { attr = "msg"; expected = "remote:dep+inner"; };
};
})
(s "remote-subdir" {
phases = twice {
sources.x.flake = { flake = "${url}/mono.tar.gz?dir=sub"; inherit fetchOnTarget; };
check = checkFlake { attr = "who"; expected = "sub"; subdir = true; };
};
})
(s "subdir-inputs" {
phases = twice {
sources = alt // {
x.flake = {
flake = "git+file:/fixtures/monoin?dir=sub"; inherit fetchOnTarget;
inputs.dep.follows = "alt";
};
};
check = checkFlake { attr = "msg"; expected = "monoin:alt"; subdir = true; generated = true; };
};
})
(s "dirty" {
phases = twice {
sources.x.flake = { flake = "/fixtures/dirty"; inherit fetchOnTarget; };
check = checkFlake { attr = "msg"; expected = "dirty:dep+inner,sub"; } + ''
test ! -e "$T"/x/untracked || fail "untracked file delivered"
'';
};
})
(s "unlocked" {
phases = [{
sources.x.flake = { flake = "/fixtures/unlocked"; inherit fetchOnTarget; };
expect.fail = "requires lock file changes";
}];
})
(s "notdir" {
phases = [{
sources = {
x.file = { path = "/fixtures/filesrc"; inputs.dep.follows = "p"; };
p.pipe = "/fixtures/pipe";
};
expect.fail = "error: source p is not a directory";
}];
})
(s "noinput-nested" {
phases = [{
sources.x.flake-input = { flake = "/fixtures/top"; name = "dep/nope"; inherit fetchOnTarget; };
expect.fail = "error: flake /fixtures/top has no input dep/nope";
}];
})
(s "shared" {
phases = twice {
sources = {
x.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; };
dep.flake-input = { flake = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
sub.flake-input = { flake = "/fixtures/top"; name = "sub"; fetchOnTarget = true; };
};
check = checkFlake { attr = "msg"; expected = "top:dep+inner,sub"; } + ''
expect_eq dep "$(nix_eval "$T"/dep#greeting)" dep+inner
expect_eq sub "$(nix_eval "$T"/sub#who)" sub
'';
};
})
(s "follows-empty" {
phases = twice {
sources.x.flake = { flake = "/fixtures/nofollow"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "nofollow"; };
};
})
(s "follows-cycle" {
phases = twice {
sources.x.flake = { flake = "/fixtures/cyc"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "cyc:cyca,cycb"; };
};
})
# Names resolve through the parent's lock file, so dep/inner is the
# parent's inner, which dep's inner follows.
(s "follows-name" {
phases = twice {
sources.x.flake-input = { flake = "/fixtures/follow"; name = "dep/inner"; inherit fetchOnTarget; };
check = checkFlake { attr = "who"; expected = "inner2"; };
};
})
# The delivered input comes with the inputs its own lock file pins, which
# evaluating it needs, not with the parent's inner, which it follows there.
(s "follows-closure" {
phases = twice {
sources.x.flake-input = { flake = "/fixtures/follow"; name = "dep"; inherit fetchOnTarget; };
check = checkFlake { attr = "greeting"; expected = "dep+inner"; } + roots 1;
};
})
(s "relative-input" {
phases = twice {
sources.x.flake = { flake = "/fixtures/rel"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "rel:lib"; };
};
})
(s "flake-to-file" {
phases = [
{
sources.x.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; };
check = roots 3;
mutate = ''
readlink "$T"/x > "$T"/old
'';
}
{
sources.x.file = "/fixtures/alt";
check = ''
test ! -L "$T"/x || fail "x is still a link to $(readlink "$T"/x)"
test -e "$T"/x/flake.nix || fail "flake.nix missing"
nix-store --verify-path "$(cat "$T"/old)" || fail "store path changed"
'' + roots 0;
}
];
})
(s "symlink-to-flake" {
phases = [
{
sources = alt // { x.symlink = "alt"; };
check = ''
expect_eq link "$(readlink "$T"/x)" alt
'';
}
{
sources = alt // { x.flake = { flake = "/fixtures/top"; inherit fetchOnTarget; }; };
check = checkFlake { attr = "msg"; expected = "top:dep+inner,sub"; } + ''
test -e "$T"/alt/flake.nix || fail "alt changed"
'';
}
];
})
# The ssh master stops, after failing as well as after succeeding.
(s "ssh-master" (let
checkDeployer = ''
for i in 1 2 3 4 5 6 7 8 9 10; do
if ! pgrep -f 'ssh: .*/populate\..*\[mux\]' >/dev/null; then
exit 0
fi
sleep 0.5
done
fail "ssh master remains"
'';
in {
phases = [
{
sources.x.flake-input = { flake = "/fixtures/top"; name = "nope"; inherit fetchOnTarget; };
expect.fail = "error: flake /fixtures/top has no input nope";
inherit checkDeployer;
}
{
sources = alt;
inherit checkDeployer;
}
];
}))
(s "override-generated" {
phases = twice {
sources = {
x.flake = {
flake = "/fixtures/top"; inherit fetchOnTarget;
inputs.dep = { follows = "b"; inputs.q.follows = "c2"; };
};
b.file = { path = "/fixtures/two"; inputs.p.follows = "c1"; };
c1.file = "/fixtures/c1";
c2.file = "/fixtures/c2";
};
check = ''
expect_eq msg "$(nix_eval_online "$T"/.populate/flakes/x#msg)" top:two:c1+c2,sub
'';
};
})
(s "nonflake-input" {
phases = twice {
sources = {
x.file = { path = "/fixtures/usedata"; inputs.data.follows = "data"; };
data.file = "/fixtures/data";
};
check = ''
expect_eq msg "$(nix_eval "$T"/.populate/flakes/x#msg)" data:hello
'';
};
})
# The target directory in a Git work tree.
(s "gitdir" {
prepare = ''
mkdir -p "$T"
git -C "$T" init -q
'';
phases = twice {
sources = alt // {
x.file = { path = "/fixtures/filesrc"; inputs.dep.follows = "alt"; };
};
check = ''
# Not just the path, which Nix takes for the enclosing work tree.
expect_eq msg "$(nix_eval_online path:"$T"/.populate/flakes/x#msg)" top:alt,sub
'';
};
})
# Output of the same length, which rsync's quick check doesn't notice.
(s "pipe-change" {
prepareDeployer = ''
echo piped > /fixtures/pipe2-data
'';
phases = [
{
sources.x.pipe = "/fixtures/pipe2";
check = ''
expect_eq x "$(cat "$T"/x)" piped
'';
mutateDeployer = ''
echo PIPED > /fixtures/pipe2-data
'';
}
{
sources.x.pipe = "/fixtures/pipe2";
check = ''
expect_eq x "$(cat "$T"/x)" PIPED
'';
mutateDeployer = ''
echo piped > /fixtures/pipe2-data
'';
}
];
})
(s "source-name" {
phases = twice {
sources = {
"alt-1.0".file = "/fixtures/alt";
x.file = { path = "/fixtures/filesrc"; inputs.dep.follows = "alt-1.0"; };
};
check = ''
expect_eq msg "$(nix_eval_online "$T"/.populate/flakes/x#msg)" top:alt,sub
'';
};
})
] ++ lib.optional (targetKind == "local")
# The target has the path the input is locked to.
(s "uselocal" {
phases = twice {
sources.x.flake = { flake = "/fixtures/uselocal"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "uselocal:alt"; };
};
})
) targetKinds;
}
|