1# Contributing to the Nixpkgs reference manual
2
3This directory houses the sources files for the Nixpkgs reference manual.
4
5> [!IMPORTANT]
6> We are actively restructuring our documentation to follow the [Diátaxis framework](https://diataxis.fr/)
7>
8> Going forward, this directory should **only** contain [reference documentation](https://nix.dev/contributing/documentation/diataxis#reference).
9> For tutorials, guides and explanations, contribute to <https://nix.dev/> instead.
10>
11> We are actively working to generate **all** reference documentation from the [doc-comments](https://github.com/NixOS/rfcs/blob/master/rfcs/0145-doc-strings.md) present in code.
12> This also provides the benefit of using `:doc` in the `nix repl` to view reference documentation locally on the fly.
13
14For documentation only relevant for contributors, use Markdown files next to the source and regular code comments.
15
16> [!TIP]
17> Feedback for improving support for parsing and rendering doc-comments is highly appreciated.
18> [Open an issue](https://github.com/NixOS/nixpkgs/issues/new?labels=6.topic%3A+documentation&title=Doc%3A+) to request bugfixes or new features.
19
20Rendered documentation:
21- [Unstable (from master)](https://nixos.org/manual/nixpkgs/unstable/)
22- [Stable (from latest release)](https://nixos.org/manual/nixpkgs/stable/)
23
24The rendering tool is [nixos-render-docs](../pkgs/by-name/ni/nixos-render-docs), sometimes abbreviated `nrd`.
25
26## Contributing to this documentation
27
28You can quickly check your edits with `nix-build`:
29
30```ShellSession
31$ cd /path/to/nixpkgs
32$ nix-build doc
33```
34
35If the build succeeds, the manual will be in `./result/share/doc/nixpkgs/manual.html`.
36
37### Development environment
38
39In order to reduce repetition, consider using tools from the provided development environment:
40
41Load it from the Nixpkgs documentation directory with
42
43```ShellSession
44$ cd /path/to/nixpkgs/doc
45$ nix-shell
46```
47
48To load the development utilities automatically when entering that directory, [set up `nix-direnv`](https://nix.dev/guides/recipes/direnv).
49
50Make sure that your local files aren't added to Git history by adding the following lines to `.git/info/exclude` at the root of the Nixpkgs repository:
51
52```
53/**/.envrc
54/**/.direnv
55```
56
57#### `devmode`
58
59Use [`devmode`](../pkgs/by-name/de/devmode/README.md) for a live preview when editing the manual.
60
61### Testing redirects
62
63Once you have a successful build, you can open the relevant HTML (path mentioned above) in a browser along with the anchor, and observe the redirection.
64
65Note that if you already loaded the page and *then* input the anchor, you will need to perform a reload.
66This is because browsers do not re-run client JS code when only the anchor has changed.
67
68## Syntax
69
70As per [RFC 0072](https://github.com/NixOS/rfcs/pull/72), all new documentation content should be written in [CommonMark](https://commonmark.org/) Markdown dialect.
71
72Additional syntax extensions are available, all of which can be used in NixOS option documentation.
73The following extensions are currently used:
74
75#### Tables
76
77Tables, using the [GitHub-flavored Markdown syntax](https://github.github.com/gfm/#tables-extension-).
78
79#### Anchors
80
81Explicitly defined **anchors** on headings, to allow linking to sections.
82These should be always used, to ensure the anchors can be linked even when the heading text changes, and to prevent conflicts between [automatically assigned identifiers](https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/auto_identifiers.md).
83
84It uses the widely compatible [header attributes](https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/attributes.md) syntax:
85
86```markdown
87## Syntax {#sec-contributing-markup}
88```
89
90> [!Note]
91> NixOS option documentation does not support headings in general.
92
93#### Inline Anchors
94
95Allow linking arbitrary place in the text (e.g. individual list items, sentences…).
96
97They are defined using a hybrid of the link syntax with the attributes syntax known from headings, called [bracketed spans](https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/bracketed_spans.md):
98
99```markdown
100- []{#ssec-gnome-hooks-glib} `glib` setup hook will populate `GSETTINGS_SCHEMAS_PATH` and then `wrapGApps*` hook will prepend it to `XDG_DATA_DIRS`.
101```
102
103#### Automatic links
104
105If you **omit a link text** for a link pointing to a section, the text will be substituted automatically.
106For example `[](#chap-contributing)`.
107
108This syntax is taken from [MyST](https://myst-parser.readthedocs.io/en/latest/using/syntax.html#targets-and-cross-referencing).
109
110
111#### HTML
112
113Inlining HTML is not allowed.
114Parts of the documentation gets rendered to various non-HTML formats, such as man pages in the case of NixOS manual.
115
116#### Roles
117
118If you want to link to a man page, you can use `` {manpage}`nix.conf(5)` ``.
119The references will turn into links when a mapping exists in [`doc/manpage-urls.json`](./manpage-urls.json).
120Please keep the `manpage-urls.json` file alphabetically sorted.
121
122A few markups for other kinds of literals are also available:
123
124- `` {command}`rm -rfi` ``
125- `` {env}`XDG_DATA_DIRS` ``
126- `` {file}`/etc/passwd` ``
127- `` {option}`networking.useDHCP` ``
128- `` {var}`/etc/passwd` ``
129
130These literal kinds are used mostly in NixOS option documentation.
131
132This syntax is taken from [MyST](https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html#roles-an-in-line-extension-point).
133Though, the feature originates from [reStructuredText](https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-manpage) with slightly different syntax.
134They are handled by `myst_role` defined per renderer. <!-- reverse references in code -->
135
136#### Admonitions
137
138Set off from the text to bring attention to something.
139
140It uses pandoc’s [fenced `div`s syntax](https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/fenced_divs.md):
141
142```markdown
143::: {.warning}
144This is a warning
145:::
146```
147
148The following are supported:
149
150- `caution`
151- `important`
152- `note`
153- `tip`
154- `warning`
155- `example`
156
157Example admonitions require a title to work.
158If you don't provide one, the manual won't be built.
159
160```markdown
161::: {.example #ex-showing-an-example}
162
163# Title for this example
164
165Text for the example.
166:::
167```
168
169#### [Definition lists](https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/definition_lists.md)
170
171For defining a group of terms:
172
173```markdown
174pear
175: green or yellow bulbous fruit
176
177watermelon
178: green fruit with red flesh
179```
180
181## Commit conventions
182
183- Make sure you read about the [commit conventions](../CONTRIBUTING.md#commit-conventions) common to Nixpkgs as a whole.
184
185- If creating a commit purely for documentation changes, format the commit message in the following way:
186
187 ```
188 doc: (documentation summary)
189
190 (Motivation for change, relevant links, additional information.)
191 ```
192
193 Examples:
194
195 * doc: update the kernel config documentation to use `nix-shell`
196 * doc: add information about `nix-update-script`
197
198 Closes #216321.
199
200- If the commit contains more than just documentation changes, follow the commit message format relevant for the rest of the changes.
201
202## Documentation conventions
203
204In an effort to keep the Nixpkgs manual in a consistent style, please follow the conventions below, unless they prevent you from properly documenting something.
205In that case, please open an issue about the particular documentation convention and tag it with a "needs: documentation" label.
206When needed, each convention explain why it exists, so you can make a decision whether to follow it or not based on your particular case.
207Note that these conventions are about the **structure** of the manual (and its source files), not about the content that goes in it.
208You, as the writer of documentation, are still in charge of its content.
209
210### One sentence per line
211
212Put each sentence in its own line.
213This makes reviews and suggestions much easier, since GitHub's review system is based on lines.
214It also helps identifying long sentences at a glance.
215
216### Callouts and examples
217
218Use the [admonition syntax](#admonitions) for callouts and examples.
219
220### Provide self-contained examples
221
222Provide at least one example per function, and make examples self-contained.
223This is easier to understand for beginners.
224It also helps with testing that it actually works – especially once we introduce automation.
225
226Example code should be such that it can be passed to `pkgs.callPackage`.
227Instead of something like:
228
229```nix
230pkgs.dockerTools.buildLayeredImage {
231 name = "hello";
232 contents = [ pkgs.hello ];
233}
234```
235
236Write something like:
237
238```nix
239{ dockerTools, hello }:
240dockerTools.buildLayeredImage {
241 name = "hello";
242 contents = [ hello ];
243}
244```
245
246### REPLs
247
248When showing inputs/outputs of any [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop), such as a shell or the Nix REPL, use a format as you'd see in the REPL, while trying to visually separate inputs from outputs.
249This means that for a shell, you should use a format like the following:
250```shell
251$ nix-build -A hello '<nixpkgs>' \
252 --option require-sigs false \
253 --option trusted-substituters file:///tmp/hello-cache \
254 --option substituters file:///tmp/hello-cache
255/nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1
256```
257Note how the input is preceded by `$` on the first line and indented on subsequent lines, and how the output is provided as you'd see on the shell.
258
259For the Nix REPL, you should use a format like the following:
260```shell
261nix-repl> builtins.attrNames { a = 1; b = 2; }
262[ "a" "b" ]
263```
264Note how the input is preceded by `nix-repl>` and the output is provided as you'd see on the Nix REPL.
265
266### Headings for inputs, outputs and examples
267
268When documenting functions or anything that has inputs/outputs and example usage, use nested headings to clearly separate inputs, outputs, and examples.
269Keep examples as the last nested heading, and link to the examples wherever applicable in the documentation.
270
271The purpose of this convention is to provide a familiar structure for navigating the manual, so any reader can expect to find content related to inputs in an "inputs" heading, examples in an "examples" heading, and so on.
272An example:
273```
274## buildImage
275
276Some explanation about the function here.
277Describe a particular scenario, and point to [](#ex-dockerTools-buildImage), which is an example demonstrating it.
278
279### Inputs
280
281Documentation for the inputs of `buildImage`.
282Perhaps even point to [](#ex-dockerTools-buildImage) again when talking about something specifically linked to it.
283
284### Passthru outputs
285
286Documentation for any passthru outputs of `buildImage`.
287
288### Examples
289
290Note that this is the last nested heading in the `buildImage` section.
291
292:::{.example #ex-dockerTools-buildImage}
293
294# Using `buildImage`
295
296Example of how to use `buildImage` goes here.
297
298:::
299```
300
301### Function arguments
302
303Use [definition lists](#definition-lists) to document function arguments, and the attributes of such arguments as well as their [types](https://nixos.org/manual/nix/stable/language/values).
304For example:
305
306```markdown
307# pkgs.coolFunction {#pkgs.coolFunction}
308
309`pkgs.coolFunction` *`name`* *`config`*
310
311Description of what `callPackage` does.
312
313
314## Inputs {#pkgs-coolFunction-inputs}
315
316If something's special about `coolFunction`'s general argument handling, you can say so here.
317Otherwise, just describe the single argument or start the arguments' definition list without introduction.
318
319*`name`* (String)
320
321: The name of the resulting image.
322
323*`config`* (Attribute set)
324
325: Introduce the parameter. Maybe you have a test to make sure `{ }` is a sensible default; then you can say: these attributes are optional; `{ }` is a valid argument.
326
327 `outputHash` (String; _optional_)
328
329 : A brief explanation including when and when not to pass this attribute.
330
331 : _Default:_ the output path's hash.
332```
333
334Checklist:
335- Start with a synopsis, to show the order of positional arguments.
336- Metavariables are in emphasized code spans: ``` *`arg1`* ```.
337 Metavariables are placeholders where users may write arbitrary expressions.
338 This includes positional arguments.
339- Attribute names are regular code spans: ``` `attr1` ```.
340 These identifiers can _not_ be picked freely by users, so they are _not_ metavariables.
341- _optional_ attributes have a _`Default:`_ if it's easily described as a value.
342- _optional_ attributes have a _`Default behavior:`_ if it's not easily described using a value.
343- Nix types aren't in code spans, because they are not code
344- Nix types are capitalized, to distinguish them from the camelCase Module System types, which _are_ code and behave like functions.
345
346#### Examples
347
348To define a referenceable figure use the following fencing:
349
350```markdown
351:::{.example #an-attribute-set-example}
352# An attribute set example
353
354You can add text before
355
356 ```nix
357 { a = 1; b = 2;}
358 ```
359
360and after code fencing
361:::
362```
363
364Defining examples through the `example` fencing class adds them to a "List of Examples" section after the Table of Contents.
365Though this is not shown in the rendered documentation on nixos.org.
366
367#### Figures
368
369To define a referenceable figure use the following fencing:
370
371```markdown
372::: {.figure #nixos-logo}
373# NixOS Logo
374
375:::
376```
377
378Defining figures through the `figure` fencing class adds them to a `List of Figures` after the `Table of Contents`.
379Though this is not shown in the rendered documentation on nixos.org.
380
381#### Footnotes
382
383To add a foonote explanation, use the following syntax:
384
385```markdown
386Sometimes it's better to add context [^context] in a footnote.
387
388[^context]: This explanation will be rendered at the end of the chapter.
389```
390
391#### Inline comments
392
393Inline comments are supported with following syntax:
394
395```markdown
396<!-- This is an inline comment -->
397```
398
399The comments will not be rendered in the rendered HTML.
400
401#### Link reference definitions
402
403Links can reference a label, for example, to make the link target reusable:
404
405```markdown
406::: {.note}
407Reference links can also be used to [shorten URLs][url-id] and keep the markdown readable.
408:::
409
410[url-id]: https://github.com/NixOS/nixpkgs/blob/19d4f7dc485f74109bd66ef74231285ff797a823/doc/README.md
411```
412
413This syntax is taken from [CommonMark](https://spec.commonmark.org/0.30/#link-reference-definitions).
414
415#### Typographic replacements
416
417Typographic replacements are enabled.
418Check the [list of possible replacement patterns check](https://github.com/executablebooks/markdown-it-py/blob/3613e8016ecafe21709471ee0032a90a4157c2d1/markdown_it/rules_core/replacements.py#L1-L15).
419
420## Getting help
421
422If you need documentation-specific help or reviews, ping [@NixOS/documentation-team](https://github.com/orgs/nixos/teams/documentation-team) on your pull request.