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