aboutsummaryrefslogtreecommitdiff
path: root/docs/index.xml
diff options
context:
space:
mode:
Diffstat (limited to 'docs/index.xml')
-rw-r--r--docs/index.xml152
1 files changed, 152 insertions, 0 deletions
diff --git a/docs/index.xml b/docs/index.xml
index 06bbc87..d45ba57 100644
--- a/docs/index.xml
+++ b/docs/index.xml
@@ -12,6 +12,158 @@
12 <language>en-us</language> 12 <language>en-us</language>
13 <copyright>Creative Commons BY-NC-SA 4.0</copyright> 13 <copyright>Creative Commons BY-NC-SA 4.0</copyright>
14 <item> 14 <item>
15<title>Novice Nix: Flake Templates</title>
16<description>&lt;p&gt;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, &lt;code&gt;nix&lt;/code&gt; already supports templates!&lt;/p&gt;
17&lt;p&gt;You might already be familiar with &lt;code&gt;nix flake init&lt;/code&gt;, that drops a “default” flake expression into your current working directory. If you head over to the manpage:&lt;/p&gt;
18&lt;pre&gt;&lt;code&gt;nix flake init --help&lt;/code&gt;&lt;/pre&gt;
19&lt;p&gt;You will read that &lt;code&gt;nix flake init&lt;/code&gt; creates a flake using the “default template”. Additionally, you can create a flake from a specific template by passing the &lt;code&gt;-t&lt;/code&gt; flag. Where does this default originate from?&lt;/p&gt;
20&lt;h2 id="flake-registries"&gt;Flake Registries&lt;/h2&gt;
21&lt;p&gt;Quick detour into registries! Registries are a way to alias popular flakes using identifiers:&lt;/p&gt;
22&lt;pre&gt;&lt;code&gt;# list a few predefined registries
23$ nix registry list
24. . .
25global flake:nixpkgs github:NixOS/nixpkgs
26global flake:patchelf github:NixOS/patchelf
27global flake:nix-serve github:edolstra/nix-serve
28global flake:templates github:NixOS/templates
29global flake:nickel github:tweag/nickel
30. . .
31
32# you can do
33$ nix flake show nickel
34
35# instead of
36$ nix flake show github:tweag/nickel
37
38# which is short for
39$ nix flake show git+https://github.com/tweag/nickel&lt;/code&gt;&lt;/pre&gt;
40&lt;p&gt;You might notice a registry called &lt;code&gt;templates&lt;/code&gt; aliased to &lt;code&gt;github:NixOS/templates&lt;/code&gt;. Take a peek with &lt;code&gt;nix flake show&lt;/code&gt;:&lt;/p&gt;
41&lt;pre&gt;&lt;code&gt;$ nix flake show templates
42github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee
43├───defaultTemplate: template: A very basic flake
44└───templates
45 ├───bash-hello: template: An over-engineered Hello World in bash
46 ├───c-hello: template: An over-engineered Hello World in C
47 ├───rust-web-server: template: A Rust web server including a NixOS module
48 ├───simpleContainer: template: A NixOS container running apache-httpd
49 └───trivial: template: A very basic flake&lt;/code&gt;&lt;/pre&gt;
50&lt;p&gt;Aha! There is a flake output called &lt;code&gt;defaultTemplate&lt;/code&gt;. This is the template being sourced when you run &lt;code&gt;nix flake init&lt;/code&gt;. Astute readers may conclude the following:&lt;/p&gt;
51&lt;pre&gt;&lt;code&gt;$ nix flake init
52
53# is equivalent to
54$ nix flake init -t templates#defaultTemplate
55
56# is equivalent to
57$ nix flake init -t github:NixOS/templates#defaultTemplate
58
59# which is short for
60$ nix flake init -t git+https://NixOS/templates#defaultTemplate&lt;/code&gt;&lt;/pre&gt;
61&lt;p&gt;Similarly, the other templates can be accessed via:&lt;/p&gt;
62&lt;pre&gt;&lt;code&gt;$ nix flake init -t templates#c-hello
63$ nix flake init -t templates#simpleContainer
64# I think you get the drift ...&lt;/code&gt;&lt;/pre&gt;
65&lt;h2 id="rolling-your-own-templates"&gt;Rolling your own templates&lt;/h2&gt;
66&lt;p&gt;Alright, so all we need to do is:&lt;/p&gt;
67&lt;ul&gt;
68&lt;li&gt;create a flake with a &lt;code&gt;templates&lt;/code&gt; output&lt;/li&gt;
69&lt;li&gt;populate our template directories with content&lt;/li&gt;
70&lt;li&gt;(&lt;strong&gt;optionally&lt;/strong&gt;) alias our custom templates flake to an identifier using registries, for easier access&lt;/li&gt;
71&lt;/ul&gt;
72&lt;p&gt;Start off by creating a directory to store your templates in (we will be converting this to a registry later):&lt;/p&gt;
73&lt;pre&gt;&lt;code&gt;$ mkdir ~/mytemplates&lt;/code&gt;&lt;/pre&gt;
74&lt;p&gt;A flake that exposes a “template” as its output looks something like this:&lt;/p&gt;
75&lt;pre&gt;&lt;code&gt;# inside ~/mytemplates/flake.nix
76
77{
78 description = &amp;quot;Pepper&amp;#39;s flake templates&amp;quot;;
79
80 outputs = { self, ... }: {
81 templates = {
82 latex-report = {
83 path = ./latex-report-template;
84 description = &amp;quot;A latex whitepaper project&amp;quot;;
85 };
86 rust-hello = {
87 path = ./rust-hello-template;
88 description = &amp;quot;Simple Hello World in Rust&amp;quot;;
89 };
90 };
91 };
92}&lt;/code&gt;&lt;/pre&gt;
93&lt;p&gt;The &lt;code&gt;path&lt;/code&gt; attribute to each template is what gets copied over when you initialize a flake. Running &lt;code&gt;nix flake init -t .#latex-report&lt;/code&gt; will initialize the current directory with the contents of &lt;code&gt;./latex-report-template&lt;/code&gt; (we are yet to populate these directories).&lt;/p&gt;
94&lt;p&gt;The output of &lt;code&gt;nix flake show&lt;/code&gt; should be something like:&lt;/p&gt;
95&lt;pre&gt;&lt;code&gt;$ nix flake show
96path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...}
97└───templates
98 ├───latex-report: template: A latex whitepaper project
99 └───rust-hello: template: Simple Hello World in Rust&lt;/code&gt;&lt;/pre&gt;
100&lt;p&gt;Populate your template directories with content, here are my template directories for example:&lt;/p&gt;
101&lt;pre&gt;&lt;code&gt;$ tree mytemplates
102mytemplates/
103├── flake.nix
104├── latex-report-template
105│   ├── flake.nix
106│   ├── makefile
107│   └── src
108│   ├── meta.sty
109│   └── report.tex
110└── rust-hello-template
111 ├── Cargo.toml
112 ├── flake.nix
113 └── src
114 └── main.rs&lt;/code&gt;&lt;/pre&gt;
115&lt;p&gt;And that’s it! Start using your templates with:&lt;/p&gt;
116&lt;pre&gt;&lt;code&gt;$ nix flake init -t ~/mytemplates#rust-hello
117$ tree .
118.
119├── Cargo.toml
120├── flake.nix
121└── src
122 └── main.rs&lt;/code&gt;&lt;/pre&gt;
123&lt;p&gt;To avoid writing &lt;code&gt;~/mytemplates&lt;/code&gt; each time, simply alias it to a registry:&lt;/p&gt;
124&lt;pre&gt;&lt;code&gt;# alias it to `biscuits`
125$ nix registry add biscuits ~/mytemplates
126
127# you will see it listed under `user` registries
128$ nix registry list
129. . .
130user flake:biscuits path:/home/np/template-tests
131. . .
132
133$ nix flake init -t biscuits#latex-report&lt;/code&gt;&lt;/pre&gt;
134&lt;h2 id="extending-the-official-templates"&gt;Extending the official templates&lt;/h2&gt;
135&lt;p&gt;I personally, would like the &lt;code&gt;biscuits&lt;/code&gt; registry to include not just my homemade templates, but also the templates from &lt;code&gt;NixOS/templates&lt;/code&gt; (and maybe a couple of other repositories in the wild):&lt;/p&gt;
136&lt;pre&gt;&lt;code&gt; {
137 description = &amp;quot;Pepper&amp;#39;s flake templates&amp;quot;;
138
139+ inputs = {
140+ official-templates.url = github:NixOS/templates;
141+ other-templates.url = github:some-other/templates;
142+ };
143
144 outputs = { self, official-templates, other-templates ... }: {
145
146 templates = {
147 latex-report = {
148 path = ./latex-report-template;
149 description = &amp;quot;A latex whitepaper project&amp;quot;;
150 };
151 rust-hello = {
152 path = ./rust-hello-template;
153 description = &amp;quot;Simple Hello World in Rust, with overloaded Rust toolchain&amp;quot;;
154 };
155 }
156+ // official-templates.templates
157+ // other-templates.templates;
158
159 };
160 }&lt;/code&gt;&lt;/pre&gt;
161&lt;p&gt;Running &lt;code&gt;nix flake show biscuits&lt;/code&gt; will now list templates from the &lt;code&gt;biscuits&lt;/code&gt; registry as well as the ones from &lt;code&gt;NixOS/templates&lt;/code&gt;. Ensure that the names don’t collide though.&lt;/p&gt;</description>
162<link>https://peppe.rs/posts/novice_nix:_flake_templates/</link>
163<pubDate>Tue, 05 Oct 2021 16:49:00 +0000</pubDate>
164<guid>https://peppe.rs/posts/novice_nix:_flake_templates/</guid>
165</item>
166<item>
15<title>SDL2 Devlog</title> 167<title>SDL2 Devlog</title>
16<description>&lt;p&gt;I have been working on an editor for the &lt;a href="https://git.peppe.rs/graphics/obi/about"&gt;One Bit Image&lt;/a&gt; file format in Rust and SDL2. This entry in my blog follows my progress on the editor. The days are listed in reverse chronological order, begin from the bottom, if this is your first time on this page.&lt;/p&gt; 168<description>&lt;p&gt;I have been working on an editor for the &lt;a href="https://git.peppe.rs/graphics/obi/about"&gt;One Bit Image&lt;/a&gt; file format in Rust and SDL2. This entry in my blog follows my progress on the editor. The days are listed in reverse chronological order, begin from the bottom, if this is your first time on this page.&lt;/p&gt;
17&lt;h3 id="day-20"&gt;Day 20&lt;/h3&gt; 169&lt;h3 id="day-20"&gt;Day 20&lt;/h3&gt;