Skip to content

Commit

Permalink
Merge pull request #330030 from tweag/syntax
Browse files Browse the repository at this point in the history
Document editorconfig and nixfmt, remove manual rules
  • Loading branch information
infinisil authored Jul 25, 2024
2 parents 2ef8b77 + 8121f5a commit cc49e3c
Showing 1 changed file with 2 additions and 129 deletions.
131 changes: 2 additions & 129 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,138 +557,11 @@ Names of files and directories should be in lowercase, with dashes between words

### Syntax

- Use 2 spaces of indentation per indentation level in Nix expressions, 4 spaces in shell scripts.

- Do not use tab characters, i.e. configure your editor to use soft tabs. For instance, use `(setq-default indent-tabs-mode nil)` in Emacs. Everybody has different tab settings so it’s asking for trouble.
- Set up [editorconfig](https://editorconfig.org/) for your editor, such that [the settings](./.editorconfig) are automatically applied.

- Use `lowerCamelCase` for variable names, not `UpperCamelCase`. Note, this rule does not apply to package attribute names, which instead follow the rules in [package naming](./pkgs/README.md#package-naming).

- Function calls with attribute set arguments are written as

```nix
foo {
arg = <...>;
}
```

not

```nix
foo
{
arg = <...>;
}
```

Also fine is

```nix
foo { arg = <...>; }
```

if it's a short call.

- In attribute sets or lists that span multiple lines, the attribute names or list elements should be aligned:

```nix
{
# A long list.
list = [
elem1
elem2
elem3
];
# A long attribute set.
attrs = {
attr1 = short_expr;
attr2 =
if true then big_expr else big_expr;
};
# Combined
listOfAttrs = [
{
attr1 = 3;
attr2 = "fff";
}
{
attr1 = 5;
attr2 = "ggg";
}
];
}
```

- Short lists or attribute sets can be written on one line:

```nix
{
# A short list.
list = [ elem1 elem2 elem3 ];
# A short set.
attrs = { x = 1280; y = 1024; };
}
```

- Breaking in the middle of a function argument can give hard-to-read code, like

```nix
someFunction { x = 1280;
y = 1024; } otherArg
yetAnotherArg
```

(especially if the argument is very large, spanning multiple lines).

Better:

```nix
someFunction
{ x = 1280; y = 1024; }
otherArg
yetAnotherArg
```

or

```nix
let res = { x = 1280; y = 1024; };
in someFunction res otherArg yetAnotherArg
```

- The bodies of functions, asserts, and withs are not indented to prevent a lot of superfluous indentation levels, i.e.

```nix
{ arg1, arg2 }:
assert system == "i686-linux";
stdenv.mkDerivation { /* ... */ }
```

not

```nix
{ arg1, arg2 }:
assert system == "i686-linux";
stdenv.mkDerivation { /* ... */ }
```

- Function formal arguments are written as:

```nix
{ arg1, arg2, arg3 }: { /* ... */ }
```

but if they don't fit on one line they're written as:

```nix
{ arg1, arg2, arg3
, arg4
# Some comment...
, argN
}: { }
```
- New files must be formatted by entering the `nix-shell` from the repository root and running `nixfmt`.

- Functions should list their expected arguments as precisely as possible. That is, write

Expand Down

0 comments on commit cc49e3c

Please sign in to comment.