-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.nix
4216 lines (4182 loc) · 153 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
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
{ lib
, config ? {}
, ... }:
let
cfg = config.notnft or {};
enumMode = cfg.enumMode or "normal";
laxEnums = enumMode == "lax";
strictEnums = enumMode == "strict";
CTX_F_RHS = 1;
CTX_F_STMT = 2;
CTX_F_PRIMARY = 4;
# CTX_F_DTYPE = 8;
CTX_F_SET_RHS = 16;
CTX_F_MANGLE = 32;
CTX_F_SES = 64; # set_elem_expr_stmt
CTX_F_MAP = 128; # LHS of map_expr
CTX_F_CONCAT = 256;
CTX_F_ALL = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256;
isImmediateExpr = expr: builtins.isInt expr || builtins.isString expr || builtins.isPath expr || builtins.isBool expr;
# this assumes the structure is already checked, it just checks whether the expressions make sense in this context
isValidExpr = ctx: expr:
let
hasAllBits = bits: x: builtins.bitAnd bits x == bits;
isValid = isValidExpr ctx;
isValidPrim = isValidExpr (builtins.bitOr CTX_F_PRIMARY ctx);
isValidLhs = isValidExpr (builtins.bitOr CTX_F_MAP ctx);
isValidRhs = isValidExpr (builtins.bitOr CTX_F_RHS ctx);
isValidSetRhs = isValidExpr (builtins.bitOr CTX_F_SET_RHS ctx);
isValidCat = isValidExpr (builtins.bitOr CTX_F_CONCAT ctx);
in
if builtins.isInt expr || builtins.isString expr || builtins.isPath expr then true
else if builtins.isBool expr then (hasAllBits CTX_F_RHS ctx) != 0 || (hasAllBits CTX_F_PRIMARY ctx) != 0
else if builtins.isList expr then (
if hasAllBits CTX_F_PRIMARY ctx then false
else if hasAllBits CTX_F_RHS ctx || hasAllBits CTX_F_STMT ctx then builtins.all isValid expr
else false)
else if !(builtins.isAttrs expr) then false
else if builtins.length (builtins.attrNames expr) != 1 then false
# extracted from src/parser_json.c
else let
key = builtins.head (builtins.attrNames expr);
contexts = builtins.mapAttrs (k: builtins.foldl' builtins.bitOr 0) {
concat = [ CTX_F_RHS CTX_F_STMT /*CTX_F_DTYPE*/ CTX_F_SET_RHS CTX_F_SES CTX_F_MAP ];
set = [ CTX_F_RHS CTX_F_STMT ];
map = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS ];
prefix = [ CTX_F_RHS CTX_F_SET_RHS CTX_F_STMT CTX_F_CONCAT ];
range = [ CTX_F_RHS CTX_F_SET_RHS CTX_F_STMT CTX_F_CONCAT ];
payload = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_MANGLE CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
exthdr = [ CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
"tcp option" = [ CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_MANGLE CTX_F_SES CTX_F_CONCAT ];
"ip option" = [ CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_MANGLE CTX_F_SES CTX_F_CONCAT ];
"sctp chunk" = [ CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_MANGLE CTX_F_SES CTX_F_CONCAT ];
meta = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_MANGLE CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
osf = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_MAP CTX_F_CONCAT ];
ipsec = [ CTX_F_PRIMARY CTX_F_MAP CTX_F_CONCAT ];
socket = [ CTX_F_PRIMARY CTX_F_CONCAT ];
rt = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
ct = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_MANGLE CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
numgen = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
jhash = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
symhash = [ CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
fib = [ CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
"|" = [ CTX_F_RHS CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
"^" = [ CTX_F_RHS CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
"&" = [ CTX_F_RHS CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
">>" = [ CTX_F_RHS CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
"<<" = [ CTX_F_RHS CTX_F_STMT CTX_F_PRIMARY CTX_F_SET_RHS CTX_F_SES CTX_F_MAP CTX_F_CONCAT ];
accept = [ CTX_F_RHS CTX_F_SET_RHS ];
drop = [ CTX_F_RHS CTX_F_SET_RHS ];
continue = [ CTX_F_RHS CTX_F_SET_RHS ];
jump = [ CTX_F_RHS CTX_F_SET_RHS ];
goto = [ CTX_F_RHS CTX_F_SET_RHS ];
return = [ CTX_F_RHS CTX_F_SET_RHS ];
elem = [ CTX_F_RHS CTX_F_STMT CTX_F_PRIMARY CTX_F_SES ];
};
extraChecks = let
binOpCheck = expr:
isValidPrim (builtins.head expr)
&& isValidRhs (builtins.head (builtins.tail expr));
in {
jhash = { expr, ... }: isValid expr;
"|" = binOpCheck;
"^" = binOpCheck;
"&" = binOpCheck;
">>" = binOpCheck;
"<<" = binOpCheck;
concat = builtins.all isValidCat;
prefix = { addr, ... }: isValidPrim addr;
range = builtins.all isValidPrim;
set = expr:
if builtins.isList expr then builtins.all (elem:
if builtins.isList elem && builtins.length elem == 2
then isValidRhs (builtins.head elem) && isValidSetRhs (builtins.head (builtins.tail elem))
else isValidRhs elem) expr
else isImmediateExpr expr;
map = { key, data }: isValidLhs key && isValidRhs data;
elem = { val, ... }: isValidExpr val;
};
in
hasAllBits ctx (contexts.${key} or CTX_F_ALL)
&& (extraChecks.${key} or (expr: true)) expr.${key};
# get all expressions contained in an expression (non-recursive)
# warning: this may not yet be merged
innerExprs = expr:
if builtins.isList expr then expr
else if !(builtins.isAttrs expr) then []
else if builtins.length (builtins.attrNames expr) != 1 then []
else if expr?map.key && expr?map.data then [ expr.map.key expr.map.data ]
else if expr?range.min && expr?range.max then [ expr.range.min expr.range.max ]
else if expr?"|".left && expr?"|".right then [ expr."|".left expr."|".right ]
else if expr?"&".left && expr?"&".right then [ expr."&".left expr."&".right ]
else if expr?"^".left && expr?"^".right then [ expr."^".left expr."^".right ]
else if expr?"<<".left && expr?"<<".right then [ expr."<<".left expr."<<".right ]
else if expr?">>".left && expr?">>".right then [ expr.">>".left expr.">>".right ]
else lib.toList (expr.jhash.expr or expr."|" or expr."&" or expr."^" or expr."<<" or expr.">>" or expr.concat or expr.prefix or expr.range or expr.set or expr.elem.val or []);
# get the expr plus all of its inner exprs, recursively
# warning: this may not yet be merged
innerExprsRec = expr: [ expr ] ++ (map innerExprsRec (innerExprs expr));
isStringLike = s: builtins.isString s || s?__toString;
# get all enums appropriate for the expression (might have duplicates), or empty list
# specifically, this is used when passed a closure in the DSL for RHS of an operation
# warning: this may not yet be merged
exprEnums = expr:
(if builtins.isList expr then builtins.concatMap exprEnums expr
else if !(builtins.isAttrs expr) then []
else if expr?__expr__ then exprEnums expr.__expr__
else if expr?__enumName__ && nftTypes?${expr.__enumName__}.enum then [ nftTypes.${expr.__enumName__}.enum ]
else if builtins.length (builtins.attrNames expr) != 1 then []
else let key = builtins.head (builtins.attrNames expr); val = expr.${key}; in {
# set = exprEnum val;
map = exprEnums val.key ++ exprEnums val.data;
# prefix = exprEnum val.addr;
# range = exprEnum val;
payload =
if val?base then [ ]
else if !(isStringLike (val.protocol or null)) then [ ]
else if !(isStringLike (val.field or null)) then [ booleans ]
else if (payloadProtocols?${toString val.protocol}.fields.${toString val.field}.enum) then [ payloadProtocols.${toString val.protocol}.fields.${toString val.field}.enum ]
else [ ];
exthdr =
if !(isStringLike (val.name or null)) then [ ]
else if !(isStringLike (val.field or null)) then [ booleans ]
else if exthdrs?${toString val.name}.fields.${toString val.field}.enum then [ exthdrs.${toString val.name}.fields.${toString val.field}.enum ]
else [ ];
"tcp option" =
if val?base then []
else if !(isStringLike (val.name or null)) then [ ]
else if !(isStringLike (val.field or null)) then [ booleans ]
else if tcpOptions?${toString val.name}.fields.${toString val.field}.enum then [ tcpOptions.${toString val.name}.fields.${toString val.field}.enum ]
else [ ];
"ip option" =
if !(isStringLike (val.name or null)) then [ ]
else if !(isStringLike (val.field or null)) then [ booleans ]
else if ipOptions?${toString val.name}.fields.${toString val.field}.enum then [ ipOptions.${toString val.name}.fields.${toString val.field}.enum ]
else [ ];
"sctp chunk" =
if !(isStringLike (val.name or null)) then [ ]
else if !(isStringLike (val.field or null)) then [ booleans ]
else if sctpChunks?${toString val.name}.fields.${toString val.field}.enum then [ sctpChunks.${toString val.name}.fields.${toString val.field}.enum ]
else [ ];
meta =
if isStringLike (val.key or null) && metaKeys?${toString val.key}.type.enum then [ metaKeys.${toString val.key}.type.enum ]
else [ ];
# osf = [ ]; returns a string
# ipsec = [ ]; returns int/ipv4/ipv6 depending on params
# can return other stuff too
socket =
if isStringLike (val.key or null) && (toString val.key == "transparent" || toString val.key == "wildcard") then [ booleans ]
else [ ];
# returns realm/ipv4/ipv6/int/bool
rt = if isStringLike (val.key or null) && toString val.key == "ipsec" then [ booleans ] else [ ];
ct = if isStringLike (val.key or null) then lib.toList ({
state = ctStates;
direction = ctDirs;
status = ctStatuses;
l3proto = nfProtos;
}.${toString val.key} or []) else [ ];
# numgen = [ ];
# jhash = [ ];
# symhash = [ ];
fib =
if isStringLike (val.result or null)
then lib.toList ({ type = fibAddrTypes; }.${toString val.result} or [])
else [ ];
"|" = exprEnums val;
"^" = exprEnums val;
"&" = exprEnums val;
">>" = exprEnums val;
"<<" = exprEnums val;
elem = val?val && exprEnums val.val;
}.${key});
# get all enums appropriate for the expression (might have duplicates), or empty list
# warning: this may not yet be merged
exprEnumsRec = expr:
builtins.concatMap exprEnums (innerExprsRec expr);
mergeEnums = allEnums:
lib.filterAttrs (k: v: v != null)
(builtins.zipAttrsWith
(name: values:
if builtins.length values == 1
then builtins.head values
# if more than one enum has this value, simply pass the name
else name)
(lib.unique allEnums));
# warning: expr may not yet be merged
exprEnumsMerged = expr: mergeEnums (exprEnumsRec expr);
# this is a customized version of lib.types.submodule
# - advantages:
# - a custom merge functions can be applied at the end, for doing some final checks and fixups
# - I use null as a placeholder for undefined values. By default nulls are automatically stripped from the merged output,
# unless `skipNulls` is `false`
# - hence description is much better as it's aware of both nullable and non-nullable options
# - a custom chk function can be applied. Also, the default chk function checks that all fields are present
# - this is needed for either to function well with nftables' schema, as it only uses `chk` to check whether a type is compatible
# - but I don't need `lib.types.either` or `lib.types.oneOf` anyway as I use a custom `oneOf'`...
# - disadvantages:
# - the above means that unlike lib.types.submodule, here you can't define the same submodule in multiple separate files,
# the definition must be contained in a single attrset (you can't use functions/nix file paths in place of the attrset either)
# - Honestly I'm not sure why you would set a single nftables expr/stmt in multiple different locations, so it should be fine.
submodule' = { options, finalMerge ? lib.id, skipNulls ? true, freeformType ? null, chk ? null }:
let
reqFields = builtins.attrNames (if skipNulls then lib.filterAttrs (k: v: v.type.name != "nullOr") options else options);
optFields = if skipNulls then builtins.attrNames (lib.filterAttrs (k: v: v.type.name == "nullOr") options) else [];
reqFieldsDesc =
if reqFields == [] then null
else if builtins.length reqFields == 1 then ''field "${builtins.head reqFields}"''
else "fields ${builtins.concatStringsSep ", " (map (x: ''"${x}"'') reqFields)}";
optFieldsDesc =
if optFields == [] then null
else if builtins.length optFields == 1 then ''optional field "${builtins.head optFields}"''
else "optional fields ${builtins.concatStringsSep ", " (map (x: ''"${x}"'') optFields)}";
in
submoduleWith' {
shorthandOnlyDefinesConfig = true;
modules = lib.toList ({
inherit options;
} // (if freeformType != null then {
inherit freeformType;
} else {}));
description = "submodule with ${builtins.concatStringsSep " and " (builtins.filter builtins.isString [ reqFieldsDesc optFieldsDesc ])}";
descriptionClass = "conjunction";
chk = if chk != null then chk else x: builtins.all (optName: x?${optName}) reqFields;
inherit finalMerge skipNulls;
};
# single-option submodule' (SK = single key)
submoduleSK = key: val: submodule' {
skipNulls = false;
options.${key} = val;
};
submoduleWith' =
{ modules
, specialArgs ? {}
, shorthandOnlyDefinesConfig ? false
, description ? null
, descriptionClass ? "noun"
, class ? null
, finalMerge ? lib.id
, skipNulls ? true
, chk ? (_: true)
}@attrs:
let
inherit (lib.modules) evalModules;
allModules = defs: map ({ value, file }:
if builtins.isAttrs value && shorthandOnlyDefinesConfig
then { _file = file; config = value; }
else { _file = file; imports = [ value ]; }
) defs;
base = evalModules {
inherit class specialArgs;
modules = [{ _module.args.name = lib.mkOptionDefault "‹name›"; }] ++ modules;
};
freeformType = base._module.freeformType;
name = "submodule'";
in lib.mkOptionType {
inherit name descriptionClass;
description =
if description != null then description
else freeformType.description or name;
check = x: builtins.isAttrs x && chk x;
merge = loc: defs:
finalMerge ((if skipNulls then lib.filterAttrs (k: v: !(builtins.isNull v)) else lib.id) (base.extendModules {
modules = [ { _module.args.name = lib.last loc; } ] ++ allModules defs;
prefix = loc;
}).config);
emptyValue = { value = {}; };
getSubOptions = prefix: (base.extendModules
{ inherit prefix; }).options // lib.optionalAttrs (freeformType != null) {
# Expose the sub options of the freeform type. Note that the option
# discovery doesn't care about the attribute name used here, so this
# is just to avoid conflicts with potential options from the submodule
_freeformOptions = freeformType.getSubOptions prefix;
};
getSubModules = modules;
substSubModules = m: submoduleWith' (attrs // {
modules = m;
});
nestedTypes = lib.optionalAttrs (freeformType != null) {
freeformType = freeformType;
};
functor = lib.types.defaultFunctor name // {
type = submoduleWith';
payload = {
inherit modules class specialArgs shorthandOnlyDefinesConfig description;
};
binOp = lhs: rhs: {
class =
# `or null` was added for backwards compatibility only. `class` is
# always set in the current version of the module system.
if lhs.class or null == null then rhs.class or null
else if rhs.class or null == null then lhs.class or null
else if lhs.class or null == rhs.class then lhs.class or null
else throw "A submoduleWith' option is declared multiple times with conflicting class values \"${toString lhs.class}\" and \"${toString rhs.class}\".";
modules = lhs.modules ++ rhs.modules;
specialArgs =
let intersecting = builtins.intersectAttrs lhs.specialArgs rhs.specialArgs;
in if intersecting == {}
then lhs.specialArgs // rhs.specialArgs
else throw "A submoduleWith' option is declared multiple times with the same specialArgs \"${toString (builtins.attrNames intersecting)}\"";
shorthandOnlyDefinesConfig =
if lhs.shorthandOnlyDefinesConfig == null
then rhs.shorthandOnlyDefinesConfig
else if rhs.shorthandOnlyDefinesConfig == null
then lhs.shorthandOnlyDefinesConfig
else if lhs.shorthandOnlyDefinesConfig == rhs.shorthandOnlyDefinesConfig
then lhs.shorthandOnlyDefinesConfig
else throw "A submoduleWith' option is declared multiple times with conflicting shorthandOnlyDefinesConfig values";
description =
if lhs.description == null
then rhs.description
else if rhs.description == null
then lhs.description
else if lhs.description == rhs.description
then lhs.description
else throw "A submoduleWith' option is declared multiple times with conflicting descriptions";
};
};
};
# a custom oneOf that doesn't use `builtins.either` and has customized name/description/chk
# also instead of just calling `chk` when merging, it properly checks whether the values merge with that type via tryEval
oneOf' = { name, description, descriptionClass ? "noun", types, chk? (_: true) }: lib.types.mkOptionType rec {
inherit name description descriptionClass;
check = x: builtins.any (type: type.check x) types && chk x;
nestedTypes = builtins.listToAttrs (lib.imap0 (i: x: { name = toString i; value = x; }) types);
typeMerge = null;
merge = loc: defs:
let
validTypes = builtins.filter (type: builtins.all ({ value, ... }: type.check value) defs) types;
res = builtins.foldl'
(x: type: if x != null then x else
let val = builtins.tryEval (type.merge loc defs);
in if val.success then val.value else x)
null
validTypes;
in
if builtins.length validTypes == 1
then (builtins.head validTypes).merge loc defs
else if res == null
then throw "The definition of option `${lib.showOption loc}` isn't a valid ${description}. Definition values:${lib.options.showDefs defs}"
else res;
};
stringLike = lib.mkOptionType {
name = "stringLike";
description = "string";
descriptionClass = "noun";
check = s: builtins.isString s || s?__toString;
merge = loc: defs: toString (lib.options.mergeEqualOption loc defs);
};
types =
# create "name type". Name type is anything that can receive either a name as a literal string or an attrset
# with the property "name", in which case the property will be taken instead.
# In hindsight, this might not be a good decision, but I don't see any major downsides either
# addAt means check for "@" in case of a string, or add "@" before the name in case of name attr
let mkName = { name, description, addAt ? false }: lib.mkOptionType {
name = "${name}";
description = "${description}";
descriptionClass = "noun";
check = x: (builtins.isString x && (!addAt || lib.hasPrefix "@" x)) || (builtins.isAttrs x && x?name);
merge = loc: defs: lib.mergeOneOption loc (map (def@{ value, ... }: def // {
value =
if builtins.isAttrs value then (if addAt then "@${value.name}" else value.name)
else toString value;
}) defs);
};
# create an option with the same attrs as given but make it nullable and default to null
mkNullOption = attrs: lib.mkOption (attrs // {
default = null;
type = lib.types.nullOr attrs.type;
});
# this creates an enum type out of an enum (see the other mkEnum function below for a description of what enums are)
mkEnum = { name, description, enum }: lib.mkOptionType {
inherit name description;
descriptionClass = "noun";
check =
let chk = x: builtins.isAttrs x && x?__toString && x?__value__ && x?__enumName__ && (builtins.any (y: x.__value__ == y.__value__ && x.__enumName__ == y.__enumName__) (builtins.attrValues enum));
in if strictEnums then chk
else if laxEnums then (x: builtins.isString x || (builtins.isAttrs x && x?__toString))
else (x: builtins.elem x (builtins.attrNames enum) || chk x);
merge = loc: defs: (lib.mergeOneOption loc (map (def: def // {
value = toString def.value;
}) defs));
};
mkTableType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = lib.mdDoc ''The table’s family, e.g. **"ip"** or **"ip6"**.'';
};
name = {
type = lib.types.str;
description = "The table’s name.";
};
} // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The table’s handle. In input, it is used only in **delete** command as alternative to **name**.";
};
} else { }) // (if withExtraFields then builtins.mapAttrs mkOpt {
comment = {
type = lib.types.str;
description = "Undocumented upstream";
};
} else { });
};
mkChainType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withNewName ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family.";
};
table = {
type = types.tableName;
description = "The table's name.";
};
name = {
type = lib.types.str;
description = "The chain's name.";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
type = {
type = types.chainType;
description = "The chain’s type.";
};
hook = {
type = types.hook;
description = "The chain’s hook.";
};
prio = {
type = types.chainPriority;
description = "The chain’s priority.";
};
dev = {
type = lib.types.str;
description = "The chain’s bound interface (if in the netdev family).";
};
policy = {
type = types.chainPolicy;
description = "The chain’s policy.";
};
comment = {
type = lib.types.str;
description = "Undocumented upstream";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The chain’s handle. In input, it is used only in **delete** command as alternative to **name**.";
};
} else {})
// (if withNewName then builtins.mapAttrs mkOpt {
newname = {
type = lib.types.str;
description = lib.mdDoc "A new name for the chain, only relevant in the **rename** command.";
};
} else {});
};
mkRuleType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withIndex ? false, withExpr ? false, withComment ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family.";
};
table = {
type = types.tableName;
description = "The table's name.";
};
chain = {
type = types.chainName;
description = "The chain's name.";
};
}
// (if withExpr then builtins.mapAttrs mkOpt {
expr = {
type = lib.types.listOf types.statement;
description = lib.mdDoc "An array of statements this rule consists of. In input, it is used in **add**/**insert**/**replace** commands only.";
};
} else {})
// (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The rule’s handle. In **delete**/**replace** commands, it serves as an identifier of the rule to delete/replace. In **add**/**insert** commands, it serves as an identifier of an existing rule to append/prepend the rule to.";
};
} else {})
// (if withIndex then builtins.mapAttrs mkOpt {
index = {
type = lib.types.ints.unsigned;
description = lib.mdDoc "The rule’s position for add/insert commands. It is used as an alternative to handle then.";
};
} else {})
// (if withComment then builtins.mapAttrs mkOpt {
comment = {
type = lib.types.str;
description = "Optional rule comment.";
};
} else {});
};
mkSetType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false, isMap ? null }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
name = if isMap == true then "map" else "set";
in submodule' {
finalMerge = ret:
if ret?elem then
let expr = ret.elem; in assert lib.assertMsg
(if builtins.isList expr then builtins.all (elem:
if builtins.isList elem && builtins.length elem == 2
then isValidExpr CTX_F_RHS (builtins.head elem) && isValidExpr CTX_F_SET_RHS (builtins.head (builtins.tail elem))
else isValidExpr CTX_F_RHS elem) expr
else isImmediateExpr expr)
"Set/map add command's exprs are invalid in this context";
finalMerge ret
else finalMerge ret;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family.";
};
table = {
type = types.tableName;
description = "The table's name.";
};
name = {
type = lib.types.str;
description = "The ${name}'s name.";
};
} // (if withExtraFields then builtins.mapAttrs mkOpt {
type = {
type = lib.types.either (lib.types.nonEmptyListOf types.keyType) types.keyType;
description = ''The ${name}’s datatype - might be a string, such as "ipv4_addr" or an array consisting of strings (for concatenated types).'';
};
} else {})
// (if withExtraFields && isMap != false then builtins.mapAttrs mkOpt {
map = {
type = types.type;
description =
if isMap == true then
"Type of values this map maps to."
else
"Type of values this set maps to (i.e. this set is a map).";
};
} else {}) // (if withExtraFields then builtins.mapAttrs mkOpt {
policy = {
type = types.setPolicy;
description = "The ${name}’s policy.";
};
flags = {
type = lib.types.listOf types.setFlag;
description = "The ${name}’s flags.";
};
elem = {
type =
if isMap == true then lib.types.nonEmptyListOf (types.listOfSize2 types.expression)
else lib.types.nonEmptyListOf types.expression;
description = lib.mdDoc ("Initial ${name} element(s)." + (lib.optionalString (isMap != false) " For mappings, an array of arrays with exactly two elements is expected."));
};
timeout = {
type = lib.types.ints.unsigned;
description = "Element timeout in seconds.";
};
gc-interval = {
type = lib.types.ints.u32;
description = "Garbage collector interval in seconds.";
};
size = {
type = lib.types.ints.u32;
description = "Maximum number of elements supported.";
};
stmt = {
type = lib.types.listOf types.statement;
description = "Undocumented upstream";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The set’s handle. For input, it is used in the **delete** command only.";
};
} else {});
};
mkElementType = { finalMerge ? lib.id, reqFields ? [], withElem ? false, isMap ? null }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
name = if isMap == true then "map" else "set";
in submodule' {
finalMerge = ret:
if ret?elem then
let expr = ret.elem; in assert lib.assertMsg
(if builtins.isList expr then builtins.all (elem:
if builtins.isList elem && builtins.length elem == 2
then isValidExpr CTX_F_RHS (builtins.head elem) && isValidExpr CTX_F_SET_RHS (builtins.head (builtins.tail elem))
else isValidExpr CTX_F_RHS elem) expr
else isImmediateExpr expr)
"Element add command's exprs are invalid in this context";
finalMerge ret
else finalMerge ret;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = lib.mdDoc ''The table’s family, e.g. **"ip"** or **"ip6"**.'';
};
table = {
type = types.tableName;
description = "The table’s name.";
};
name = {
type = if isMap == true then types.mapName else types.setName;
description = "The ${name}’s name.";
};
} // (if withElem then builtins.mapAttrs mkOpt {
elem = {
type =
if isMap == true then lib.types.nonEmptyListOf (types.listOfSize2 types.expression)
else lib.types.nonEmptyListOf types.expression;
description = lib.mdDoc ("Elements to add to the ${name}." + (lib.optionalString (isMap != false) " Use `[ key val ]` to specify a map element."));
};
} else {});
};
mkFlowtableType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family.";
};
table = {
type = types.tableName;
description = "The table's name.";
};
name = {
type = lib.types.str;
description = "The flowtable's name.";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
hook = {
type = types.hook;
description = "The flowtable’s hook.";
};
prio = {
type = types.flowtablePriority;
description = "The flowtable’s priority.";
};
dev = {
type = lib.types.either (lib.types.nonEmptyListOf lib.types.str) lib.types.str;
description = "The flowtable’s interface(s).";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The flowtable’s handle. In input, it is used by the **delete** command only.";
};
} else {});
};
mkCounterType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family.";
};
table = {
type = types.tableName;
description = "The table's name.";
};
name = {
type = lib.types.str;
description = "The counter's name.";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
packets = {
type = lib.types.ints.unsigned;
description = "Packet counter value.";
};
bytes = {
type = lib.types.ints.unsigned;
description = "Byte counter value.";
};
comment = {
type = lib.types.str;
description = "Optional comment.";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The counter’s handle. In input, it is used by the **delete** command only.";
};
} else {});
};
mkQuotaType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family";
};
table = {
type = types.tableName;
description = "The table's name";
};
name = {
type = lib.types.str;
description = "The quota's name";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
bytes = {
type = lib.types.ints.unsigned;
description = "Quota threshold.";
};
used = {
type = lib.types.ints.unsigned;
description = "Quota used so far.";
};
inv = {
type = lib.types.bool;
default = false;
description = "If true, match if the quota has been exceeded.";
};
comment = {
type = lib.types.str;
description = "Optional comment.";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The quota’s handle. In input, it is used by the **delete** command only.";
};
} else {});
};
mkSecmarkType = { finalMerge ? lib.id, reqFields ? [], withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family";
};
table = {
type = types.tableName;
description = "The table's name";
};
name = {
type = lib.types.str;
description = "The secmark's name";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
context = {
type = lib.types.str;
description = "Undocumented upstream.";
};
comment = {
type = lib.types.str;
description = "Optional comment.";
};
} else {});
};
# no handle here!
mkCtHelperType = { finalMerge ? lib.id, reqFields ? [], withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family";
};
table = {
type = types.tableName;
description = "The table's name";
};
name = {
type = lib.types.str;
description = "The ct helper's name";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
type = {
type = lib.types.str;
description = lib.mdDoc ''The ct helper type name, e.g. **"ftp"** or **"tftp"**.'';
};
protocol = {
type = types.ctProto;
description = "The ct helper’s layer 4 protocol.";
};
l3proto = {
type = types.l3Proto;
description = lib.mdDoc ''The ct helper's layer 3 protocol, e.g. **"ip"** or **"ip6"**.'';
};
comment = {
type = lib.types.str;
description = "Optional comment.";
};
} else {});
};
mkLimitType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family";
};
table = {
type = types.tableName;
description = "The table's name";
};
name = {
type = lib.types.str;
description = "The limit's name";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
rate = {
type = lib.types.ints.unsigned;
description = "The limit’s rate value.";
};
per = {
type = types.timeUnit;
description = lib.mdDoc ''Time unit to apply the limit to, e.g. **"week"**, **"day"**, **"hour"**, etc. If omitted, defaults to **"second"**.'';
defaultText = lib.literalExpression "second";
};
rate_unit = {
type = types.rateUnit;
description = lib.mdDoc ''Unit of rate values. If omitted, defaults to **"packets"**.'';
defaultText = lib.literalExpression "packets";
};
burst = {
type = lib.types.ints.u32;
description = lib.mdDoc "The limit’s burst value. If omitted, defaults to **0**.";
defaultText = lib.literalExpression 0;
};
burst_unit = {
type = types.rateUnit;
description = lib.mdDoc ''Unit of burst values. If omitted, defaults to **"bytes"**. Has no effect if `rate_unit` is set to **"packets"**.'';
defaultText = lib.literalExpression "packets";
};
inv = {
type = lib.types.bool;
description = lib.mdDoc "If true, match if limit was exceeded. If omitted, defaults to **false**.";
defaultText = lib.literalExpression false;
};
comment = {
type = lib.types.str;
description = "Optional comment.";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The limit’s handle. In input, it is used by the **delete** command only.";
};
} else {});
};
mkCtTimeoutType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family";
};
table = {
type = types.tableName;
description = "The table's name";
};
name = {
type = lib.types.str;
description = "The ct timeout object's name";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
protocol = {
type = types.ctProto;
description = "The ct timeout object’s layer 4 protocol.";
};
l3proto = {
type = types.l3Proto;
description = lib.mdDoc ''The ct timeout object's layer 3 protocol, e.g. **"ip"** or **"ip6"**.'';
};
policy = {
type = lib.types.attrsOf lib.types.ints.u32;
description = "Undocumented upstream, each key is conn state name (`established`, `syn_sent`, `close_wait`, etc), each val is timeout value";
};
comment = {
type = lib.types.str;
description = "Optional comment.";
};
} else {}) // (if withHandle then builtins.mapAttrs mkOpt {
handle = {
type = lib.types.int;
description = lib.mdDoc "The ct timeout object’s handle. In input, it is used by the **delete** command only.";
};
} else {});
};
mkCtExpectationType = { finalMerge ? lib.id, reqFields ? [], withHandle ? false, withExtraFields ? false }:
let
req = x: builtins.elem x reqFields;
mkOpt = x: if req x then lib.mkOption else mkNullOption;
in submodule' {
inherit finalMerge;
options = builtins.mapAttrs mkOpt {
family = {
type = types.family;
description = "The table's family";
};
table = {
type = types.tableName;
description = "The table's name";
};
name = {
type = lib.types.str;
description = "The ct expectation object's name";
};
}
// (if withExtraFields then builtins.mapAttrs mkOpt {
l3proto = {
type = types.l3Proto;
description = lib.mdDoc ''The ct expectation object's layer 3 protocol, e.g. **"ip"** or **"ip6"**.'';
};
protocol = {
type = types.ctProto;
description = "The ct expectation object’s layer 4 protocol.";
};