-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathstandard.md
1481 lines (1248 loc) · 34.2 KB
/
standard.md
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
# Standard Nix format
This document describes the standard Nix format, as first established [by RFC 166](https://github.com/NixOS/rfcs/blob/master/rfcs/0166-nix-formatting.md#standard-nix-format).
Controversial changes to this document must go through another RFC.
It is up to the Nix formatting team to decide when an RFC is necessary.
## Terms and definitions:
- **Brackets:** `[]`
- **Braces:** `{}`
- **Parentheses**: `()`
- **Expressions:**
All syntax nodes that would be a syntactically correct Nix program on their own.
- **Terms:** The following expressions are called terms
- Variables, int, float, string, path, list, set, selection, all parenthesised expressions
- As a rule of thumb: Expressions which can be used as list items (without parentheses)
- **Absorption:**
A multiline expression can have an alternative layout depending on the context.
In that case, it will start on the end of the current line instead of a new line,
potentially saving a level of indentation of its content.
```nix
{
# The right-hand side of bindings is an example of a situation where absorption improves the style.
absorbed = with bar; [
1
2
3
];
notAbsorbed =
with bar; # Placing a comment here will force the non-absorbed, multiline layout.
[
1
2
3
];
# In this case, absorption does not reduce the indentation level of the set.
absorbed' =
let
qux = 1;
in
# { is absorbed
bar: baz: {
# <-- same level
};
notAbsorbed' =
let
qux = 1;
in
way:
too:
many:
arguments:
{
# <-- same level
};
}
```
- **Absorbable Terms:**
Attribute sets, lists, and multiline `''` strings are called absorbable terms. Parenthesized absorbable terms are absorbable terms again too.
## General
- The formatter should be as "pure" as possible, i.e. different input formats of the "same" code (same AST with comments) should result in the same output format.
- The formatter may take the input formatting into account in some cases in order to preserve multi-line syntax elements (which would otherwise have been contracted by the rules).
- Line breaks may be added or removed, but empty lines must not be created. Single empty lines must be preserved, and consecutive empty lines must be collapsed into a single empty line.
This allows the formatter to expand or compact multi-line expressions, while still allowing grouping of code.
For example, formatting this code:
```nix
[
0 10
(
20 + 1
)
30
]
```
turns into this:
```nix
[
0 # Line break added
10
(20 + 1) # Line breaks removed
# Consecutive empty lines turned into a single empty line
30
]
```
- Expressions of the same kind that can be treated as a sequence of expressions on the same level should be treated as such, even though they are technically parsed as a nested tree.
- This applies to else-if chains, functions with multiple arguments, some operators, etc.
- Example:
```nix
# This is treated as a sequence of if-then-else chains, instead of indenting the second if as part of the else body
if cond1 then
foo
else if cond2 then
bar
else
baz
```
- Indentation should reflect the expression structure.
Example:
```nix
# Bad, the indentation misleads the user
{
foo = {
bar = if
baz == null then 10
else 20
;
}; }
# Good
{
foo = {
bar =
if baz == null then
10
else
20;
};
}
```
### Editor Config
This [editor config](https://editorconfig.org/) specifies the basic details about Nix files:
```editorconfig
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = space
```
### Single-line common ancestor expression rule
For any two (sub-)expressions that are fully on a common single line, their smallest common ancestor expression must also be on the same line.
**Example**
```nix
# Bad, expressions cond and foo are fully on the same line,
# but their smallest common ancestor expression is the entire if-then-else, which spans multiple lines
if cond then foo
else bar
# Okay, cond, foo and bar have the if-then-else as a common ancestor expression,
# which is also fully on the same line
if cond then foo else bar
# Bad, due to function application precedence, the smallest common ancestor expression
# of foo and bar is `foo || bar baz`, which spans two lines
foo || bar
baz
```
**Rationale**
This rule has turned out to be very practical at catching code that could be potentially hard to understand or edit.
### Line length
- There should be a configurable _soft_ line length limit, limiting the number of characters on one line without counting the leading indentation.
The default should be 100 characters.
- There may also be a configurable _hard_ line length limit, which includes the leading indentation.
- String-like values such as strings, paths, comments, urls, etc. may go over the hard line length limit.
### Indentation
- Two spaces must be used for each indentation level.
- This may be revisited should Nix get proper support for [using tabs for indentation](https://github.com/NixOS/nix/issues/7834) in the future.
- No special care is taken to preserve vertical alignment in the AST or comments.
- It is non-trivial to specify a rule for preserving vertical alignment, so this is out of scope for now, but could be reconsidered in the future.
- Examples:
```nix
{
# Vertically aligned input like this..
foo = 10; # Foo
b = 10; # - b
baz = 10; # - baz
more = 10; # - more
# ..will get formatted like this.
# The vertical alignment is not preserved.
foo = 10; # Foo
b = 10; # - b
baz = 10; # - baz
more = 10; # - more
# Vertically aligned input like this..
netbsd = { execFormat = elf; families = { inherit bsd; }; };
none = { execFormat = unknown; families = { }; };
# ..will get formatted like this.
netbsd = {
execFormat = elf;
families = {
inherit bsd;
};
};
none = {
execFormat = unknown;
families = { };
};
# Vertically aligned input like this..
optExecFormat =
lib.optionalString (kernel.name == "netbsd" &&
gnuNetBSDDefaultExecFormat cpu != kernel.execFormat
)
kernel.execFormat.name;
# ..will get formatted like this.
optExecFormat = lib.optionalString (
kernel.name == "netbsd" && gnuNetBSDDefaultExecFormat cpu != kernel.execFormat
) kernel.execFormat.name;
}
```
- Increasing indentation levels must not be "skipped": On subsequent lines, indentation can only increase by at most one level, but may decrease arbitrarily many levels.
- Examples:
```nix
# Bad indentation
buildInputs = [
foo # <-- Not okay, increase by 2 levels
] ++ lib.optionals cond [
bar
];
# Okay indentation, subsequent lines at most one more level
buildInputs =
[
foo
] ++ lib.optionals cond [
bar
];
# Bad indentation
attribute = { args }: let
foo = "bar"; # <-- Not okay, increase by 2 levels
in
foo;
# Okay indentation
attribute = { args }:
let
foo = "bar";
in
foo;
# Bad indentation
(callFunction {
foo = "bar"; # <-- Not okay, increase by 2 levels
}
arg
)
# Okay indentation
(callFunction
{
foo = "bar";
}
arg
)
# Okay indentation
let
x = {
a = foo
bar
baz;
}; # <-- The decrease by two levels here is okay, only increases are limited to one level
in
null
```
### Expansion of expressions
Unless stated otherwise, any expression that fits onto one single line must be trivially formatted as such.
For list elements, attributes, and function arguments, the following applies:
- If expanded into multiple lines, each item must be on its own line.
- Grouping similar items together can be done by adding blank lines or comments between the groups instead.
- This also applies to the first item, so e.g. `[ firstElement` in a multi line list is not allowed.
- Long sequences of items should be liberally expanded, even if they would fit onto one line character-wise.
- The motivation is to keep the information per line manageable. Usually "number of elements" is a better metric for that than "line length".
- The cutoff is usually determined empirically based on common usage patterns.
**Examples:**
```nix
{
buildInputs = [
foo
bar
baz
somethingElse
];
systemd.services = {
foo = { };
bar = { };
};
inherit
lib
foo
bar
baz
;
}
```
### Strings
- The kind of quotes used in strings (`"` vs `''`) must be preserved from the input.
- The non-interpolated string parts must be preserved from the input
- E.g. changing `\t` to a tab character must not be done automatically
**Examples:**
```nix
# Kept as is
"foo \n\t ${bar} baz"
# This one too
''
foo \n\t ${bar} baz
''
# Even if strings exceed the line length limit, no attempt to make it smaller is made
''
This is a really long string that would not fit within the line length limit
''
```
#### Interpolations
- "Simple" interpolations must be rendered using the single-line format, regardless of the line's length.
- Otherwise, the multiline formatting must be used
- "simple" is implementation-defined and generally includes short expressions of low complexity.
Multiline expressions are never "simple".
- If the interpolation is the first thing on the string line, then its contents may be absorbed.
- Otherwise, the interpolation code must start on a new line
**Examples**:
```nix
# Short and simple
"foo \n\t ${bar} baz"
# Interpolation of simple or short code
# Good
throw ''Some very long error messages containing ${variables} and stuff''
# Bad
throw ''Some very long error messages containing ${
variables
} and stuff''
''
# Don't absorb interpolations if they don't start the line
# Good
some longer line ${
some function [
1
2
]
} baz
# Bad
some longer line ${some function [
1
2
]} baz
# However, absorption is allowed here, since the interpolation starts a line
${other function (
# with stuff
)}
''
```
### Comments
- `/**` comments must be handled according to [RFC 0145: Doc comments](https://github.com/NixOS/rfcs/pull/145).
- Specifically, the expression that the comment is attached to must be maintained by the formatter, as well as the resulting doc string.
- Empty comments may be deleted.
- Often their only purpose is to vertically align lines, which is not allowed.
- Single-line `/*` comments must be converted to `#` comments.
- Single-line comments may be moved up or down a line to improve the layout.
- Anything after the first `#` of single-line comments must be preserved.
- This allows the common pattern of prefixing many lines with `#` to comment them out, without the formatter trying to change anything.
- For multiline `/*` and `/**` comments:
- Both `/*`/`/**` and `*/` start on a new line each, and are vertically aligned (i.e. have the same level of indentation).
- The left-most line in between have one extra level of indentation (relative to the starting `/*`/`/**`).
- Inner-comment indentation is preserved, in a similar way as for multiline strings.
- Whitespace immediately after `/*`/`/**` and whitespace before `*/` may not be preserved.
- Content after `/*`/`/**` on the same line may get shifted the next line.
- Comments where all intermittent lines start with a `*` may have it stripped.
- Otherwise, the non-whitespace content of each comment line must be preserved.
**Examples:**
Note that these examples show *allowed* transformations, which may or may not be applied by the formatter.
```nix
/* foo */
↓
# foo
/*bar */
↓
# bar
function call ( # trailing comment
body
)
↓
function call (
# trailing comment
body
)
if /* inline comment */ cond then
true
else
false
↓
# inline comment
if cond then
true
else
false
if cond /* inline comment */ then
true
then
false
↓
if
cond # inline comment
then
true
else
false
/* foo */ ''
bar
''
↓
# foo
''
bar
''
/* Foo
bar
baz */
↓
/*
Foo
bar
baz
*/
/* Foo
bar
baz
*/
↓
/*
Foo
bar
baz
*/
/* Foo
* bar
*/
↓
/*
Foo
bar
*/
# Some comment
# Some preserved indentation
#This also stays as is
```
**Alternatives:**
- There are some ways of using multi-line comments to comment parts of strings that wouldn't otherwise support comments.
For example in bash
```bash
some-command \ # Some comment
some-arg \ # Some comment
another-arg
```
is not valid.
In Nix we have the ability to use string concatenation and inline comments to add comments between the arguments:
```nix
''
some-command \
'' + /* Some comment */ ''
some-arg \
'' + /* Some comment */ ''
another-arg
''
```
This style of the `+` operator for one isn't consistent with the rest of the formatting rules.
Alternatively:
```nix
some-command \ ${""/* Some comment */}
some-arg \ ${"" /* Some comment */}
another-arg
```
But this is considered too hacky.
### Function application
In a function application chain, the first element is treated as the "function" and the remaining ones as "arguments".
- As many arguments as possible must be fit onto the first line.
- If there is at most one multi-line argument that can be absorbed and all other arguments before/after fit onto a single line respectively, then that multi-line argument is absorbed.
- Otherwise, the first argument not fitting onto the first line will start a new line with indentation, and all subsequent arguments will start on their own line as well.
- All arguments that are not on the same line as the function must be indented by one level.
- If the last argument is parenthesized, the parentheses should get absorbed while its body is put on a new line with indentation.
- Exception: If the last argument is parenthesized and its body contains an absorbable term, an alternative and more compact layout may be used instead: The body gets compacted and its term absorbed.
- In this case, the inner term may be force-expanded.
- This results in less indentation for many common Nix idioms.
**Examples:**
```nix
# All arguments fit onto the first line
function arg1 arg2
# The line length limit is reached, so the remaining arguments need to be on their own lines
function arg1 arg2 arg3
arg4
arg5
# The last argument is a multiline expression, so it doesn't fit on the first line,
# but it can still start on the first line
function arg1 arg2 {
more = "things";
}
# The second argument doesn't fit on the first line, but it's not the last argument,
# so it needs to start on a new line
function arg1 {
more = "things";
} arg3
# In this case, the remaining arguments after the second one woulnd't fit onto the line anymore, therefore start all of them on a new line
function arg1
{
more = "things";
}
arg3
many
long
args
# Same with multiple multiline arguments
function
{
a = 1;
b = 2;
}
{
c = 1;
d = 2;
}
# Assume that the line length limit is here ↓
# Good
concatMapString (s: "short string: ${s}") (
attrsToList foo
)
# Good, this is also allowed
concatMapString (s: "short string: ${s}")
(attrsToList foo)
# Bad: The first argument would have fit onto the first line
concatMapString (
s: "short string: ${s}"
) (attrsToList foo)
# Good: The body of the last argument starts on a new line with indentation
pkgs.nixosTest (
{ pkgs }:
{
config = { };
}
)
# Good: The last argument is parenthesised and contains a function declaration, the exception makes this have less lines
stdenv.mkDerivation (finalAttrs: {
name = "...";
})
```
**Drawbacks**
- This style sometimes forces lists or attribute sets to start on a new line, with additional indentation of their items.
**Alternatives**
- Compacting multiline arguments like this:
```nix
function arg1 {
# stuff
} arg3
function {
# ...
} {
# ...
}
```
- This violates the guideline of the indentation representing the expression structure, and thus reduces readability.
### Function declaration
- The body of the function must not be indented relative to its first arguments.
- A small number of ("simple") identifier arguments can be written onto the same line.
- Otherwise they're each on their own line.
- The body may get absorbed here
- Attribute set arguments must always start on a new line and they must not be mixed with identifier arguments.
- If they have few attributes, the argument may be written on a single line
- Otherwise each attribute must be on its own line with indentation, followed by a trailing comma.
- Due to structural similarity and for consistency reasons, attribute set arguments with a default value follow the same rules as [bindings](#bindings).
**Examples**
```nix
#1
name: value: name ++ value
#2 absorption
name: value: ''
${name} = ${value};
''
#3
name: value:
name
++ value
++ more stuff making the line too long
#4
{ pkgs }: pkgs.hello
#5
args@{
some,
argument,
default ? value,
...
}:
{
# body
}
#6
{ pkgs }:
name: value:
{
# body
}
#7: These would be over the line length limit on a single line
aaaa:
bbbb:
cccc:
dddd:
eeee:
null
#8: @ patterns can also come afterwards
{ pkgs }@args: pkgs.hello
```
**Alternatives**
- Have leading commas for parameters in attribute set arguments, like currently done in Nixpkgs.
- This makes attribute set arguments less likely to be confused with lists.
- It's easier to see where arguments start and end.
```nix
{ some
, arg
}:
args@{
some
, argument
# Single line comment
, commentedArgument
, # Comment on the value
# multiline comment
default ? value
, ...
}:
# ...
```
Problems with this alternative:
- Moving items around with this style may require editing lines.
- Inconsistent with the [expression expansion guidelines](#expansion-of-expressions), which disallows forms like `{ some`; `some` should start on a new line instead.
- This does not work well with leading `@` bindings.
- It's unclear whether comments belong to the next or the previous argument.
- The leading comma style was a lesser-evil workaround for the lack of trailing commas in the Nix language. Now that the language has this feature, there is no reason to keep it that way anymore.
### Operators
From the [list of operators](https://nixos.org/manual/nix/stable/language/operators.html#operators), this section focuses on binary operators.
Function application and attribute selection are not treated as an "operator" in the sense of this section, see [function application](#function-application) instead.
#### Non-chainable operators
Operators with no associativity are non-chainable.
Each invocation will always have exactly one left-hand side and one right-hand side.
The right-hand side must always be attached to the operator on the same line.
The operator must either be attached to the left-hand side as well, or start on a new line.
```nix
shortVariable == 42
stringLength (drvName (toString oldDependency))
== stringLength (drvName (toString newDependency))
some complicated calculation {
# arguments
} == other stuff {
# which may be multiline
}
some complicated calculation {
# arguments
}
== "some very long string"
```
#### Chainable operators
Chained binary associative [operators](https://nixos.org/manual/nix/stable/language/operators.html#operators) with the same or monotonically decreasing precedence must be treated together as a single operator chain.
If an operator chain does not fit onto one line, it must be expanded such that every operator starts a new line:
- If the operand can also fit on the same line as the operator, it must be put there
- Otherwise, the operand must either be absorbed or start a new line with indentation
Operator chains in bindings may be compacted as long as all lines between the first and last one are indented.
**Examples**
```nix
# These chained associative operators have increasing precedence, so they're _not_ treated the same
foo
-> # <- The operator starts on a new line, but right operand is all of the below lines, they don't fit here, so indent
bar
||
baz
&& qux # <- The operand fits on this line
# These chained associative operators have decreasing precedence, so they're treated the same
foo
&& bar # <- All of these operands are just identifiers, they fit on the same line
|| baz # <- We shouldn't indent these lines, because it misleads into thinking that || binds stronger than &&
-> qux
[
some
flags
]
++ ( # <- Parenthesized expressions get absorbed
foo
)
++ optionals condition [ # <- As are some multiline function applications
more
items
]
++
runCommand name # <- Function application which cannot be absorbed start on a new line with indentation
''
echo hi
''
test
# In bindings we can use a more compact form as long as all in-between lines are indented.
{
foo = bar // {
x = 10;
y = 20;
} // baz;
}
# Bad, we can't use the more compact form because an intermediate line is not indented.
{
foo = {
x = 10;
y = 20;
} // bar // {
z = 30;
w = 40;
};
}
# Good, this is the non-compact operator form
{
foo =
{
x = 10;
y = 20;
}
// bar
// {
z = 30;
w = 40;
};
}
# Good
{
postPatch =
''
patchShebangs .
''
+ lib.optionalString withFrei0r ''
substituteInPlace libavfilter/vf_frei0r.c \
--replace /usr/local/lib/frei0r-1 ${frei0r}/lib/frei0r-1
substituteInPlace doc/filters.texi \
--replace /usr/local/lib/frei0r-1 ${frei0r}/lib/frei0r-1
'';
configureFlags =
[
# * Program flags
(enableFeature buildFfmpeg "ffmpeg")
(enableFeature buildFfplay "ffplay")
(enableFeature buildFfprobe "ffprobe")
]
++ optionals withBin [ "--bindir=${placeholder "bin"}/bin" ]
++ [
# ...
];
}
```
### if-then-else
- `if` and `else` keywords must always start on a new line.
- The `if` and `else` bodies must always be indented.
- If the condition does not fit onto one line, then it will start on the next line with indentation, and `then` will be on the start of the line following the condition.
- `else if` chains are treated as one long sequence, with no indentation creep on each step.
- `else if` chains must not be on a single line.
**Examples**
```nix
# Condition fits on one line
if builtins.length matches != 0 then
{ inherit path matches; }
else if path == /. then
[
1
2
]
else
go (dirOf path);
# Condition doesn't fit onto one line
if
matches != null
&& builtins.length matches != 0
then
{ inherit path matches; }
else if path == /. then
null
else
go (dirOf path);
```
**Alternatives**
- The bodies could be absorbed in some cases, saving an indentation level:
```nix
#1a
if builtins.length matches != 0 then {
inherit path matches;
} else if path == /. then [
1
2
] else
go (dirOf path);
```
- This results in inconsistent vertical start of the keywords, making the structure harder to follow
- Have the `then` on the start of the next line, directly followed by the if body:
```nix
#1b
if builtins.length matches != 0
then { inherit path matches; }
else if path == /.
then [
1
2
]
else go (dirOf path);
#1c
if builtins.length matches != 0
then { inherit path matches; }
else if path == /.
then [
1
2
]
else go (dirOf path);
```
### assert
- `assert <cond>;` mirrors the formatting for [bindings](#bindings).
- `assert`s `<body>` must always start on their own line and the body also starts on its own line without any additional indentation.
```nix
# Good
assert foo;
[
bar
baz
]
# Bad
assert foo; [
bar
baz
]
# Good
{
vendor ?
assert false;
null,
vendor ? null,
}:
null
let
# Good
x =
assert foo;
bar;
# Bad
y = assert foo;
bar;
in
x
# Multiline condition
assert
let
x = true;
in x;
true
# Function call condition with absorbed last argument, same formatting as bindings
assert assertMsg (isPath path) ''
lib.path.append:
The first argument is of type ${builtins.typeOf path}, but a path was expected
'';
true
```
**Alternatives**
- Treat it the [same as `with`](#with). The reasons not to do that:
- `assert`'s stand on their own and could be removed without breaking anything. Comparatively, `with`'s can't be removed without breaking the code
- `assert`'s are a bit like `if-then-else` statements, which are also spread out over multiple lines
### with
- In any situation where a term would get absorbed, the term with a `with` prepended to it may get absorbed as well.