Skip to content

Commit d904754

Browse files
committed
docs: creating templates
1 parent 1faf95c commit d904754

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-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

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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, create a `flake.nix` in the root directory of your Git repository with the following content:
6+
```
7+
outputs = { self }: {
8+
9+
templates.rust = {
10+
path = ./rust;
11+
description = "A simple Rust/Cargo project";
12+
welcomeText = ''
13+
# Simple Rust/Cargo Template
14+
## Intended usage
15+
The intended usage is...
16+
17+
## More info
18+
- [Rust language](https://www.rust-lang.org/)
19+
- [Rust on the NixOS Wiki](https://wiki.nixos.org/wiki/Rust)
20+
- ...
21+
'';
22+
};
23+
24+
templates.default = self.templates.rust;
25+
}
26+
```
27+
28+
A template has the following attributes:
29+
- `description`: A one-line description of the template, in CommonMark syntax.
30+
- `path`: The path of the directory to be copied. This directory can be created with `devenv init rust`.
31+
- `welcomeText`: A block of markdown text to display when a user initializes a new project based on this template.
32+
33+
!!! note "What is the difference between devenv and Flake templates?"
34+
The only difference is the contents in the template `path` directory.
35+
Nix does not enforce any rules upon the contents of this directory.
36+
Currently the commands `devenv init --template` and `nix flake init --template` are interchangeable. If you get a devenv project or not, depends on the contents of the template.
37+
38+
Template consumers can then use the following command to use the `default` template:
39+
```
40+
devenv init --template github:owner/repo
41+
```
42+
43+
The reference to the Github repository is called a Flake reference. Some more examples:
44+
- `github:owner/repo#rust`: The `rust` template.
45+
- `github:owner/repo/some-branch`: The `default` template from the `some-branch` Git branch.
46+
- `github:owner/repo?dir=some-dir`: The `default` template from the `flake.nix` inside the `some-dir` directory.
47+
- `gitlab:owner/repo`: The `default` template from a Gitlab repository.
48+
49+
When testing you may also use a local directory path.
50+
51+
see [Flake references](https://nix.dev/manual/nix/2.26/command-ref/new-cli/nix3-flake#flake-references) for the official documentation.
52+
53+
## Imports
54+
Imports can be used to split `devenv.nix` when making more advanced templates.
55+
56+
```nix title="devenv.nix"
57+
{ config, lib, ... }:
58+
{
59+
imports = [ ./another-file.nix ];
60+
}
61+
```
62+
63+
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.
64+
65+
```yaml title="devenv.yaml"
66+
inputs:
67+
nixpkgs:
68+
url: github:cachix/devenv-nixpkgs/rolling
69+
templates:
70+
url: github:owner/repo
71+
imports:
72+
- templates
73+
```
74+
75+
The specified repository is usually the same repository where the template itself is located.
76+
77+
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.
78+
79+
!!! note "Defaults"
80+
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.
81+
82+
## Merging existing files
83+
84+
Templates cannot override existing files, however it is possible to implement custom logic to handle this.
85+
86+
First rename the template file (e.g. `.gitignore.devenv-template`) and then add the following to the `devenv.nix` file:
87+
88+
```nix
89+
enterShell = ''
90+
if [ -f .gitignore.devenv-template ]; then
91+
cat .gitignore.devenv-template >> .gitignore
92+
rm .gitignore.devenv-template
93+
fi
94+
'';
95+
```
96+
97+
## Custom options
98+
99+
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)