docs(nix): clarify let binding rules
This commit is contained in:
29
docs/nix.md
29
docs/nix.md
@@ -131,30 +131,33 @@ Use:
|
||||
|
||||
## `let` Bindings
|
||||
|
||||
### Use `let` for named intermediate values
|
||||
### Use `let` to name concepts
|
||||
|
||||
Use a `let` binding when it names an important intermediate value.
|
||||
Use a `let` binding when it avoids repeated complex expressions.
|
||||
Do not use a `let` binding only to shorten a standard library call.
|
||||
Use a `let` binding when the name explains the value's role.
|
||||
Use a `let` binding when multiple expressions share one important value.
|
||||
Do not use a `let` binding only to shorten a function call.
|
||||
|
||||
Avoid:
|
||||
|
||||
```nix
|
||||
let
|
||||
inherit (lib) mapAttrs;
|
||||
names = lib.attrNames hosts;
|
||||
in
|
||||
mapAttrs f attrs
|
||||
lib.genAttrs names mkHost
|
||||
```
|
||||
|
||||
Use:
|
||||
|
||||
```nix
|
||||
lib.mapAttrs f attrs
|
||||
let
|
||||
hostNames = lib.attrNames hosts;
|
||||
in
|
||||
lib.genAttrs hostNames mkHost
|
||||
```
|
||||
|
||||
### Keep `let` scopes local
|
||||
### Keep `let` bindings near their use
|
||||
|
||||
Place a `let` binding near the expression that needs it.
|
||||
Place a `let` binding in the smallest expression that needs it.
|
||||
Do not lift a value to a larger scope only for convenience.
|
||||
A larger scope makes the dependency look more important than it is.
|
||||
|
||||
@@ -165,8 +168,8 @@ let
|
||||
hostNames = lib.attrNames hosts;
|
||||
in
|
||||
{
|
||||
names = hostNames;
|
||||
count = lib.length hostNames;
|
||||
hostCount = lib.length hostNames;
|
||||
nixosConfigurations = flakeAssembly.mkHosts (self + "/hosts");
|
||||
}
|
||||
```
|
||||
|
||||
@@ -174,8 +177,8 @@ Use:
|
||||
|
||||
```nix
|
||||
{
|
||||
names = lib.attrNames hosts;
|
||||
count = lib.length (lib.attrNames hosts);
|
||||
hostCount = lib.length (lib.attrNames hosts);
|
||||
nixosConfigurations = flakeAssembly.mkHosts (self + "/hosts");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user