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
|
# Run with:
# cd nixpkgs
# ./lib/tests/modules.sh
{ lib, config, ... }:
let
inherit (lib)
mkOption
mkOrder
mkMerge
mkBefore
mkAfter
mkIf
mkOverride
mkDefault
mkForce
types
;
in
{
options = {
attrList = mkOption {
type = types.lazyAttrsOf (types.attrListOf types.str);
};
attrListInt = mkOption {
type = types.lazyAttrsOf (types.attrListOf types.int);
};
attrListSubmodule = mkOption {
type = types.attrListOf (
types.submodule {
options.port = mkOption {
type = types.int;
description = "Port number";
};
options.host = mkOption {
type = types.str;
default = "localhost";
description = "Hostname";
};
}
);
};
# asAttrs: value is a merged attrset, ordered list in valueMeta
asAttrs = mkOption {
type = types.lazyAttrsOf (
types.attrListWith {
elemType = types.str;
asAttrs = true;
mergeAttrValues = _name: values: lib.last values;
}
);
};
# asAttrs with default mergeAttrValues: duplicates collected into lists
asAttrsDefault = mkOption {
type = types.lazyAttrsOf (
types.attrListWith {
elemType = types.int;
asAttrs = true;
}
);
};
# Strict wrappers that force deep evaluation, for testing error cases
attrListStrict = mkOption {
type = types.lazyAttrsOf types.raw;
};
attrListIntStrict = mkOption {
type = types.lazyAttrsOf types.raw;
};
# either picks attrList when input is list/attrset, int when input is int
eitherAttrListOrInt = mkOption {
type = types.either (types.attrListOf types.str) types.int;
};
eitherAttrListOrIntFallback = mkOption {
type = types.either (types.attrListOf types.str) types.int;
};
eitherIntOrAttrList = mkOption {
type = types.either types.int (types.attrListOf types.str);
};
eitherIntOrAttrListFallback = mkOption {
type = types.either types.int (types.attrListOf types.str);
};
assertions = mkOption { };
};
imports = [
# Second module contributing to multiModule
{
attrListInt.multiModule = [
{ b = 2; }
];
}
];
config = {
# List input: pass-through
attrList.listInput = [
{ a = "alpha"; }
{ b = "beta"; }
];
# Attrset input with explicit ordering
attrList.attrsetOrdered = {
x = mkOrder 200 "x-val";
y = mkOrder 100 "y-val";
};
# Mixed: list elements at default priority, attrset with mkOrder
attrList.mixed = mkMerge [
[
{ m = "from-list"; }
]
{
n = mkOrder 50 "from-attrset";
}
];
# Multiple list definitions from separate modules
attrListInt.multiModule = [
{ a = 1; }
];
# Attrset without mkOrder uses default priority
attrList.attrsetNoOrder = {
foo = "bar";
baz = "qux";
};
# Empty list
attrList.empty = [ ];
# Ordering test: lower priority first
attrList.ordering = mkMerge [
{
last = mkOrder 1500 "last";
}
{
first = mkOrder 500 "first";
}
[
{ middle = "middle"; }
]
];
# List elements support mkOrder/mkBefore/mkAfter
attrList.listOrdering = [
(mkAfter { z = "after"; })
{ m = "default"; }
(mkBefore { a = "before"; })
];
# Plain list entries land at default priority (1000):
# they appear after mkOrder 999 and before mkOrder 1001.
attrList.listDefaultPrio = mkMerge [
{ after = mkOrder 1001 "after"; }
[
{ mid = "list-entry"; }
]
{ before = mkOrder 999 "before"; }
];
# mkBefore and mkAfter
attrList.beforeAfter = mkMerge [
{
z = mkAfter "after";
}
{
a = mkBefore "before";
}
[
{ m = "default"; }
]
];
# mkIf: conditional definition
attrList.withMkIf = mkMerge [
(mkIf true [
{ yes = "included"; }
])
(mkIf false [
{ no = "excluded"; }
])
];
# mkOverride: higher priority override wins
attrList.withOverride = mkMerge [
(mkOverride 100 [
{ replaced = "gone"; }
])
(mkOverride 50 [
{ winner = "wins"; }
])
];
# mkDefault: lower priority than normal
attrList.withDefault = mkMerge [
(mkDefault [
{ default = "overridden"; }
])
[
{ normal = "wins"; }
]
];
# mkForce on the whole option (should discard other defs)
attrList.withForce = mkMerge [
[
{ discarded = "gone"; }
]
(mkForce [
{ forced = "wins"; }
])
];
# mkForce with mkOrder inside
attrList.forceWithOrder = mkForce [
(mkAfter { second = "after"; })
(mkBefore { first = "before"; })
];
# mkForce on individual element values; non-forced entries are discarded.
# Discarded values use a mix of: plain value, mkDefault(abort ...), mkOverride 100 (abort ...).
# The abort variants verify laziness: peelProperties sees the wrapper without forcing the content.
attrListInt.forceElementValue = [
{ a = mkDefault (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
{ a = mkForce 42; }
{ a = mkOverride 100 (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
{ b = 2; }
];
# mkForce on attrset format
attrList.forceAttrset = mkMerge [
[
{ discarded = "gone"; }
]
(mkForce {
x = mkOrder 200 "x-val";
y = mkOrder 100 "y-val";
})
];
# mkForce on repeated key: forced entries override non-forced
attrList.forceRepeatedKey = [
{ x = mkOverride 100 (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
{ x = mkForce "wins"; }
{ x = mkForce "wins 2"; }
];
# mkForce on repeated key across mkMerge
attrList.forceRepeatedKeyMerge = mkMerge [
[
{ x = "unused: overridden by mkForce"; }
{ x = mkDefault (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
]
[
{ x = mkOverride 100 (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
]
[
{ x = mkForce "forced"; }
]
];
# mkForce on repeated key in attrset format across mkMerge
attrList.forceRepeatedKeyAttrs = mkMerge [
{
x = mkDefault (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated");
y = "kept";
}
{ x = mkForce "forced"; }
];
# mkForce only affects the key it's on, other keys survive
attrList.forcePartialAttrs = mkMerge [
{
x = "unused: overridden by mkForce";
y = "normal y";
}
{ x = mkForce "forced x"; }
];
# mkForce in attrset format overrides same key from list format
attrList.forceMixedFormats = mkMerge [
[
{ x = mkOverride 100 (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
{ y = "list y"; }
]
{ x = mkForce "attrset forced x"; }
];
# Nesting: list format, mkOrder on element + mkForce on value
attrList.nestListOrderForce = mkMerge [
[
{ x = mkDefault (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
(mkOrder 500 { x = mkForce "forced-early"; })
(mkOrder 1500 { y = "late"; })
]
[
(mkOrder 100 { z = "earliest"; })
]
];
# Nesting: list format, mkOrder(mkForce(val)) on value
attrList.nestListOrderOfForce = mkMerge [
[
{ x = mkOverride 100 (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
{ y = "plain-early"; }
]
[
{ x = mkOrder 1500 (mkForce "forced-late"); }
{ z = mkOrder 500 "earliest"; }
]
[
{ x = "unused: overridden by mkForce"; }
{ w = mkOrder 1200 "mid"; }
]
];
# Nesting: list format, mkForce(mkOrder(val)) on value
attrList.nestListForceOfOrder = mkMerge [
[
{ x = "unused: overridden by mkForce"; }
{ y = "plain-early"; }
]
[
{ x = mkForce (mkOrder 1500 "forced-late"); }
{ z = mkOrder 500 "earliest"; }
]
[
{ x = mkDefault (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated"); }
{ w = mkOrder 1200 "mid"; }
]
];
# Nesting: attrset format, mkOrder wrapping mkForce
attrList.nestAttrsOrderOfForce = mkMerge [
{
x = mkOverride 100 (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated");
y = "plain-early";
}
{
x = mkOrder 1500 (mkForce "forced-late");
z = mkOrder 500 "earliest";
}
{
x = "unused: overridden by mkForce";
w = mkOrder 1200 "mid";
}
];
# Nesting: attrset format, mkForce wrapping mkOrder
attrList.nestAttrsForceOfOrder = mkMerge [
{
x = "unused: overridden by mkForce";
y = "plain-early";
}
{
x = mkForce (mkOrder 1500 "forced-late");
z = mkOrder 500 "earliest";
}
{
x = mkDefault (abort "overridden by mkForce; laziness guarantee: MUST NOT be evaluated");
w = mkOrder 1200 "mid";
}
];
# mkIf false on individual element value filters it out (list format)
attrListInt.optionalValueList = [
{ a = mkIf true 1; }
{ b = mkIf false 2; }
{ c = 3; }
];
# mkIf false on individual element value filters it out (attrset format)
attrListInt.optionalValueAttrs = {
a = mkIf true 1;
b = mkIf false 2;
c = 3;
};
# submodule elemType: produces real valueMeta
attrListSubmodule = [
{
web = {
port = 80;
};
}
{
db = {
port = 5432;
host = "dbhost";
};
}
];
# asAttrs: unique keys — value is a plain attrset
asAttrs.unique = [
{ a = "alpha"; }
{ b = "beta"; }
];
# asAttrs: duplicate keys — last in order wins
asAttrs.duplicateKeys = mkMerge [
{ x = mkOrder 500 "first"; }
{ x = mkOrder 1500 "last"; }
{ y = "only"; }
];
# asAttrs: with ordering — value is attrset, ordered list in valueMeta
asAttrs.ordered = {
z = mkOrder 200 "z-val";
a = mkOrder 100 "a-val";
};
# asAttrs: with mkForce — forced key overrides
asAttrs.withForce = mkMerge [
{ x = "unused: overridden by mkForce"; }
{
x = mkForce "forced";
y = "kept";
}
];
# asAttrs: empty
asAttrs.empty = [ ];
# asAttrsDefault: unique keys
asAttrsDefault.unique = [
{ a = 1; }
{ b = 2; }
];
# asAttrsDefault: duplicate keys — default collects into lists
asAttrsDefault.duplicates = mkMerge [
{ x = mkOrder 500 10; }
{ x = mkOrder 1500 30; }
{ y = 99; }
[
{ x = 20; }
]
];
# either: attrList branch matches for list input
eitherAttrListOrInt = [
{ a = "hello"; }
{ b = "world"; }
];
# either: int input falls through to int branch
eitherAttrListOrIntFallback = 42;
# either (swapped): int first, attrList second — int input matches int
eitherIntOrAttrList = 42;
# either (swapped): list input falls through to attrList branch
eitherIntOrAttrListFallback = [
{ a = "hello"; }
];
# Bad: string where int expected
attrListInt.badValue = [
{ a = "not-an-int"; }
];
# Bad: list element with multiple keys
attrList.badListElem = [
{
a = "ok";
b = "extra";
}
];
# Bad: plain string instead of list or attrset
attrList.badString = "not-a-container";
# Bad: list element is a bare string, not a singleton attrset
attrList.badListString = [
"not a singleton attribute"
];
attrListStrict = builtins.mapAttrs (k: v: builtins.deepSeq v v) config.attrList;
attrListIntStrict = builtins.mapAttrs (k: v: builtins.deepSeq v v) config.attrListInt;
assertions =
let
c = lib.evalModules {
modules = [ ./declare-attrList.nix ];
};
cfg = c.config;
in
# List input preserves elements
assert
cfg.attrList.listInput == [
{ a = "alpha"; }
{ b = "beta"; }
];
# Attrset input with mkOrder: lower priority comes first
assert
cfg.attrList.attrsetOrdered == [
{ y = "y-val"; }
{ x = "x-val"; }
];
# Mixed input: mkOrder 50 < default 1000
assert
cfg.attrList.mixed == [
{ n = "from-attrset"; }
{ m = "from-list"; }
];
# Multiple definitions from separate modules concatenate
# (import module's definition comes before this module's)
assert
cfg.attrListInt.multiModule == [
{ b = 2; }
{ a = 1; }
];
# Attrset without mkOrder: all at default priority
assert builtins.length cfg.attrList.attrsetNoOrder == 2;
# Empty list stays empty
assert cfg.attrList.empty == [ ];
# List elements support mkOrder/mkBefore/mkAfter
assert
cfg.attrList.listOrdering == [
{ a = "before"; }
{ m = "default"; }
{ z = "after"; }
];
# Plain list entries are at default priority (1000)
assert
cfg.attrList.listDefaultPrio == [
{ before = "before"; }
{ mid = "list-entry"; }
{ after = "after"; }
];
# Ordering: 500 < 1000 (default) < 1500
assert
cfg.attrList.ordering == [
{ first = "first"; }
{ middle = "middle"; }
{ last = "last"; }
];
# mkBefore (500) < default (1000) < mkAfter (1500)
assert
cfg.attrList.beforeAfter == [
{ a = "before"; }
{ m = "default"; }
{ z = "after"; }
];
# mkIf true includes, mkIf false excludes
assert
cfg.attrList.withMkIf == [
{ yes = "included"; }
];
# mkOverride: only lowest priority override survives
assert
cfg.attrList.withOverride == [
{ winner = "wins"; }
];
# mkDefault is overridden by normal definitions
assert
cfg.attrList.withDefault == [
{ normal = "wins"; }
];
# mkForce discards other definitions
assert
cfg.attrList.withForce == [
{ forced = "wins"; }
];
# mkForce with mkOrder inside: ordering still works
assert
cfg.attrList.forceWithOrder == [
{ first = "before"; }
{ second = "after"; }
];
# mkForce on individual element values passes through
assert
cfg.attrListInt.forceElementValue == [
{ a = 42; }
{ b = 2; }
];
# mkForce on attrset format: discards other defs, ordering preserved
assert
cfg.attrList.forceAttrset == [
{ y = "y-val"; }
{ x = "x-val"; }
];
# mkForce on repeated key: forced entries override non-forced
assert
cfg.attrList.forceRepeatedKey == [
{ x = "wins"; }
{ x = "wins 2"; }
];
# mkForce on repeated key across mkMerge: forced wins
assert
cfg.attrList.forceRepeatedKeyMerge == [
{ x = "forced"; }
];
# mkForce on repeated key in attrset format: discards other x, keeps y
assert
cfg.attrList.forceRepeatedKeyAttrs == [
{ y = "kept"; }
{ x = "forced"; }
];
# mkForce only affects its own key
assert
cfg.attrList.forcePartialAttrs == [
{ y = "normal y"; }
{ x = "forced x"; }
];
# mkForce in attrset format overrides same key from list format
assert
cfg.attrList.forceMixedFormats == [
{ y = "list y"; }
{ x = "attrset forced x"; }
];
# Nesting: list format, mkOrder on element + mkForce on value
# z(100) < x-forced(500) < y(1500); x-discarded filtered by mkForce
assert
cfg.attrList.nestListOrderForce == [
{ z = "earliest"; }
{ x = "forced-early"; }
{ y = "late"; }
];
# Nesting: list format, mkOrder(mkForce(val)) on value
# z(500) < y(1000) < w(1200) < x-forced(1500); x-discarded entries filtered
assert
cfg.attrList.nestListOrderOfForce == [
{ z = "earliest"; }
{ y = "plain-early"; }
{ w = "mid"; }
{ x = "forced-late"; }
];
# Nesting: list format, mkForce(mkOrder(val)) on value
# z(500) < y(1000) < w(1200) < x-forced(1500); x-discarded entries filtered
assert
cfg.attrList.nestListForceOfOrder == [
{ z = "earliest"; }
{ y = "plain-early"; }
{ w = "mid"; }
{ x = "forced-late"; }
];
# Nesting: attrset format, mkOrder(mkForce(val))
# z(500) < y(1000) < w(1200) < x-forced(1500); x-discarded entries filtered
assert
cfg.attrList.nestAttrsOrderOfForce == [
{ z = "earliest"; }
{ y = "plain-early"; }
{ w = "mid"; }
{ x = "forced-late"; }
];
# Nesting: attrset format, mkForce(mkOrder(val))
# z(500) < y(1000) < w(1200) < x-forced(1500); x-discarded entries filtered
assert
cfg.attrList.nestAttrsForceOfOrder == [
{ z = "earliest"; }
{ y = "plain-early"; }
{ w = "mid"; }
{ x = "forced-late"; }
];
# mkIf false on individual element value filters it out (list format)
assert
cfg.attrListInt.optionalValueList == [
{ a = 1; }
{ c = 3; }
];
# mkIf false on individual element value filters it out (attrset format)
assert
cfg.attrListInt.optionalValueAttrs == [
{ a = 1; }
{ c = 3; }
];
# submodule: value, option descriptions, and valueMeta with real configuration metadata
assert
cfg.attrListSubmodule == [
{
web = {
host = "localhost";
port = 80;
};
}
{
db = {
host = "dbhost";
port = 5432;
};
}
];
assert
builtins.map (m: m.configuration.config) c.options.attrListSubmodule.valueMeta.attrList == [
{
host = "localhost";
port = 80;
}
{
host = "dbhost";
port = 5432;
}
];
assert
builtins.map (
m:
builtins.mapAttrs (n: o: o.description) (builtins.removeAttrs m.configuration.options [ "_module" ])
) c.options.attrListSubmodule.valueMeta.attrList == [
{
host = "Hostname";
port = "Port number";
}
{
host = "Hostname";
port = "Port number";
}
];
# valueMeta.attrList has one entry per (non-filtered) element
assert
c.options.attrList.valueMeta.attrs.listInput.attrList == [
{ }
{ }
];
assert
c.options.attrList.valueMeta.attrs.attrsetOrdered.attrList == [
{ }
{ }
];
assert
c.options.attrList.valueMeta.attrs.mixed.attrList == [
{ }
{ }
];
assert c.options.attrList.valueMeta.attrs.empty.attrList == [ ];
assert
c.options.attrListInt.valueMeta.attrs.optionalValueList.attrList == [
{ }
{ }
];
# either: headError is null for valid attrList input, so attrList branch is picked
assert
cfg.eitherAttrListOrInt == [
{ a = "hello"; }
{ b = "world"; }
];
# either: headError is non-null for int input, so int branch is picked
assert cfg.eitherAttrListOrIntFallback == 42;
# either (swapped): int first — int input matches
assert cfg.eitherIntOrAttrList == 42;
# either (swapped): list input falls through to attrList branch
assert
cfg.eitherIntOrAttrListFallback == [
{ a = "hello"; }
];
# asAttrs: unique keys — value is a plain attrset
assert
cfg.asAttrs.unique == {
a = "alpha";
b = "beta";
};
# ordered list preserved in valueMeta
assert
c.options.asAttrs.valueMeta.attrs.unique.attrListValue == [
{ a = "alpha"; }
{ b = "beta"; }
];
# asAttrs: duplicate keys — last in order wins
assert
cfg.asAttrs.duplicateKeys == {
x = "last";
y = "only";
};
assert
c.options.asAttrs.valueMeta.attrs.duplicateKeys.attrListValue == [
{ x = "first"; }
{ y = "only"; }
{ x = "last"; }
];
# asAttrs: ordered — value is attrset (unordered), list in valueMeta preserves order
assert
cfg.asAttrs.ordered == {
a = "a-val";
z = "z-val";
};
assert
c.options.asAttrs.valueMeta.attrs.ordered.attrListValue == [
{ a = "a-val"; }
{ z = "z-val"; }
];
# asAttrs: mkForce — forced key overrides, value is attrset
assert
cfg.asAttrs.withForce == {
x = "forced";
y = "kept";
};
# asAttrs: empty — value is empty attrset
assert cfg.asAttrs.empty == { };
# asAttrsDefault: unique keys — each value wrapped in singleton list
assert
cfg.asAttrsDefault.unique == {
a = [ 1 ];
b = [ 2 ];
};
# asAttrsDefault: duplicate keys — values collected into list in order
assert
cfg.asAttrsDefault.duplicates == {
x = [
10
20
30
];
y = [ 99 ];
};
assert
c.options.asAttrsDefault.valueMeta.attrs.duplicates.attrListValue == [
{ x = 10; }
{ y = 99; }
{ x = 20; }
{ x = 30; }
];
# valueMeta.definitions: mkDefinition records with mkOrder-wrapped single-key attrsets
# Use duplicateKeys which has mixed priorities and repeated keys
assert
let
defs = c.options.asAttrs.valueMeta.attrs.duplicateKeys.definitions;
extract = d: {
prio = d.value.priority;
value = d.value.content;
};
in
map extract defs == [
{
prio = 500;
value = {
x = "first";
};
}
{
prio = 1000;
value = {
y = "only";
};
}
{
prio = 1500;
value = {
x = "last";
};
}
];
# Round-trip: feed definitions through mapDefinitionValue + mkMerge into a listOf option
assert
let
rendered = lib.modules.mapDefinitionValue (attr: lib.cli.toCommandLineGNU { } attr) (
mkMerge c.options.asAttrs.valueMeta.attrs.duplicateKeys.definitions
);
result =
(lib.evalModules {
modules = [
{ options.out = mkOption { type = types.listOf types.str; }; }
{ config.out = rendered; }
# Interleave: mkOrder 800 lands between x(500) and y(1000)
{ config.out = mkOrder 800 [ "--interleaved" ]; }
];
}).config.out;
in
result == [
"-xfirst"
"--interleaved"
"-yonly"
"-xlast"
];
# Error cases are tested via checkConfigError in modules.sh
"ok";
};
}
|