Skip to content

Commit

Permalink
treewide: Fix all Nix ASTs in all markdown files
Browse files Browse the repository at this point in the history
This allows for correct highlighting and maybe future automatic
formatting. The AST was verified to work with nixfmt only.
  • Loading branch information
dasJ authored and fricklerhandwerk committed Mar 28, 2024
1 parent bc77c7a commit fcc95ff
Show file tree
Hide file tree
Showing 150 changed files with 2,944 additions and 2,135 deletions.
108 changes: 60 additions & 48 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ Names of files and directories should be in lowercase, with dashes between words

```nix
foo {
arg = ...;
arg = "...";
}
```

Expand All @@ -566,56 +566,60 @@ Names of files and directories should be in lowercase, with dashes between words
```nix
foo
{
arg = ...;
arg = "...";
}
```

Also fine is

```nix
foo { arg = ...; }
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";
}
];
{
# 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 list.
list = [ elem1 elem2 elem3 ];
# A short set.
attrs = { x = 1280; y = 1024; };
# A short set.
attrs = { x = 1280; y = 1024; };
}
```

- Breaking in the middle of a function argument can give hard-to-read code, like
Expand Down Expand Up @@ -649,49 +653,49 @@ Names of files and directories should be in lowercase, with dashes between words
```nix
{ arg1, arg2 }:
assert system == "i686-linux";
stdenv.mkDerivation { ...
stdenv.mkDerivation { /* ... */ }
```

not

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

- Function formal arguments are written as:

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

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

```nix
{ arg1, arg2, arg3
, arg4, ...
, # Some comment...
argN
}:
, arg4
# Some comment...
, argN
}: { }
```

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

```nix
{ stdenv, fetchurl, perl }: ...
{ stdenv, fetchurl, perl }: "..."
```

instead of

```nix
args: with args; ...
args: with args; "..."
```

or

```nix
{ stdenv, fetchurl, perl, ... }: ...
{ stdenv, fetchurl, perl, ... }: "..."
```

For functions that are truly generic in the number of arguments (such as wrappers around `mkDerivation`) that have some required arguments, you should write them using an `@`-pattern:
Expand All @@ -700,7 +704,7 @@ Names of files and directories should be in lowercase, with dashes between words
{ stdenv, doCoverageAnalysis ? false, ... } @ args:
stdenv.mkDerivation (args // {
... if doCoverageAnalysis then "bla" else "" ...
foo = if doCoverageAnalysis then "bla" else "";
})
```

Expand All @@ -710,32 +714,40 @@ Names of files and directories should be in lowercase, with dashes between words
args:
args.stdenv.mkDerivation (args // {
... if args ? doCoverageAnalysis && args.doCoverageAnalysis then "bla" else "" ...
foo = if args ? doCoverageAnalysis && args.doCoverageAnalysis then "bla" else "";
})
```

- Unnecessary string conversions should be avoided. Do

```nix
rev = version;
{
rev = version;
}
```

instead of

```nix
rev = "${version}";
{
rev = "${version}";
}
```

- Building lists conditionally _should_ be done with `lib.optional(s)` instead of using `if cond then [ ... ] else null` or `if cond then [ ... ] else [ ]`.

```nix
buildInputs = lib.optional stdenv.isDarwin iconv;
{
buildInputs = lib.optional stdenv.isDarwin iconv;
}
```

instead of

```nix
buildInputs = if stdenv.isDarwin then [ iconv ] else null;
{
buildInputs = if stdenv.isDarwin then [ iconv ] else null;
}
```

As an exception, an explicit conditional expression with null can be used when fixing a important bug without triggering a mass rebuild.
Expand Down
10 changes: 5 additions & 5 deletions doc/build-helpers/fetchers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ For example, consider the following fetcher:
fetchurl {
url = "http:https://www.example.org/hello-1.0.tar.gz";
hash = "sha256-lTeyxzJNQeMdu1IVdovNMtgn77jRIhSybLdMbTkf2Ww=";
};
}
```

A common mistake is to update a fetcher’s URL, or a version parameter, without updating the hash.
Expand All @@ -39,7 +39,7 @@ A common mistake is to update a fetcher’s URL, or a version parameter, without
fetchurl {
url = "http:https://www.example.org/hello-1.1.tar.gz";
hash = "sha256-lTeyxzJNQeMdu1IVdovNMtgn77jRIhSybLdMbTkf2Ww=";
};
}
```

