aboutsummaryrefslogtreecommitdiff
path: root/posts/novice_nix:_flake_templates.md
blob: 63b0b49fdf6e971055e69eaadea5eb7e902c7147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Flakes are very handy to setup entirely pure,
project-specific dependencies (not just dependencies, but
build steps, shell environments and more) in a declarative
way. Writing Flake expressions can get repetitive though,
oftentimes, you'd much rather start off with a skeleton.
Luckily, `nix` already supports templates!

You might already be familiar with `nix flake init`, that
drops a "default" flake expression into your current working
directory. If you head over to the manpage:

```bash
nix flake init --help
```

You will read that `nix flake init` creates a flake using
the "default template". Additionally, you can create a flake
from a specific template by passing the `-t` flag. Where
does this default originate from?

## Flake Registries

Quick detour into registries! Registries are a way to alias
popular flakes using identifiers:

```bash
# list a few predefined registries
$ nix registry list
. . . 
global flake:nixpkgs github:NixOS/nixpkgs
global flake:patchelf github:NixOS/patchelf
global flake:nix-serve github:edolstra/nix-serve
global flake:templates github:NixOS/templates
global flake:nickel github:tweag/nickel
. . .

# you can do 
$ nix flake show nickel

# instead of 
$ nix flake show github:tweag/nickel

# which is short for
$ nix flake show git+https://github.com/tweag/nickel
```

You might notice a registry called `templates` aliased to
`github:NixOS/templates`. Take a peek with `nix flake show`:

```bash
$ nix flake show templates
github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee
├───defaultTemplate: template: A very basic flake
└───templates
    ├───bash-hello: template: An over-engineered Hello World in bash
    ├───c-hello: template: An over-engineered Hello World in C
    ├───rust-web-server: template: A Rust web server including a NixOS module
    ├───simpleContainer: template: A NixOS container running apache-httpd
    └───trivial: template: A very basic flake
```

Aha! There is a flake output called `defaultTemplate`. This
is the template being sourced when you run `nix flake init`.
Astute readers may conclude the following:

```bash
$ nix flake init

# is equivalent to
$ nix flake init -t templates#defaultTemplate

# is equivalent to
$ nix flake init -t github:NixOS/templates#defaultTemplate

# which is short for
$ nix flake init -t git+https://github.com/NixOS/templates#defaultTemplate
```

Similarly, the other templates can be accessed via:

```bash
$ nix flake init -t templates#c-hello
$ nix flake init -t templates#simpleContainer
# I think you get the drift ...
```

## Rolling your own templates

Alright, so all we need to do is:

 - create a flake with a `templates` output
 - populate our template directories with content
 - (**optionally**) alias our custom templates flake to an
   identifier using registries, for easier access

Start off by creating a directory to store your templates in
(we will be converting this to a registry later):

```bash
$ mkdir ~/mytemplates
```

A flake that exposes a "template" as its output looks
something like this:

```nix
# inside ~/mytemplates/flake.nix

{
  description = "Pepper's flake templates";

  outputs = { self, ... }: {
    templates = {
      latex-report = {
        path = ./latex-report-template;
        description = "A latex whitepaper project";
      };
      rust-hello = {
        path = ./rust-hello-template;
        description = "Simple Hello World in Rust";
      };
    };
  };
}
```

The `path` attribute to each template is what gets copied
over when you initialize a flake. Running `nix flake init -t
.#latex-report` will initialize the current directory with
the contents of `./latex-report-template` (we are yet to
populate these directories).

The output of `nix flake show` should be something like:

```bash
$ nix flake show
path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...}
└───templates
    ├───latex-report: template: A latex whitepaper project
    └───rust-hello: template: Simple Hello World in Rust
```

Populate your template directories with content, here are my
template directories for example:

```bash
$ tree mytemplates
mytemplates/
├── flake.nix
├── latex-report-template
│   ├── flake.nix
│   ├── makefile
│   └── src
│       ├── meta.sty
│       └── report.tex
└── rust-hello-template
    ├── Cargo.toml
    ├── flake.nix
    └── src
        └── main.rs
```

And that's it! Start using your templates with:

```bash
$ nix flake init -t ~/mytemplates#rust-hello
$ tree .
.
├── Cargo.toml
├── flake.nix
└── src
    └── main.rs
```

To avoid writing `~/mytemplates` each time, simply alias it
to a registry:

```bash
# alias it to `biscuits`
$ nix registry add biscuits ~/mytemplates

# you will see it listed under `user` registries
$ nix registry list
. . .
user   flake:biscuits path:/home/np/template-tests
. . .

$ nix flake init -t biscuits#latex-report
```

## Extending the official templates

I personally, would like the `biscuits` registry to include
not just my homemade templates, but also the templates from
`NixOS/templates` (and maybe a couple of other repositories
in the wild):

```nix
 {
   description = "Pepper's flake templates";
 
+  inputs = {
+    official-templates.url = github:NixOS/templates;
+    other-templates.url = github:some-other/templates;
+  };
 
   outputs = { self, official-templates, other-templates ... }: {
 
     templates = {
       latex-report = {
         path = ./latex-report-template;
         description = "A latex whitepaper project";
       };
       rust-hello = {
         path = ./rust-hello-template;
         description = "Simple Hello World in Rust, with overloaded Rust toolchain";
       };
     }
+    // official-templates.templates
+    // other-templates.templates;
 
   };
 }
```

Running `nix flake show biscuits` will now list templates
from the `biscuits` registry as well as the ones from
`NixOS/templates`. Ensure that the names don't collide
though.