Skip to content

Commit d2da73a

Browse files
committed
docs: creating templates
1 parent 53d1431 commit d2da73a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

docs/guides/.pages

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
nav:
22
- Using with Flakes: using-with-flakes.md
33
- Using with flake.parts: using-with-flake-parts.md
4+
- Creating templates: creating-templates.md

docs/guides/creating-templates.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
The following guide describes how to extend devenv and provide templates for other users. It is possible to bootstrap `devenv.nix` with custom content, change defaults or even define custom devenv options.
2+
3+
## Templates
4+
5+
To provide a devenv template, add the following to your `devenv.nix` file:
6+
```nix title="devenv.nix"
7+
templates.rust = {
8+
path = ./rust;
9+
description = "A simple Rust/Cargo project";
10+
welcomeText = ''
11+
# Simple Rust/Cargo Template
12+
## Intended usage
13+
The intended usage is...
14+
15+
## More info
16+
- [Rust language](https://www.rust-lang.org/)
17+
- [Rust on the NixOS Wiki](https://wiki.nixos.org/wiki/Rust)
18+
- ...
19+
'';
20+
};
21+
22+
templates.default = config.templates.rust;
23+
}
24+
```
25+
26+
A template has the following attributes:
27+
- `description`: A one-line description of the template, in CommonMark syntax.
28+
- `path`: The path of the directory to be copied. This directory can be created with `devenv init rust`.
29+
- `welcomeText`: A block of markdown text to display when a user initializes a new project based on this template.
30+
31+
Template consumers can then use the following command to use the `default` template:
32+
```
33+
devenv init --template github:owner/repo
34+
```
35+
36+
The reference to the Github repository is called a Flake reference. Some more examples:
37+
- `github:owner/repo#rust`: The `rust` template.
38+
- `github:owner/repo/some-branch`: The `default` template from the `some-branch` Git branch.
39+
- `github:owner/repo?dir=some-dir`: The `default` template from the `flake.nix` inside the `some-dir` directory.
40+
- `gitlab:owner/repo`: The `default` template from a Gitlab repository.
41+
42+
When testing you may also use a local directory path.
43+
44+
see [Flake references](https://nix.dev/manual/nix/2.26/command-ref/new-cli/nix3-flake#flake-references) for the official documentation.
45+
46+
## Imports
47+
Imports can be used to split `devenv.nix` when making more advanced templates.
48+
49+
```nix title="devenv.nix"
50+
{ config, lib, ... }:
51+
{
52+
imports = [ ./another-file.nix ];
53+
}
54+
```
55+
56+
It is also possible to import from an external repository. This method is recommended for implementation details that the template consumer is not supposed to modify.
57+
58+
```yaml title="devenv.yaml"
59+
inputs:
60+
nixpkgs:
61+
url: github:cachix/devenv-nixpkgs/rolling
62+
templates:
63+
url: github:owner/repo
64+
imports:
65+
- templates
66+
```
67+
68+
The specified repository is usually the same repository where the template itself is located.
69+
70+
The import always begins with the input name, but may include a directory (e.g. `templates/some/directory`). In the example above the import will look for a `devenv.nix` file in the Git root directory.
71+
72+
!!! note "Defaults"
73+
To set defaults prepend the value with `lib.mkDefault` so that the template consumer can change the value (e.g. `enable = lib.mkDefault true`). Lists are automatically merged and don't require `lib.mkDefault`. The template can specify a list of default packages and these are then merged with the users packages.
74+
75+
## Merging existing files
76+
77+
Templates cannot override existing files, however it is possible to implement custom logic to handle this.
78+
79+
First rename the template file (e.g. `.gitignore.devenv-template`) and then add the following to the `devenv.nix` file:
80+
81+
```nix
82+
enterShell = ''
83+
if [ -f .gitignore.devenv-template ]; then
84+
cat .gitignore.devenv-template >> .gitignore
85+
rm .gitignore.devenv-template
86+
fi
87+
'';
88+
```
89+
90+
## Custom options
91+
92+
It is possible to extend devenv with additional modules and options. See <https://nix.dev/tutorials/module-system/deep-dive.html>

0 commit comments

Comments
 (0)