**This will reuse the old contents**.
Expand All @@ -49,7 +49,7 @@ Remember to invalidate the hash argument, in this case by setting the `hash` att
fetchurl {
url = "http:https://www.example.org/hello-1.1.tar.gz";
hash = "";
};
}
```

Use the resulting error message to determine the correct hash.
Expand Down Expand Up @@ -123,7 +123,7 @@ Here is an example of `fetchDebianPatch` in action:
buildPythonPackage rec {
pname = "pysimplesoap";
version = "1.16.2";
src = ...;
src = "...";
patches = [
(fetchDebianPatch {
Expand All @@ -134,7 +134,7 @@ buildPythonPackage rec {
})
];
...
# ...
}
```

Expand Down
1 change: 1 addition & 0 deletions doc/build-helpers/images/dockertools.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ dockerTools.buildImage {
hello
dockerTools.binSh
];
}
```
After building the image and loading it in Docker, we can create a container based on it and enter a shell inside the container.
Expand Down
12 changes: 8 additions & 4 deletions doc/build-helpers/special/checkpoint-build.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ However, we can tell Nix explicitly what the previous build state was, by repres
To change a normal derivation to a checkpoint based build, these steps must be taken:
- apply `prepareCheckpointBuild` on the desired derivation, e.g.
```nix
checkpointArtifacts = (pkgs.checkpointBuildTools.prepareCheckpointBuild pkgs.virtualbox);
{
checkpointArtifacts = (pkgs.checkpointBuildTools.prepareCheckpointBuild pkgs.virtualbox);
}
```
- change something you want in the sources of the package, e.g. use a source override:
```nix
changedVBox = pkgs.virtualbox.overrideAttrs (old: {
src = path/to/vbox/sources;
});
{
changedVBox = pkgs.virtualbox.overrideAttrs (old: {
src = path/to/vbox/sources;
});
}
```
- use `mkCheckpointBuild changedVBox checkpointArtifacts`
- enjoy shorter build times
Expand Down
54 changes: 32 additions & 22 deletions doc/build-helpers/testers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ If the `moduleNames` argument is omitted, `hasPkgConfigModules` will use `meta.p
# Check that `pkg-config` modules are exposed using default values

```nix
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
};
{
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
};
meta.pkgConfigModules = [ "libfoo" ];
meta.pkgConfigModules = [ "libfoo" ];
}
```

:::
Expand All @@ -28,10 +30,12 @@ meta.pkgConfigModules = [ "libfoo" ];
# Check that `pkg-config` modules are exposed using explicit module names

```nix
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
moduleNames = [ "libfoo" ];
};
{
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
moduleNames = [ "libfoo" ];
};
}
```

:::
Expand All @@ -55,7 +59,9 @@ The default argument to the command is `--version`, and the version to be checke
This example will run the command `hello --version`, and then check that the version of the `hello` package is in the output of the command.

```nix
passthru.tests.version = testers.testVersion { package = hello; };
{
passthru.tests.version = testers.testVersion { package = hello; };
}
```

:::
Expand All @@ -70,13 +76,15 @@ This means that an output like "leetcode 0.4.21" would fail the tests, and an ou
A common usage of the `version` attribute is to specify `version = "v${version}"`.

```nix
version = "0.4.2";
{
version = "0.4.2";
passthru.tests.version = testers.testVersion {
package = leetcode-cli;
command = "leetcode -V";
version = "leetcode ${version}";
};
passthru.tests.version = testers.testVersion {
package = leetcode-cli;
command = "leetcode -V";
version = "leetcode ${version}";
};
}
```

:::
Expand Down Expand Up @@ -116,7 +124,7 @@ runCommand "example" {
grep -F 'failing though' $failed/testBuildFailure.log
[[ 3 = $(cat $failed/testBuildFailure.exit) ]]
touch $out
'';
''
```

:::
Expand Down Expand Up @@ -193,12 +201,14 @@ once to get a derivation hash, and again to produce the final fixed output deriv
# Prevent nix from reusing the output of a fetcher

```nix
tests.fetchgit = testers.invalidateFetcherByDrvHash fetchgit {
name = "nix-source";
url = "https://github.com/NixOS/nix";
rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a";
hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY=";
};
{
tests.fetchgit = testers.invalidateFetcherByDrvHash fetchgit {
name = "nix-source";
url = "https://github.com/NixOS/nix";
rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a";
hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY=";
};
}
```

:::
Expand Down

0 comments on commit fcc95ff

Please sign in to comment.