Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

treewide: Fix AST of Nix in all markdown files #299554

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "...";
roberth marked this conversation as resolved.
Show resolved Hide resolved
}
```

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
14 changes: 7 additions & 7 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://www.example.org/hello-1.0.tar.gz";
hash = "sha256-lTeyxzJNQeMdu1IVdovNMtgn77jRIhSybLdMbTkf2Ww=";
};
}
```

A common mistake is to update a fetcher鈥檚 URL, or a version parameter, without updating the hash.
Expand All @@ -39,7 +39,7 @@ A common mistake is to update a fetcher鈥檚 URL, or a version parameter, without
fetchurl {
url = "http://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://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 Expand Up @@ -243,7 +243,7 @@ This is a useful last-resort workaround for license restrictions that prohibit r
If the requested file is present in the Nix store, the resulting derivation will not be built, because its expected output is already available.
Otherwise, the builder will run, but fail with a message explaining to the user how to provide the file. The following code, for example:

```
```nix
requireFile {
name = "jdk-${version}_linux-x64_bin.tar.gz";
url = "https://www.oracle.com/java/technologies/javase-jdk11-downloads.html";
Expand All @@ -270,7 +270,7 @@ It produces packages that cannot be built automatically.

`fetchtorrent` expects two arguments. `url` which can either be a Magnet URI (Magnet Link) such as `magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c` or an HTTP URL pointing to a `.torrent` file. It can also take a `config` argument which will craft a `settings.json` configuration file and give it to `transmission`, the underlying program that is performing the fetch. The available config options for `transmission` can be found [here](https://github.com/transmission/transmission/blob/main/docs/Editing-Configuration-Files.md#options)

```
```nix
{ fetchtorrent }:

fetchtorrent {
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
Loading