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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
|
# 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, and its parent.
gitHead,
gitFirst,
# Version of rsync on the deploying machine.
rsyncVersion,
}:
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"; };
# How a source draws inputs from other sources: not at all, by rewriting
# the lock file of its flake, or by a generated flake.
inputModes = [ "none" "rewrite" "generated" ];
# The options of a source drawing inputs in mode.
drawInputs = mode: inputs: optionalAttrs (mode != "none") ({
inherit inputs;
} // optionalAttrs (mode == "generated") {
rewriteLock = false;
});
# The flake of source x that has its inputs drawn in mode.
flakeOf = mode:
if mode == "generated" then ''"$T"/.populate/flakes/x'' else ''"$T"/x'';
# 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.
# targetPath deploys to another path than the scenario's target
# directory, and controlTargetPath is the control for a path that makes
# evaluation fail. env sets environment variables for populate, as
# arguments of env. interrupt ends populate after that many seconds,
# which it's expected not to finish before.
phase = {
sources, backup ? false, prune ? false, deploy ? null,
targetPath ? path, env ? "", interrupt ? null,
expect ? "success", control ? null, controlTargetPath ? null,
check ? "", mutate ? null, checkDeployer ? null, mutateDeployer ? null,
}: let
command = sources: targetPath: let
args = {
source = krops.evalSource [ sources ];
inherit backup prune;
target = target // { path = targetPath; };
force = true;
};
in
if deploy == null then populate args
else writeDeploy "deploy" (args // { useNixOutputMonitor = false; } // deploy);
evaluates = sources: targetPath:
(builtins.tryEval
(builtins.seq (toString (command sources targetPath)) true)).success;
script = text: if text == null then null else prelude + vars + text;
in
if expect == "eval-error" then
if control == null && controlTargetPath == null then
throw "scenario ${name}: expecting evaluation to fail needs a control"
else if evaluates sources targetPath then
throw "scenario ${name}: populate evaluates, but is expected to fail"
else if !(
if controlTargetPath != null then evaluates sources controlTargetPath
else evaluates
(if lib.isFunction control then control sources else control)
targetPath
) then
throw "scenario ${name}: the control doesn't evaluate either"
else
null
else {
populate = toString (command sources targetPath);
inherit env interrupt;
expect = if expect == "success" then null else expect.fail;
# No phase gets around checking the canaries.
check = script (check + "\ncanary_check\n");
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;
# For all machines: before the scenario, and after it for the ones its
# checks don't run on.
canarySetup = prelude + "canary_setup\n";
canaryCheck = prelude + "canary_check\n";
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, or a writable directory if materialized, and
# evaluates the attribute attr of the flake there, or of its generated
# flake, after collecting garbage.
checkFlake = {
attr, expected, subdir ? false, generated ? false, materialized ? false,
}: (if materialized then ''
test -d "$T"/x && ! test -L "$T"/x || fail "x isn't a directory"
test -w "$T"/x/flake.nix || fail "flake.nix isn't writable"
'' else ''
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"; };
} // drawInputs flags.inputs { dep.follows = "alt"; };
} // optionalAttrs (flags.inputs != "none") 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 != "none") ''
# 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 ${flakeOf flags.inputs}#msg)" top:alt,sub
'';
};
};
in
matrix "file" {
checksum = [ false true ];
deleteExcluded = [ false true ];
filters = [ false true ];
inputs = inputModes;
} f
++ [
# The deprecated exclude option, once, because it warns.
(scenario (let
args = f {
checksum = false;
deleteExcluded = true;
exclude = true;
filters = false;
inputs = "none";
} null;
in args // {
name = "file-local-exclude";
targetKind = "local";
phases = [ (builtins.head args.phases) ];
}))
]
# A source whose .rsync-filter rsync 3.5.0 and later can't read, because
# it's reached through a symlink that neither root nor the user deploying
# owns. (Running as root, the scenarios can't use the other common
# cause, a directory on the way that the user may enter but not read.)
++ concatMap (targetKind: map (insecureLinks: let
affected = lib.versionAtLeast rsyncVersion "3.5.0";
excluded = !affected || insecureLinks == true;
in scenario {
name = "file-${targetKind}-insecureLinks" +
(if insecureLinks == null then "Null" else if insecureLinks then "True" else "False");
inherit targetKind;
prepare = ''
mkdir -p "$T"/x
echo old > "$T"/x/garbage
'';
phases = twice ({
sources.x.file = { path = "/fixtures/filtered-link"; inherit insecureLinks; };
} // (if affected && insecureLinks == null then {
expect.fail = "can't read /fixtures/filtered-link/.rsync-filter, so it would sync /fixtures/filtered-link/ without it.";
} else {
check = ''
expect_eq keep "$(cat "$T"/x/keep)" keep
${if excluded then ''
test ! -e "$T"/x/garbage || fail "garbage not excluded"
'' else ''
expect_eq garbage "$(cat "$T"/x/garbage)" garbage
''}
'';
}));
}) [ null true false ]) targetKinds
++ concatMap (targetKind: let
affected = lib.versionAtLeast rsyncVersion "3.5.0";
unreadable = name: { path, file, dir ? "x", env ? "" }: scenario {
name = "file-${targetKind}-insecureLinks${name}";
inherit targetKind;
prepare = ''
mkdir -p "$T"/${dir}
echo old > "$T"/${dir}/garbage
'';
phases = twice ({
sources.x.file = { inherit path; };
inherit env;
} // (if affected then {
expect.fail = "can't read ${file}, so it would sync ${path}/ without it.";
} else {
check = ''
expect_eq keep "$(cat "$T"/${dir}/keep)" keep
test ! -e "$T"/${dir}/garbage || fail "garbage not excluded"
'';
}));
};
in [
# The filter file is in the directory above what gets synced, where it
# applies as well.
(unreadable "Parent" {
path = "/fixtures/filtered-parent-link/sub";
file = "/fixtures/filtered-parent-link/.rsync-filter";
})
# The filter file is in a directory whose name rsync prints escaped
# in the C locale.
(unreadable "Name" {
path = "/fixtures/filtered-name-link";
file = "/fixtures/filtered-name-link/bär/.rsync-filter";
dir = "x/bär";
env = "LC_ALL=C";
})
]) targetKinds;
flake = matrix "flake" {
kind = [ "git" "path" "subdir" "remote" ];
fetchOnTarget = [ false true ];
inputs = inputModes;
materialize = [ 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 != "none" then "alt" else "dep+inner"},sub";
path = msg.git;
remote = "remote:${if flags.inputs != "none" then "alt" else "dep+inner"}";
};
in
# The flake in a subdirectory has no inputs to draw from sources.
if flags.kind == "subdir" && flags.inputs != "none" then null else {
isolate = true;
phases = twice ({
sources = {
x.flake = {
url = flake;
inherit (flags) fetchOnTarget materialize;
} // drawInputs flags.inputs { dep.follows = "alt"; };
} // optionalAttrs (flags.inputs != "none") 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;
materialized = flags.materialize;
} else checkFlake {
attr = "msg"; expected = msg.${flags.kind};
generated = flags.inputs == "generated";
materialized = flags.materialize;
};
}));
});
flake-input = concatMap
(name: matrix "flake-input-${lib.replaceStrings ["/"] ["."] name}" ({
fetchOnTarget = [ false true ];
materialize = [ false true ];
} // optionalAttrs (name == "dep") {
inputs = inputModes;
}) (flags: ctx: let
inputs = flags.inputs or "none";
in {
isolate = true;
phases = twice ({
sources = {
x.flake-input = {
url = "/fixtures/top";
inherit name;
inherit (flags) fetchOnTarget materialize;
} // drawInputs inputs { inner.follows = "alt"; };
} // optionalAttrs (inputs != "none") alt;
} // (if copyingWithSudo flags ctx then {
expect = "eval-error";
control = withFetchOnTarget;
} else {
check = {
dep = checkFlake {
attr = "greeting";
expected = if inputs != "none" then "dep+alt" else "dep+inner";
generated = inputs == "generated";
materialized = flags.materialize;
};
"dep/inner" = checkFlake {
attr = "who"; expected = "inner"; materialized = flags.materialize;
};
sub = checkFlake {
attr = "who"; expected = "sub"; subdir = true;
materialized = flags.materialize;
};
}.${name};
}));
}))
[ "dep" "dep/inner" "sub" ];
git = matrix "git" {
shallow = [ false true ];
fetchAlways = [ false true ];
cleanExclude = [ false true ];
inputs = inputModes;
} (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 == "generated") ''
grep -q '"git+file:' "$T"/.populate/flakes/x/flake.nix ||
fail "the Git work tree isn't referred to as git+file:"
'' + optionalString (flags.inputs != "none") ''
gc
expect_eq msg "$(nix_eval ${flakeOf flags.inputs}#msg)" git:alt
'';
sources = ref: {
x.git = {
url = gitUrl;
inherit ref;
inherit (flags) shallow fetchAlways;
clean.exclude = optional flags.cleanExclude "keepme";
} // drawInputs flags.inputs { dep.follows = "alt"; };
} // optionalAttrs (flags.inputs != "none") alt;
in {
phases = [
# The older commit first, so deploying HEAD changes the work tree, and
# fetches if fetchAlways is set or the clone is shallow.
{
sources = sources gitFirst;
check = ''
expect_eq HEAD "$(git -C "$T"/x rev-parse HEAD)" ${gitFirst}
expect_eq commits "$(git -C "$T"/x rev-list --count HEAD)" 1
test ! -e "$T"/x/two || fail "two present"
'';
}
{
sources = sources gitHead;
inherit check;
mutate = ''
echo junk > "$T"/x/junk
echo keepme > "$T"/x/keepme
'';
}
{
sources = sources gitHead;
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.
# flake is what gets deployed: nothing, or a flake from a file, git, or
# flake source, or one whose inputs get drawn from a source, by rewriting
# its lock file or through a generated flake.
deploy = matrix "deploy" {
flake = [ "none" "file" "git" "source" "rewrite" "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;
# Without a user, ssh decides.
userHost = target:
optionalString (target.user != null && target.user != "") "${target.user}@" +
target.host;
expected = [ "switch" ]
++ optionals flags.buildTarget [
"--build-host" (userHost buildTarget)
"--target-host" (userHost target)
]
++ optional (flags.buildTarget && target.sudo) "--use-remote-sudo";
# The flake's directory, which gets deployed by its resolved path, as
# path:, so no enclosing Git work tree captures it, or as the Git work
# tree it is.
flakeDir =
if flags.flake == "generated" then ".populate/flakes/x" else "x";
scheme = if flags.flake == "git" then "git+file" else "path";
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 = {
none.x.file = "/fixtures/alt";
file.x.file = "/fixtures/alt";
git.x.git = { url = gitUrl; ref = gitHead; };
}.${flags.flake} or (alt // {
x.flake = {
url = "/fixtures/top";
fetchOnTarget = ctx.targetKind == "sudo";
} // drawInputs (if flags.flake == "source" then "none" else flags.flake) {
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}
expected=${lib.escapeShellArg (concatStringsSep " " expected)}
${if flags.flake == "none" then optionalString flags.etcFlake ''
expected="$expected --no-flake"
'' else ''
expected="$expected --flake ${scheme}:$(readlink -f ${build.path}/${flakeDir})"
''}
expect_eq arguments "$(sed -n 3p /tmp/nixos-rebuild.log)" "$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;
# A scenario for each way of drawing inputs, which f gets as mode, along
# with draw, which adds inputs drawn that way to a source's options, and
# flake, which gives the flake to evaluate for a source.
modes = name: f: map
(mode: s "${name}-${mode}" (f {
draw = drawInputs mode;
flake = n:
if mode == "generated" then ''"$T"/.populate/flakes/${n}'' else ''"$T"/${n}'';
inherit mode;
}))
[ "rewrite" "generated" ];
in lib.flatten [
# A source draws an input from a source that draws inputs itself.
(modes "chain" ({ draw, flake, ... }: {
phases = twice {
sources = alt // {
a.flake = { url = "/fixtures/top"; inherit fetchOnTarget; } // draw { dep.follows = "b"; };
b.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
} // draw { inner.follows = "alt"; };
};
check = ''
gc
expect_eq msg "$(nix_eval ${flake "a"}#msg)" top:dep+alt,sub
'';
};
}))
# An input of an input drawn from a source gets drawn from another
# source.
(modes "nested" ({ draw, flake, ... }: {
phases = twice {
sources = alt // {
x.flake = {
url = "/fixtures/top"; inherit fetchOnTarget;
} // draw { dep = { follows = "b"; inputs.inner.follows = "alt"; }; };
b.flake-input = { url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
};
check = ''
gc
expect_eq msg "$(nix_eval ${flake "x"}#msg)" top:dep+alt,sub
'';
};
}))
# The lock file gets rewritten or generated again when a source inputs
# get drawn from changes.
(modes "relock" ({ draw, flake, ... }: let
x.file = { path = "/fixtures/filesrc"; } // draw { dep.follows = "a"; };
in {
phases = [
{
sources = { inherit x; a.file = "/fixtures/alt"; };
check = ''
expect_eq msg "$(nix_eval_online ${flake "x"}#msg)" top:alt,sub
'';
}
{
sources = { inherit x; a.file = "/fixtures/alt2"; };
check = ''
expect_eq msg "$(nix_eval_online ${flake "x"}#msg)" top:alt2,sub
'';
}
];
}))
# What drawing inputs needs gets removed once a source draws no inputs
# anymore.
(modes "removal" ({ draw, mode, ... }: let
state = {
rewrite = "derived/x/result";
generated = "flakes/x/flake.lock";
}.${mode};
in {
phases = [
{
sources = alt // {
x.flake = { url = "/fixtures/top"; inherit fetchOnTarget; } // draw { dep.follows = "alt"; };
};
check = ''
test -e "$T"/.populate/${state} || fail "${state} missing"
'';
}
{
sources = alt // {
x.flake = { url = "/fixtures/top"; inherit fetchOnTarget; };
};
check = ''
test ! -e "$T"/.populate/${dirOf state} || fail "${dirOf state} remains"
'' + checkFlake { attr = "msg"; expected = "top:dep+inner,sub"; };
}
];
}))
# Patches get applied in order to a copy, which becomes a store path and
# gets made again only when the patches change.
(s "patches" (let
x = patches: {
x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget patches;
};
};
both = x [ "/fixtures/patches/hello.patch" "/fixtures/patches/hello2.patch" ];
in {
phases = [
{
sources = both;
check = checkFlake { attr = "greeting"; expected = "hello2+inner"; };
# The key changes whenever the copy gets made again, which
# the store path doesn't show, being the same.
mutate = ''
readlink "$T"/x > "$T".first
stat -c %y "$T"/.populate/derived/x/key > "$T".key
'';
}
{
sources = both;
check = checkFlake { attr = "greeting"; expected = "hello2+inner"; } + ''
expect_eq "store path" "$(readlink "$T"/x)" "$(cat "$T".first)"
expect_eq "copy made at" "$(stat -c %y "$T"/.populate/derived/x/key)" "$(cat "$T".key)"
'';
}
{
sources = x [ "/fixtures/patches/hello.patch" ];
check = checkFlake { attr = "greeting"; expected = "hello+inner"; } + ''
test "$(readlink "$T"/x)" != "$(cat "$T".first)" || fail "store path unchanged"
expect_eq patches "$(ls "$T"/.populate/patches/x)" 0001-hello.patch
'';
}
{
sources = x [];
check = checkFlake { attr = "greeting"; expected = "dep+inner"; } + ''
test ! -e "$T"/.populate/patches || fail "patches remain"
test ! -e "$T"/.populate/derived || fail "derived remains"
'';
}
];
}))
(s "patch-fails" {
phases = [{
sources.x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
patches = [ "/fixtures/patches/bad.patch" ];
};
expect.fail = "error: patch bad.patch does not apply to source x";
}];
})
# Patches apply in the subdirectory the target path shows.
(s "patch-subdir" {
phases = twice {
sources.x.flake-input = {
url = "/fixtures/top"; name = "sub"; inherit fetchOnTarget;
patches = [ "/fixtures/patches/sub.patch" ];
};
check = checkFlake { attr = "who"; expected = "patched"; subdir = true; };
};
})
# A copy with a rewritten lock file gets made again only when a source
# it draws inputs from changes.
(s "rewrite-cache" (let
x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
inputs.inner.follows = "a";
};
in {
phases = [
{
sources = { inherit x; a.file = "/fixtures/alt"; };
check = checkFlake { attr = "greeting"; expected = "dep+alt"; };
# The key changes whenever the copy gets made again, which
# the store path doesn't show, being the same.
mutate = ''
readlink "$T"/x > "$T".first
stat -c %y "$T"/.populate/derived/x/key > "$T".key
'';
}
{
sources = { inherit x; a.file = "/fixtures/alt"; };
check = checkFlake { attr = "greeting"; expected = "dep+alt"; } + ''
expect_eq "store path" "$(readlink "$T"/x)" "$(cat "$T".first)"
expect_eq "copy made at" "$(stat -c %y "$T"/.populate/derived/x/key)" "$(cat "$T".key)"
'';
}
{
sources = { inherit x; a.file = "/fixtures/alt2"; };
check = checkFlake { attr = "greeting"; expected = "dep+alt2"; } + ''
test "$(readlink "$T"/x)" != "$(cat "$T".first)" || fail "store path unchanged"
'';
}
];
}))
# A materialized input is a writable copy, patched and with a rewritten
# lock file, which gets made again only when what it derives from
# changes, and becomes a link again when not materialized anymore.
(s "materialize" (let
x = patches: alt // {
x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget patches;
materialize = true;
inputs.inner.follows = "alt";
};
};
hello = [ "/fixtures/patches/hello.patch" ];
in {
phases = [
{
sources = x hello;
check = ''
test ! -L "$T"/x || fail "x is a link"
test -w "$T"/x/flake.nix || fail "flake.nix isn't writable"
gc
expect_eq greeting "$(nix_eval "$T"/x#greeting)" hello+alt
'';
mutate = ''
echo local > "$T"/x/local
'';
}
{
sources = x hello;
check = ''
expect_eq local "$(cat "$T"/x/local)" local
'';
}
{
sources = x (hello ++ [ "/fixtures/patches/hello2.patch" ]);
check = ''
test ! -e "$T"/x/local || fail "local remains"
gc
expect_eq greeting "$(nix_eval "$T"/x#greeting)" hello2+alt
'';
}
{
sources = alt // {
x.flake-input = { url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
};
check = checkFlake { attr = "greeting"; expected = "dep+inner"; } + ''
test ! -e "$T"/.populate/derived || fail "derived remains"
'';
}
];
}))
# Roots follow the delivered input.
(s "retarget" {
phases = [
{
sources.x.flake-input = { url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
check = ''
expect_eq roots "$(ls "$T"/.populate/flake-inputs/x | wc -l)" 1
'';
}
{
sources.x.flake-input = { url = "/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 = { url = "/fixtures/top"; inherit fetchOnTarget; };
c.flake-input = { url = "/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 = { url = "/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 = { url = "/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}
'';
# A scenario for each way of drawing inputs, which f gets as mode, along
# with draw, which adds inputs drawn that way to a source's options, and
# flake, which gives the flake to evaluate for a source.
modes = name: f: map
(mode: s "${name}-${mode}" (f {
draw = drawInputs mode;
flake = n:
if mode == "generated" then ''"$T"/.populate/flakes/${n}'' else ''"$T"/${n}'';
inherit mode;
}))
[ "rewrite" "generated" ];
in lib.flatten [
# 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 = { url = "kropsremote"; inherit fetchOnTarget; };
check = checkFlake { attr = "msg"; expected = "remote:dep+inner"; };
};
})
(s "remote-subdir" {
phases = twice {
sources.x.flake = { url = "${url}/mono.tar.gz?dir=sub"; inherit fetchOnTarget; };
check = checkFlake { attr = "who"; expected = "sub"; subdir = true; };
};
})
(modes "subdir-inputs" ({ draw, mode, ... }: {
phases = twice {
sources = alt // {
x.flake = {
url = "git+file:/fixtures/monoin?dir=sub"; inherit fetchOnTarget;
} // draw { dep.follows = "alt"; };
};
check = checkFlake {
attr = "msg"; expected = "monoin:alt"; subdir = true;
generated = mode == "generated";
};
};
}))
(s "dirty" {
phases = twice {
sources.x.flake = { url = "/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 = { url = "/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 = { url = "/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 = { url = "/fixtures/top"; inherit fetchOnTarget; };
dep.flake-input = { url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; };
sub.flake-input = { url = "/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 = { url = "/fixtures/nofollow"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "nofollow"; };
};
})
(s "follows-cycle" {
phases = twice {
sources.x.flake = { url = "/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 = { url = "/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 = { url = "/fixtures/follow"; name = "dep"; inherit fetchOnTarget; };
check = checkFlake { attr = "greeting"; expected = "dep+inner"; } + roots 1;
};
})
(s "relative-input" {
phases = twice {
sources.x.flake = { url = "/fixtures/rel"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "rel:lib"; };
};
})
# The target can't fetch an input of the flake. The deploying machine
# fetched it when locking, so its fetcher cache has to go.
(s "fetch-failure" {
prepareDeployer = ''
rm -fR /root/.cache/nix
'';
phases = [{
sources.x.flake = { url = "/fixtures/broken"; fetchOnTarget = true; };
expect.fail = "vanishing.tar.gz";
}];
})
# The delivered input itself is locked to a path the target lacks.
(s "gone-input" {
phases = [{
sources.x.flake-input = { url = "/fixtures/usegone"; name = "gone"; fetchOnTarget = true; };
expect.fail = "input gone refers to /fixtures/gone, which does not exist on the target";
}];
})
# An input that gets drawn from a source isn't needed on the target, with
# all it pins.
(modes "overridden-roots" ({ draw, mode, ... }: {
phases = twice {
sources = alt // {
x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
} // draw { inner.follows = "alt"; };
};
check = checkFlake {
attr = "greeting"; expected = "dep+alt";
generated = mode == "generated";
} + roots 0;
};
}))
# The input's own lock file pins something unreachable, which gets drawn
# from a source anyway.
(modes "own-lock-unreachable" ({ draw, mode, ... }: {
prepareDeployer = ''
rm -fR /root/.cache/nix
'';
phases = twice {
sources = alt // {
x.flake-input = {
url = "/fixtures/usepinned"; name = "dep"; inherit fetchOnTarget;
} // draw { inner.follows = "alt"; };
};
check = checkFlake {
attr = "greeting"; expected = "pinned+alt";
generated = mode == "generated";
};
};
}))
# The input's own lock file pins something unreachable, or lacks an input
# its flake.nix has. That fails, unless the input isn't required to come
# with its inputs.
(s "own-lock-broken" (let
x = url: requireInputs: {
x.flake-input = { inherit url; name = "dep"; inherit fetchOnTarget requireInputs; };
};
delivered = ''
test -e "$T"/x/flake.nix || fail "input not delivered"
'';
in {
prepareDeployer = ''
rm -fR /root/.cache/nix
'';
phases = [
{
sources = x "/fixtures/usepinned" true;
expect.fail = "error: source x: the inputs its own lock file pins can't be delivered. Set its requireInputs to false to deliver it anyway.";
}
{
sources = x "/fixtures/usepinned" false;
check = delivered + roots 0;
}
{
sources = x "/fixtures/usestale" true;
expect.fail = "error: source x: the inputs its own lock file pins can't be delivered. Set its requireInputs to false to deliver it anyway.";
}
{
sources = x "/fixtures/usestale" false;
check = delivered + roots 2;
}
{
sources = x "/fixtures/usestale" false;
check = delivered + roots 2;
}
];
}))
# Redeploying an unchanged input while where it comes from is
# unreachable and the fetcher caches are gone: the store has it.
(s "offline-redeploy" (let
sources.x.flake-input = { url = "/fixtures/offline"; name = "gap"; inherit fetchOnTarget; };
check = checkFlake { attr = "who"; expected = "gap"; };
in {
prepareDeployer = ''
if test -e /fixtures/www/gap.tar.gz.off; then
mv /fixtures/www/gap.tar.gz.off /fixtures/www/gap.tar.gz
fi
'';
phases = [
{
inherit sources check;
mutate = ''
rm -fR /root/.cache/nix
'';
mutateDeployer = ''
rm -fR /root/.cache/nix
mv /fixtures/www/gap.tar.gz /fixtures/www/gap.tar.gz.off
'';
}
{
inherit sources check;
mutateDeployer = ''
mv /fixtures/www/gap.tar.gz.off /fixtures/www/gap.tar.gz
'';
}
];
}))
(s "flake-to-file" {
phases = [
{
sources.x.flake = { url = "/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 = { url = "/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 = { url = "/fixtures/top"; name = "nope"; inherit fetchOnTarget; };
expect.fail = "error: flake /fixtures/top has no input nope";
inherit checkDeployer;
}
{
sources = alt;
inherit checkDeployer;
}
];
}))
# Pruning entries whose names a shell's read would strip or split.
(s "prune-names" {
prepare = ''
mkdir -p "$T/ lead" "$T/trail " "$T/$(printf 'new\nline')" "$T/axb" "$T/[b]"
'';
phases = [{
sources = alt;
prune = true;
check = ''
# In the C locale, ls sorts by bytes, so . comes first.
expect_eq entries "$(LC_ALL=C ls -A "$T" | tr '\n' ' ')" '.populate alt '
'';
}];
})
# A signal ending populate leaves neither the ssh master nor the
# temporary directory.
(s "interrupt" {
prepareDeployer = ''
find "''${TMPDIR:-/tmp}" -maxdepth 1 -name 'populate.*' -exec rm -fR {} +
'';
phases = [{
sources.x.pipe = "/fixtures/pipe-slow";
interrupt = 3;
checkDeployer = ''
for i in 1 2 3 4 5 6 7 8 9 10; do
if ! pgrep -f 'ssh: .*/populate\..*\[mux\]' >/dev/null; then
break
fi
sleep 0.5
done
! pgrep -f 'ssh: .*/populate\..*\[mux\]' >/dev/null ||
fail "ssh master remains"
expect_eq "temporary directories" \
"$(find "''${TMPDIR:-/tmp}" -maxdepth 1 -name 'populate.*')" ""
'';
}];
})
# An input of an input drawn from a source that draws inputs itself
# gets drawn from another source.
(modes "override" ({ draw, flake, ... }: {
phases = twice {
sources = {
x.flake = {
url = "/fixtures/top"; inherit fetchOnTarget;
} // draw { dep = { follows = "b"; inputs.q.follows = "c2"; }; };
b.file = { path = "/fixtures/two"; } // draw { p.follows = "c1"; };
c1.file = "/fixtures/c1";
c2.file = "/fixtures/c2";
};
check = ''
expect_eq msg "$(nix_eval_online ${flake "x"}#msg)" top:two:c1+c2,sub
'';
};
}))
# The same one level deeper: the input gets drawn from a source below
# an input that the source in between draws from a source itself.
(modes "override-deep" ({ draw, flake, ... }: {
phases = twice {
sources = {
x.flake = {
url = "/fixtures/top"; inherit fetchOnTarget;
} // draw { dep = { follows = "b"; inputs.p.inputs.z.follows = "c2"; }; };
b.file = { path = "/fixtures/two"; } // draw { p.follows = "c3"; };
c2.file = "/fixtures/c2";
c3.file = "/fixtures/c3";
};
check = ''
expect_eq msg "$(nix_eval_online ${flake "x"}#msg)" 'top:two:c3(c2)+inner,sub'
'';
};
}))
# A source with a rewritten lock file draws an input from a source with
# a generated flake, and an input below it from another source.
(s "override-mixed" {
phases = twice {
sources = {
x.flake = {
url = "/fixtures/top"; inherit fetchOnTarget;
inputs.dep = { follows = "b"; inputs.q.follows = "c2"; };
};
b.file = {
path = "/fixtures/two";
rewriteLock = false;
inputs.p.follows = "c1";
};
c1.file = "/fixtures/c1";
c2.file = "/fixtures/c2";
};
check = ''
expect_eq msg "$(nix_eval_online "$T"/x#msg)" top:two:c1+c2,sub
'';
};
})
(modes "nonflake-input" ({ draw, flake, ... }: {
phases = twice {
sources = {
x.file = { path = "/fixtures/usedata"; } // draw { data.follows = "data"; };
data.file = "/fixtures/data";
};
check = ''
expect_eq msg "$(nix_eval ${flake "x"}#msg)" data:hello
'';
};
}))
# The target directory in a Git work tree.
(modes "gitdir" ({ draw, flake, ... }: {
prepare = ''
mkdir -p "$T"
git -C "$T" init -q
'';
phases = twice {
sources = alt // {
x.file = { path = "/fixtures/filesrc"; } // draw { dep.follows = "alt"; };
};
check = ''
# Not just the path, which Nix takes for the enclosing work tree.
expect_eq msg "$(nix_eval_online path:${flake "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
'';
}
];
})
# Source names that aren't input names, or that Nix has another use
# for, both of sources inputs get drawn from and of sources drawing them.
(modes "source-name" ({ draw, flake, ... }: let
x = from: { path = "/fixtures/filesrc"; } // draw { dep.follows = from; };
in {
phases = twice {
sources = {
"alt-1.0".file = "/fixtures/alt";
self.file = "/fixtures/alt";
x.file = x "alt-1.0";
y.file = x "self";
"a.b_c-d".file = x "self";
};
check = ''
expect_eq x "$(nix_eval_online ${flake "x"}#msg)" top:alt,sub
expect_eq y "$(nix_eval_online ${flake "y"}#msg)" top:alt,sub
expect_eq a.b_c-d "$(nix_eval_online ${flake "a.b_c-d"}#msg)" top:alt,sub
'';
};
}))
# An upload an interrupted run left behind.
(s "upload-leftover" {
phases = [
{
sources.x.flake = { url = "/fixtures/top"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "top:dep+inner,sub"; };
mutate = ''
mkdir -p "$T"/.populate/flake-uploads/x/tree
echo half > "$T"/.populate/flake-uploads/x/tree/half
'';
}
{
sources.x.flake = { url = "/fixtures/top"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "top:dep+inner,sub"; };
}
];
})
] ++ lib.optional (targetKind == "local")
# The target has the path the input is locked to.
(s "uselocal" {
phases = twice {
sources.x.flake = { url = "/fixtures/uselocal"; fetchOnTarget = true; };
check = checkFlake { attr = "msg"; expected = "uselocal:alt"; };
};
})
) targetKinds;
# Populate replaces and removes things in the target directory. These
# scenarios check that it doesn't anywhere else: names and paths that lead
# out of the target directory don't evaluate, and symlinks in it get
# replaced or refused instead of followed. The canaries, which every phase
# of every scenario checks, tell if something outside changed; these
# scenarios point links at them.
safety = concatMap (targetKind: let
s = name: args: scenario ({
name = "${name}-${targetKind}";
inherit targetKind;
} // args);
copying = targetKind != "sudo";
fetchOnTarget = !copying;
outside = "/var/krops-outside";
hello = [ "/fixtures/patches/hello.patch" ];
# Sources that use every kind of state populate keeps: x a copy with
# patches, y garbage collector roots and, where the flake gets uploaded,
# an upload, and z a generated flake.
stateful = alt // {
x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
patches = hello;
inputs.inner.follows = "alt";
};
y.flake = { url = "/fixtures/top"; inherit fetchOnTarget; };
z.file = {
path = "/fixtures/filesrc";
rewriteLock = false;
inputs.dep.follows = "alt";
};
};
# A source deployed where a symlink to something outside is. The link
# gets replaced, never followed.
link = type: { sources, to, check }: s "link-${type}" {
isolate = true;
prepare = ''
mkdir -p "$T"
ln -s ${outside}/${to} "$T"/x
'';
phases = twice {
inherit sources;
check = ''
case $(readlink "$T"/x || :) in
${outside}/*) fail "x still links outside" ;;
esac
'' + check;
};
};
# Something of populate's state replaced by a symlink to outside between
# two deploys. The second one refuses to use it.
state = name: {
entry, backup ? false,
message ? "/${entry} is a symlink, but populate needs a directory of its own there",
}: s "state-${name}" {
isolate = true;
phases = [
{
sources = stateful;
inherit backup;
mutate = ''
rm -fR "$T"/${entry}
mkdir -p "$(dirname "$T"/${entry})"
ln -s ${outside}/dir "$T"/${entry}
'';
}
{
sources = stateful;
inherit backup;
expect.fail = message;
}
];
};
in lib.flatten [
(link "file" {
sources.x.file = "/fixtures/alt";
to = "dir";
check = ''
test -e "$T"/x/flake.nix || fail "flake.nix missing"
'';
})
# The link points at a Git work tree, which resetting and cleaning would
# change.
(link "git" {
sources.x.git = { url = gitUrl; ref = gitHead; };
to = "repo";
check = ''
expect_eq HEAD "$(git -C "$T"/x rev-parse HEAD)" ${gitHead}
'';
})
(link "pass" {
sources.x.pass = { dir = "/fixtures/pass"; name = "x"; };
to = "dir";
check = ''
expect_eq a "$(cat "$T"/x/a)" secret-a
'';
})
(link "passage" {
sources.x.passage = {
dir = "/fixtures/passage/store/x";
identities_file = "/fixtures/passage/identities";
};
to = "dir";
check = ''
expect_eq a "$(cat "$T"/x/a)" secret-a
'';
})
(link "pipe" {
sources.x.pipe = "/fixtures/pipe";
to = "file";
check = ''
expect_eq x "$(cat "$T"/x)" piped
'';
})
(link "derivation" {
sources.x.derivation = ''
derivation {
name = "krops-test";
system = builtins.currentSystem;
builder = "/bin/sh";
args = [ "-c" "echo built > $out" ];
}
'';
to = "file";
check = ''
expect_eq x "$(cat "$T"/x)" built
'';
})
(link "symlink" {
sources = alt // { x.symlink = "alt"; };
to = "dir";
check = ''
expect_eq link "$(readlink "$T"/x)" alt
'';
})
(link "flake" {
sources.x.flake = { url = "/fixtures/top"; inherit fetchOnTarget; };
to = "dir";
check = checkFlake { attr = "msg"; expected = "top:dep+inner,sub"; };
})
(link "flake-materialized" {
sources.x.flake = {
url = "/fixtures/top"; inherit fetchOnTarget;
materialize = true;
};
to = "dir";
check = checkFlake {
attr = "msg"; expected = "top:dep+inner,sub"; materialized = true;
};
})
(link "flake-input-patched" {
sources.x.flake-input = {
url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget;
patches = hello;
};
to = "dir";
check = checkFlake { attr = "greeting"; expected = "hello+inner"; };
})
# Symlinks to outside in what a source replaces: rsync and Git remove
# them, and so does replacing a directory by a link.
(s "inner-links" (let
prepare = ''
for name in a b c; do
mkdir -p "$T"/$name
ln -s ${outside}/dir "$T"/$name/keep
ln -s ${outside}/dir "$T"/$name/stale
ln -s ${outside}/repo/.git "$T"/$name/.git
done
# A work tree whose repository is elsewhere.
mkdir -p "$T"/d
echo "gitdir: ${outside}/repo/.git" > "$T"/d/.git
'';
in {
isolate = true;
inherit prepare;
phases = twice {
sources = {
a.file = "/fixtures/filesrc";
b.git = { url = gitUrl; ref = gitHead; };
c.flake = { url = "/fixtures/top"; inherit fetchOnTarget; };
d.git = { url = gitUrl; ref = gitHead; };
};
check = ''
expect_eq keep "$(cat "$T"/a/keep)" keep
test ! -L "$T"/a/stale || fail "a/stale remains"
expect_eq HEAD "$(git -C "$T"/b rev-parse HEAD)" ${gitHead}
test ! -L "$T"/b/.git || fail "b/.git remains a link"
test ! -L "$T"/b/stale || fail "b/stale remains"
expect_eq HEAD "$(git -C "$T"/d rev-parse HEAD)" ${gitHead}
test -d "$T"/d/.git || fail "d/.git isn't a directory"
expect_eq "store directory" "$(dirname "$(readlink "$T"/c)")" /nix/store
'';
};
}))
(state "sentinel" {
entry = ".populate";
message = "error: sentinel file is a symlink";
})
(state "backup" { entry = ".populate/backup"; backup = true; })
(map
(entry: state (lib.replaceStrings ["/"] ["-"] entry) {
entry = ".populate/${entry}";
})
[
"derived" "flake-inputs" "flake-uploads" "flakes" "patches"
"derived/x" "patches/x" "flake-inputs/y" "flake-uploads/y" "flakes/z"
])
# Symlinks to outside among what populate removes: leftovers of
# interrupted runs, state of sources that are gone, and, when pruning,
# entries of the target directory. They get removed as links. So do
# hard links to a read-only file outside, whose mode stays.
(s "leftovers" {
isolate = true;
prepare = ''
mkdir -p "$T"/hard
ln -s ${outside}/dir "$T"/other
ln -s ${outside}/repo "$T"/repo
ln ${outside}/dir/sub/file "$T"/hard/link
ln ${outside}/dir/sub/file "$T"/hardlink
'';
phases = [
{
sources = stateful;
mutate = ''
ln -s ${outside}/dir "$T"/.populate/derived/x/work
ln -s ${outside}/dir "$T"/.populate/derived/gone
ln -s ${outside}/dir "$T"/.populate/flakes/gone
ln -s ${outside}/dir "$T"/.populate/patches/gone
ln -s ${outside}/dir "$T"/.populate/flake-inputs/gone
mkdir -p "$T"/.populate/flake-uploads
ln -s ${outside}/dir "$T"/.populate/flake-uploads/gone
'';
}
{
sources = stateful;
check = ''
for name in derived/x/work derived/gone flakes/gone patches/gone; do
test ! -L "$T"/.populate/$name || fail "$name remains"
done
for name in other repo .populate/flake-inputs/gone .populate/flake-uploads/gone; do
test -L "$T"/$name || fail "$name removed without pruning"
done
'';
}
{
sources = stateful;
prune = true;
check = ''
for name in other repo hard hardlink .populate/flake-inputs/gone .populate/flake-uploads; do
test ! -L "$T"/$name && test ! -e "$T"/$name || fail "$name not pruned"
done
'';
}
];
})
# A target path with characters that mean something to shells and in
# URLs.
(s "odd-target" (let
targetPath = "/var/krops/odd-target-${targetKind}/a b*c?d#e%f";
in {
isolate = true;
phases = twice {
sources = stateful // {
g.git = { url = gitUrl; ref = gitHead; };
p.pipe = "/fixtures/pipe";
};
inherit targetPath;
prune = true;
check = ''
D=${lib.escapeShellArg targetPath}
test -e "$D"/alt/flake.nix || fail "alt missing"
expect_eq HEAD "$(git -C "$D"/g rev-parse HEAD)" ${gitHead}
expect_eq p "$(cat "$D"/p)" piped
expect_eq entries "$(ls "$T")" 'a b*c?d#e%f'
gc
expect_eq greeting "$(nix_eval "$(readlink -f "$D"/x)"#greeting)" hello+alt
expect_eq msg "$(nix_eval "$(readlink -f "$D"/y)"#msg)" top:dep+inner,sub
'';
};
}))
# The target directory itself may be a symlink: what it points to is the
# target directory then.
(s "linked-target" {
isolate = true;
prepare = ''
rm -fR "$T"-real
mkdir "$T"-real
ln -s "$(basename "$T")"-real "$T"
'';
phases = twice {
sources = stateful;
check = ''
test -L "$T" || fail "the target directory isn't a link anymore"
test -e "$T"-real/alt/flake.nix || fail "alt missing"
gc
expect_eq greeting "$(nix_eval "$(readlink -f "$T"/x)"#greeting)" hello+alt
expect_eq msg "$(nix_eval "$(readlink -f "$T"/y)"#msg)" top:dep+inner,sub
expect_eq msg "$(nix_eval_online "path:$(readlink -f "$T"/.populate/flakes/z)#msg")" top:alt,sub
'';
};
})
(lib.optionals (targetKind == "local") [
# Rewriting a lock file changes no other hard link to it. The lock
# file is like the source's, with its modification time, so rsync
# leaves the link in place. (The source is on the deploying machine,
# so this only runs there.)
(s "lock-hard-link" {
prepare = ''
mkdir -p "$T"
cp -a /fixtures/filesrc "$T"/x
cp -p "$T"/x/flake.lock "$T".lock
ln -f "$T".lock "$T"/x/flake.lock
'';
phases = twice {
sources = alt // {
x.file = { path = "/fixtures/filesrc"; inputs.dep.follows = "alt"; };
};
check = ''
cmp -s "$T".lock /fixtures/filesrc/flake.lock ||
fail "the other hard link changed"
expect_eq msg "$(nix_eval_online "$T"/x#msg)" top:alt,sub
'';
};
})
# Names that lead out of the target directory, to the directory itself,
# or to populate's state.
(s "names" {
phases = map
(name: {
sources.${name}.file = "/fixtures/alt";
expect = "eval-error";
control = { ok.file = "/fixtures/alt"; };
})
[ "" "." ".." ".populate" "a/b" "../x" "/etc" "a\nb" "a b" "a*b" "a?b" "a+b" "a=b" "-a" ];
})
# Target paths that aren't absolute, that are the root directory, or
# that resolve to another directory than they name.
(s "paths" {
phases = map
(targetPath: {
sources = alt;
inherit targetPath;
expect = "eval-error";
controlTargetPath = "/var/krops/paths-local";
})
[ "" "/" "//" "/." "relative" "/var/krops/.." "/var/krops/../.." "/var/krops/a\nb" ];
})
])
]) targetKinds;
}
|