diff options
-rw-r--r-- | docs/index.xml | 242 | ||||
-rw-r--r-- | docs/posts/novice_nix:_flake_templates/index.html | 242 | ||||
-rw-r--r-- | posts/novice_nix:_flake_templates.md | 24 |
3 files changed, 254 insertions, 254 deletions
diff --git a/docs/index.xml b/docs/index.xml index d45ba57..3246f7a 100644 --- a/docs/index.xml +++ b/docs/index.xml | |||
@@ -15,53 +15,53 @@ | |||
15 | <title>Novice Nix: Flake Templates</title> | 15 | <title>Novice Nix: Flake Templates</title> |
16 | <description><p>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, <code>nix</code> already supports templates!</p> | 16 | <description><p>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, <code>nix</code> already supports templates!</p> |
17 | <p>You might already be familiar with <code>nix flake init</code>, that drops a “default” flake expression into your current working directory. If you head over to the manpage:</p> | 17 | <p>You might already be familiar with <code>nix flake init</code>, that drops a “default” flake expression into your current working directory. If you head over to the manpage:</p> |
18 | <pre><code>nix flake init --help</code></pre> | 18 | <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">nix</span> flake init <span class="at">--help</span></span></code></pre></div> |
19 | <p>You will read that <code>nix flake init</code> creates a flake using the “default template”. Additionally, you can create a flake from a specific template by passing the <code>-t</code> flag. Where does this default originate from?</p> | 19 | <p>You will read that <code>nix flake init</code> creates a flake using the “default template”. Additionally, you can create a flake from a specific template by passing the <code>-t</code> flag. Where does this default originate from?</p> |
20 | <h2 id="flake-registries">Flake Registries</h2> | 20 | <h2 id="flake-registries">Flake Registries</h2> |
21 | <p>Quick detour into registries! Registries are a way to alias popular flakes using identifiers:</p> | 21 | <p>Quick detour into registries! Registries are a way to alias popular flakes using identifiers:</p> |
22 | <pre><code># list a few predefined registries | 22 | <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># list a few predefined registries</span></span> |
23 | $ nix registry list | 23 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry list</span> |
24 | . . . | 24 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . . </span> |
25 | global flake:nixpkgs github:NixOS/nixpkgs | 25 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:nixpkgs github:NixOS/nixpkgs</span> |
26 | global flake:patchelf github:NixOS/patchelf | 26 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:patchelf github:NixOS/patchelf</span> |
27 | global flake:nix-serve github:edolstra/nix-serve | 27 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:nix-serve github:edolstra/nix-serve</span> |
28 | global flake:templates github:NixOS/templates | 28 | <span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:templates github:NixOS/templates</span> |
29 | global flake:nickel github:tweag/nickel | 29 | <span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:nickel github:tweag/nickel</span> |
30 | . . . | 30 | <span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> |
31 | 31 | <span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a></span> | |
32 | # you can do | 32 | <span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="co"># you can do </span></span> |
33 | $ nix flake show nickel | 33 | <span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show nickel</span> |
34 | 34 | <span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a></span> | |
35 | # instead of | 35 | <span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="co"># instead of </span></span> |
36 | $ nix flake show github:tweag/nickel | 36 | <span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show github:tweag/nickel</span> |
37 | 37 | <span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a></span> | |
38 | # which is short for | 38 | <span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="co"># which is short for</span></span> |
39 | $ nix flake show git+https://github.com/tweag/nickel</code></pre> | 39 | <span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show git+https://github.com/tweag/nickel</span></code></pre></div> |
40 | <p>You might notice a registry called <code>templates</code> aliased to <code>github:NixOS/templates</code>. Take a peek with <code>nix flake show</code>:</p> | 40 | <p>You might notice a registry called <code>templates</code> aliased to <code>github:NixOS/templates</code>. Take a peek with <code>nix flake show</code>:</p> |
41 | <pre><code>$ nix flake show templates | 41 | <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show templates</span> |
42 | github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee | 42 | <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ex">github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee</span></span> |
43 | ├───defaultTemplate: template: A very basic flake | 43 | <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="ex">├───defaultTemplate:</span> template: A very basic flake</span> |
44 | └───templates | 44 | <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="ex">└───templates</span></span> |
45 | ├───bash-hello: template: An over-engineered Hello World in bash | 45 | <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───bash-hello:</span> template: An over-engineered Hello World in bash</span> |
46 | ├───c-hello: template: An over-engineered Hello World in C | 46 | <span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───c-hello:</span> template: An over-engineered Hello World in C</span> |
47 | ├───rust-web-server: template: A Rust web server including a NixOS module | 47 | <span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───rust-web-server:</span> template: A Rust web server including a NixOS module</span> |
48 | ├───simpleContainer: template: A NixOS container running apache-httpd | 48 | <span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───simpleContainer:</span> template: A NixOS container running apache-httpd</span> |
49 | └───trivial: template: A very basic flake</code></pre> | 49 | <span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">└───trivial:</span> template: A very basic flake</span></code></pre></div> |
50 | <p>Aha! There is a flake output called <code>defaultTemplate</code>. This is the template being sourced when you run <code>nix flake init</code>. Astute readers may conclude the following:</p> | 50 | <p>Aha! There is a flake output called <code>defaultTemplate</code>. This is the template being sourced when you run <code>nix flake init</code>. Astute readers may conclude the following:</p> |
51 | <pre><code>$ nix flake init | 51 | <div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init</span> |
52 | 52 | <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span> | |
53 | # is equivalent to | 53 | <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co"># is equivalent to</span></span> |
54 | $ nix flake init -t templates#defaultTemplate | 54 | <span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> templates#defaultTemplate</span> |
55 | 55 | <span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a></span> | |
56 | # is equivalent to | 56 | <span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="co"># is equivalent to</span></span> |
57 | $ nix flake init -t github:NixOS/templates#defaultTemplate | 57 | <span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> github:NixOS/templates#defaultTemplate</span> |
58 | 58 | <span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a></span> | |
59 | # which is short for | 59 | <span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="co"># which is short for</span></span> |
60 | $ nix flake init -t git+https://NixOS/templates#defaultTemplate</code></pre> | 60 | <span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> git+https://NixOS/templates#defaultTemplate</span></code></pre></div> |
61 | <p>Similarly, the other templates can be accessed via:</p> | 61 | <p>Similarly, the other templates can be accessed via:</p> |
62 | <pre><code>$ nix flake init -t templates#c-hello | 62 | <div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> templates#c-hello</span> |
63 | $ nix flake init -t templates#simpleContainer | 63 | <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> templates#simpleContainer</span> |
64 | # I think you get the drift ...</code></pre> | 64 | <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="co"># I think you get the drift ...</span></span></code></pre></div> |
65 | <h2 id="rolling-your-own-templates">Rolling your own templates</h2> | 65 | <h2 id="rolling-your-own-templates">Rolling your own templates</h2> |
66 | <p>Alright, so all we need to do is:</p> | 66 | <p>Alright, so all we need to do is:</p> |
67 | <ul> | 67 | <ul> |
@@ -70,94 +70,94 @@ $ nix flake init -t templates#simpleContainer | |||
70 | <li>(<strong>optionally</strong>) alias our custom templates flake to an identifier using registries, for easier access</li> | 70 | <li>(<strong>optionally</strong>) alias our custom templates flake to an identifier using registries, for easier access</li> |
71 | </ul> | 71 | </ul> |
72 | <p>Start off by creating a directory to store your templates in (we will be converting this to a registry later):</p> | 72 | <p>Start off by creating a directory to store your templates in (we will be converting this to a registry later):</p> |
73 | <pre><code>$ mkdir ~/mytemplates</code></pre> | 73 | <div class="sourceCode" id="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> mkdir ~/mytemplates</span></code></pre></div> |
74 | <p>A flake that exposes a “template” as its output looks something like this:</p> | 74 | <p>A flake that exposes a “template” as its output looks something like this:</p> |
75 | <pre><code># inside ~/mytemplates/flake.nix | 75 | <div class="sourceCode" id="cb7"><pre class="sourceCode nix"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># inside ~/mytemplates/flake.nix</span></span> |
76 | 76 | <span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span> | |
77 | { | 77 | <span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="kw">{</span></span> |
78 | description = &quot;Pepper&#39;s flake templates&quot;; | 78 | <span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">&quot;Pepper&#39;s flake templates&quot;</span><span class="kw">;</span></span> |
79 | 79 | <span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a></span> | |
80 | outputs = { self, ... }: { | 80 | <span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="ex">outputs</span> = { self, ... }: {</span> |
81 | templates = { | 81 | <span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">templates</span> = {</span> |
82 | latex-report = { | 82 | <span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="ex">latex-report</span> = {</span> |
83 | path = ./latex-report-template; | 83 | <span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./latex-report-template<span class="kw">;</span></span> |
84 | description = &quot;A latex whitepaper project&quot;; | 84 | <span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">&quot;A latex whitepaper project&quot;</span><span class="kw">;</span></span> |
85 | }; | 85 | <span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="kw">};</span></span> |
86 | rust-hello = { | 86 | <span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">rust-hello</span> = {</span> |
87 | path = ./rust-hello-template; | 87 | <span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./rust-hello-template<span class="kw">;</span></span> |
88 | description = &quot;Simple Hello World in Rust&quot;; | 88 | <span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">&quot;Simple Hello World in Rust&quot;</span><span class="kw">;</span></span> |
89 | }; | 89 | <span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
90 | }; | 90 | <span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
91 | }; | 91 | <span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
92 | }</code></pre> | 92 | <span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a><span class="er">}</span></span></code></pre></div> |
93 | <p>The <code>path</code> attribute to each template is what gets copied over when you initialize a flake. Running <code>nix flake init -t .#latex-report</code> will initialize the current directory with the contents of <code>./latex-report-template</code> (we are yet to populate these directories).</p> | 93 | <p>The <code>path</code> attribute to each template is what gets copied over when you initialize a flake. Running <code>nix flake init -t .#latex-report</code> will initialize the current directory with the contents of <code>./latex-report-template</code> (we are yet to populate these directories).</p> |
94 | <p>The output of <code>nix flake show</code> should be something like:</p> | 94 | <p>The output of <code>nix flake show</code> should be something like:</p> |
95 | <pre><code>$ nix flake show | 95 | <div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show</span> |
96 | path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...} | 96 | <span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="ex">path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...}</span></span> |
97 | └───templates | 97 | <span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="ex">└───templates</span></span> |
98 | ├───latex-report: template: A latex whitepaper project | 98 | <span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───latex-report:</span> template: A latex whitepaper project</span> |
99 | └───rust-hello: template: Simple Hello World in Rust</code></pre> | 99 | <span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="ex">└───rust-hello:</span> template: Simple Hello World in Rust</span></code></pre></div> |
100 | <p>Populate your template directories with content, here are my template directories for example:</p> | 100 | <p>Populate your template directories with content, here are my template directories for example:</p> |
101 | <pre><code>$ tree mytemplates | 101 | <div class="sourceCode" id="cb9"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> tree mytemplates</span> |
102 | mytemplates/ | 102 | <span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="ex">mytemplates/</span></span> |
103 | ├── flake.nix | 103 | <span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> flake.nix</span> |
104 | ├── latex-report-template | 104 | <span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> latex-report-template</span> |
105 | │ ├── flake.nix | 105 | <span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> ├── flake.nix</span> |
106 | │ ├── makefile | 106 | <span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> ├── makefile</span> |
107 | │ └── src | 107 | <span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> └── src</span> |
108 | │ ├── meta.sty | 108 | <span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> ├── meta.sty</span> |
109 | │ └── report.tex | 109 | <span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> └── report.tex</span> |
110 | └── rust-hello-template | 110 | <span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a><span class="ex">└──</span> rust-hello-template</span> |
111 | ├── Cargo.toml | 111 | <span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a> <span class="ex">├──</span> Cargo.toml</span> |
112 | ├── flake.nix | 112 | <span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">├──</span> flake.nix</span> |
113 | └── src | 113 | <span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> src</span> |
114 | └── main.rs</code></pre> | 114 | <span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> main.rs</span></code></pre></div> |
115 | <p>And that’s it! Start using your templates with:</p> | 115 | <p>And that’s it! Start using your templates with:</p> |
116 | <pre><code>$ nix flake init -t ~/mytemplates#rust-hello | 116 | <div class="sourceCode" id="cb10"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> ~/mytemplates#rust-hello</span> |
117 | $ tree . | 117 | <span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> tree .</span> |
118 | . | 118 | <span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span></span> |
119 | ├── Cargo.toml | 119 | <span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> Cargo.toml</span> |
120 | ├── flake.nix | 120 | <span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> flake.nix</span> |
121 | └── src | 121 | <span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span class="ex">└──</span> src</span> |
122 | └── main.rs</code></pre> | 122 | <span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> main.rs</span></code></pre></div> |
123 | <p>To avoid writing <code>~/mytemplates</code> each time, simply alias it to a registry:</p> | 123 | <p>To avoid writing <code>~/mytemplates</code> each time, simply alias it to a registry:</p> |
124 | <pre><code># alias it to `biscuits` | 124 | <div class="sourceCode" id="cb11"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co"># alias it to `biscuits`</span></span> |
125 | $ nix registry add biscuits ~/mytemplates | 125 | <span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry add biscuits ~/mytemplates</span> |
126 | 126 | <span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a></span> | |
127 | # you will see it listed under `user` registries | 127 | <span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a><span class="co"># you will see it listed under `user` registries</span></span> |
128 | $ nix registry list | 128 | <span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry list</span> |
129 | . . . | 129 | <span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> |
130 | user flake:biscuits path:/home/np/template-tests | 130 | <span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="ex">user</span> flake:biscuits path:/home/np/template-tests</span> |
131 | . . . | 131 | <span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> |
132 | 132 | <span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a></span> | |
133 | $ nix flake init -t biscuits#latex-report</code></pre> | 133 | <span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> biscuits#latex-report</span></code></pre></div> |
134 | <h2 id="extending-the-official-templates">Extending the official templates</h2> | 134 | <h2 id="extending-the-official-templates">Extending the official templates</h2> |
135 | <p>I personally, would like the <code>biscuits</code> registry to include not just my homemade templates, but also the templates from <code>NixOS/templates</code> (and maybe a couple of other repositories in the wild):</p> | 135 | <p>I personally, would like the <code>biscuits</code> registry to include not just my homemade templates, but also the templates from <code>NixOS/templates</code> (and maybe a couple of other repositories in the wild):</p> |
136 | <pre><code> { | 136 | <div class="sourceCode" id="cb12"><pre class="sourceCode nix"><code class="sourceCode bash"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">{</span></span> |
137 | description = &quot;Pepper&#39;s flake templates&quot;; | 137 | <span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">&quot;Pepper&#39;s flake templates&quot;</span><span class="kw">;</span></span> |
138 | 138 | <span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> </span> | |
139 | + inputs = { | 139 | <span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> inputs = {</span> |
140 | + official-templates.url = github:NixOS/templates; | 140 | <span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> official-templates.url = github:NixOS/templates<span class="kw">;</span></span> |
141 | + other-templates.url = github:some-other/templates; | 141 | <span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> other-templates.url = github:some-other/templates<span class="kw">;</span></span> |
142 | + }; | 142 | <span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> }<span class="kw">;</span></span> |
143 | 143 | <span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a> </span> | |
144 | outputs = { self, official-templates, other-templates ... }: { | 144 | <span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">outputs</span> = { self, official-templates, other-templates ... }: {</span> |
145 | 145 | <span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a> </span> | |
146 | templates = { | 146 | <span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a> <span class="ex">templates</span> = {</span> |
147 | latex-report = { | 147 | <span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">latex-report</span> = {</span> |
148 | path = ./latex-report-template; | 148 | <span id="cb12-13"><a href="#cb12-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./latex-report-template<span class="kw">;</span></span> |
149 | description = &quot;A latex whitepaper project&quot;; | 149 | <span id="cb12-14"><a href="#cb12-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">&quot;A latex whitepaper project&quot;</span><span class="kw">;</span></span> |
150 | }; | 150 | <span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">};</span></span> |
151 | rust-hello = { | 151 | <span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a> <span class="ex">rust-hello</span> = {</span> |
152 | path = ./rust-hello-template; | 152 | <span id="cb12-17"><a href="#cb12-17" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./rust-hello-template<span class="kw">;</span></span> |
153 | description = &quot;Simple Hello World in Rust, with overloaded Rust toolchain&quot;; | 153 | <span id="cb12-18"><a href="#cb12-18" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">&quot;Simple Hello World in Rust, with overloaded Rust toolchain&quot;</span><span class="kw">;</span></span> |
154 | }; | 154 | <span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
155 | } | 155 | <span id="cb12-20"><a href="#cb12-20" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span> |
156 | + // official-templates.templates | 156 | <span id="cb12-21"><a href="#cb12-21" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> // official-templates.templates</span> |
157 | + // other-templates.templates; | 157 | <span id="cb12-22"><a href="#cb12-22" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> // other-templates.templates<span class="kw">;</span></span> |
158 | 158 | <span id="cb12-23"><a href="#cb12-23" aria-hidden="true" tabindex="-1"></a> </span> | |
159 | }; | 159 | <span id="cb12-24"><a href="#cb12-24" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
160 | }</code></pre> | 160 | <span id="cb12-25"><a href="#cb12-25" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span></code></pre></div> |
161 | <p>Running <code>nix flake show biscuits</code> will now list templates from the <code>biscuits</code> registry as well as the ones from <code>NixOS/templates</code>. Ensure that the names don’t collide though.</p></description> | 161 | <p>Running <code>nix flake show biscuits</code> will now list templates from the <code>biscuits</code> registry as well as the ones from <code>NixOS/templates</code>. Ensure that the names don’t collide though.</p></description> |
162 | <link>https://peppe.rs/posts/novice_nix:_flake_templates/</link> | 162 | <link>https://peppe.rs/posts/novice_nix:_flake_templates/</link> |
163 | <pubDate>Tue, 05 Oct 2021 16:49:00 +0000</pubDate> | 163 | <pubDate>Tue, 05 Oct 2021 16:49:00 +0000</pubDate> |
diff --git a/docs/posts/novice_nix:_flake_templates/index.html b/docs/posts/novice_nix:_flake_templates/index.html index f71c13a..6e1a49d 100644 --- a/docs/posts/novice_nix:_flake_templates/index.html +++ b/docs/posts/novice_nix:_flake_templates/index.html | |||
@@ -44,53 +44,53 @@ | |||
44 | <div class="post-text"> | 44 | <div class="post-text"> |
45 | <p>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, <code>nix</code> already supports templates!</p> | 45 | <p>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, <code>nix</code> already supports templates!</p> |
46 | <p>You might already be familiar with <code>nix flake init</code>, that drops a “default” flake expression into your current working directory. If you head over to the manpage:</p> | 46 | <p>You might already be familiar with <code>nix flake init</code>, that drops a “default” flake expression into your current working directory. If you head over to the manpage:</p> |
47 | <pre><code>nix flake init --help</code></pre> | 47 | <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">nix</span> flake init <span class="at">--help</span></span></code></pre></div> |
48 | <p>You will read that <code>nix flake init</code> creates a flake using the “default template”. Additionally, you can create a flake from a specific template by passing the <code>-t</code> flag. Where does this default originate from?</p> | 48 | <p>You will read that <code>nix flake init</code> creates a flake using the “default template”. Additionally, you can create a flake from a specific template by passing the <code>-t</code> flag. Where does this default originate from?</p> |
49 | <h2 id="flake-registries">Flake Registries</h2> | 49 | <h2 id="flake-registries">Flake Registries</h2> |
50 | <p>Quick detour into registries! Registries are a way to alias popular flakes using identifiers:</p> | 50 | <p>Quick detour into registries! Registries are a way to alias popular flakes using identifiers:</p> |
51 | <pre><code># list a few predefined registries | 51 | <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># list a few predefined registries</span></span> |
52 | $ nix registry list | 52 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry list</span> |
53 | . . . | 53 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . . </span> |
54 | global flake:nixpkgs github:NixOS/nixpkgs | 54 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:nixpkgs github:NixOS/nixpkgs</span> |
55 | global flake:patchelf github:NixOS/patchelf | 55 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:patchelf github:NixOS/patchelf</span> |
56 | global flake:nix-serve github:edolstra/nix-serve | 56 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:nix-serve github:edolstra/nix-serve</span> |
57 | global flake:templates github:NixOS/templates | 57 | <span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:templates github:NixOS/templates</span> |
58 | global flake:nickel github:tweag/nickel | 58 | <span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="ex">global</span> flake:nickel github:tweag/nickel</span> |
59 | . . . | 59 | <span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> |
60 | 60 | <span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a></span> | |
61 | # you can do | 61 | <span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="co"># you can do </span></span> |
62 | $ nix flake show nickel | 62 | <span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show nickel</span> |
63 | 63 | <span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a></span> | |
64 | # instead of | 64 | <span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="co"># instead of </span></span> |
65 | $ nix flake show github:tweag/nickel | 65 | <span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show github:tweag/nickel</span> |
66 | 66 | <span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a></span> | |
67 | # which is short for | 67 | <span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="co"># which is short for</span></span> |
68 | $ nix flake show git+https://github.com/tweag/nickel</code></pre> | 68 | <span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show git+https://github.com/tweag/nickel</span></code></pre></div> |
69 | <p>You might notice a registry called <code>templates</code> aliased to <code>github:NixOS/templates</code>. Take a peek with <code>nix flake show</code>:</p> | 69 | <p>You might notice a registry called <code>templates</code> aliased to <code>github:NixOS/templates</code>. Take a peek with <code>nix flake show</code>:</p> |
70 | <pre><code>$ nix flake show templates | 70 | <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show templates</span> |
71 | github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee | 71 | <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ex">github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee</span></span> |
72 | ├───defaultTemplate: template: A very basic flake | 72 | <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="ex">├───defaultTemplate:</span> template: A very basic flake</span> |
73 | └───templates | 73 | <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="ex">└───templates</span></span> |
74 | ├───bash-hello: template: An over-engineered Hello World in bash | 74 | <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───bash-hello:</span> template: An over-engineered Hello World in bash</span> |
75 | ├───c-hello: template: An over-engineered Hello World in C | 75 | <span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───c-hello:</span> template: An over-engineered Hello World in C</span> |
76 | ├───rust-web-server: template: A Rust web server including a NixOS module | 76 | <span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───rust-web-server:</span> template: A Rust web server including a NixOS module</span> |
77 | ├───simpleContainer: template: A NixOS container running apache-httpd | 77 | <span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───simpleContainer:</span> template: A NixOS container running apache-httpd</span> |
78 | └───trivial: template: A very basic flake</code></pre> | 78 | <span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">└───trivial:</span> template: A very basic flake</span></code></pre></div> |
79 | <p>Aha! There is a flake output called <code>defaultTemplate</code>. This is the template being sourced when you run <code>nix flake init</code>. Astute readers may conclude the following:</p> | 79 | <p>Aha! There is a flake output called <code>defaultTemplate</code>. This is the template being sourced when you run <code>nix flake init</code>. Astute readers may conclude the following:</p> |
80 | <pre><code>$ nix flake init | 80 | <div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init</span> |
81 | 81 | <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span> | |
82 | # is equivalent to | 82 | <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co"># is equivalent to</span></span> |
83 | $ nix flake init -t templates#defaultTemplate | 83 | <span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> templates#defaultTemplate</span> |
84 | 84 | <span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a></span> | |
85 | # is equivalent to | 85 | <span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="co"># is equivalent to</span></span> |
86 | $ nix flake init -t github:NixOS/templates#defaultTemplate | 86 | <span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> github:NixOS/templates#defaultTemplate</span> |
87 | 87 | <span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a></span> | |
88 | # which is short for | 88 | <span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="co"># which is short for</span></span> |
89 | $ nix flake init -t git+https://NixOS/templates#defaultTemplate</code></pre> | 89 | <span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> git+https://NixOS/templates#defaultTemplate</span></code></pre></div> |
90 | <p>Similarly, the other templates can be accessed via:</p> | 90 | <p>Similarly, the other templates can be accessed via:</p> |
91 | <pre><code>$ nix flake init -t templates#c-hello | 91 | <div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> templates#c-hello</span> |
92 | $ nix flake init -t templates#simpleContainer | 92 | <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> templates#simpleContainer</span> |
93 | # I think you get the drift ...</code></pre> | 93 | <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="co"># I think you get the drift ...</span></span></code></pre></div> |
94 | <h2 id="rolling-your-own-templates">Rolling your own templates</h2> | 94 | <h2 id="rolling-your-own-templates">Rolling your own templates</h2> |
95 | <p>Alright, so all we need to do is:</p> | 95 | <p>Alright, so all we need to do is:</p> |
96 | <ul> | 96 | <ul> |
@@ -99,94 +99,94 @@ $ nix flake init -t templates#simpleContainer | |||
99 | <li>(<strong>optionally</strong>) alias our custom templates flake to an identifier using registries, for easier access</li> | 99 | <li>(<strong>optionally</strong>) alias our custom templates flake to an identifier using registries, for easier access</li> |
100 | </ul> | 100 | </ul> |
101 | <p>Start off by creating a directory to store your templates in (we will be converting this to a registry later):</p> | 101 | <p>Start off by creating a directory to store your templates in (we will be converting this to a registry later):</p> |
102 | <pre><code>$ mkdir ~/mytemplates</code></pre> | 102 | <div class="sourceCode" id="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> mkdir ~/mytemplates</span></code></pre></div> |
103 | <p>A flake that exposes a “template” as its output looks something like this:</p> | 103 | <p>A flake that exposes a “template” as its output looks something like this:</p> |
104 | <pre><code># inside ~/mytemplates/flake.nix | 104 | <div class="sourceCode" id="cb7"><pre class="sourceCode nix"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># inside ~/mytemplates/flake.nix</span></span> |
105 | 105 | <span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span> | |
106 | { | 106 | <span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="kw">{</span></span> |
107 | description = "Pepper's flake templates"; | 107 | <span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">"Pepper's flake templates"</span><span class="kw">;</span></span> |
108 | 108 | <span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a></span> | |
109 | outputs = { self, ... }: { | 109 | <span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="ex">outputs</span> = { self, ... }: {</span> |
110 | templates = { | 110 | <span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">templates</span> = {</span> |
111 | latex-report = { | 111 | <span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="ex">latex-report</span> = {</span> |
112 | path = ./latex-report-template; | 112 | <span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./latex-report-template<span class="kw">;</span></span> |
113 | description = "A latex whitepaper project"; | 113 | <span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">"A latex whitepaper project"</span><span class="kw">;</span></span> |
114 | }; | 114 | <span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="kw">};</span></span> |
115 | rust-hello = { | 115 | <span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">rust-hello</span> = {</span> |
116 | path = ./rust-hello-template; | 116 | <span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./rust-hello-template<span class="kw">;</span></span> |
117 | description = "Simple Hello World in Rust"; | 117 | <span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">"Simple Hello World in Rust"</span><span class="kw">;</span></span> |
118 | }; | 118 | <span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
119 | }; | 119 | <span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
120 | }; | 120 | <span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
121 | }</code></pre> | 121 | <span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a><span class="er">}</span></span></code></pre></div> |
122 | <p>The <code>path</code> attribute to each template is what gets copied over when you initialize a flake. Running <code>nix flake init -t .#latex-report</code> will initialize the current directory with the contents of <code>./latex-report-template</code> (we are yet to populate these directories).</p> | 122 | <p>The <code>path</code> attribute to each template is what gets copied over when you initialize a flake. Running <code>nix flake init -t .#latex-report</code> will initialize the current directory with the contents of <code>./latex-report-template</code> (we are yet to populate these directories).</p> |
123 | <p>The output of <code>nix flake show</code> should be something like:</p> | 123 | <p>The output of <code>nix flake show</code> should be something like:</p> |
124 | <pre><code>$ nix flake show | 124 | <div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show</span> |
125 | path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...} | 125 | <span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="ex">path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...}</span></span> |
126 | └───templates | 126 | <span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="ex">└───templates</span></span> |
127 | ├───latex-report: template: A latex whitepaper project | 127 | <span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="ex">├───latex-report:</span> template: A latex whitepaper project</span> |
128 | └───rust-hello: template: Simple Hello World in Rust</code></pre> | 128 | <span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="ex">└───rust-hello:</span> template: Simple Hello World in Rust</span></code></pre></div> |
129 | <p>Populate your template directories with content, here are my template directories for example:</p> | 129 | <p>Populate your template directories with content, here are my template directories for example:</p> |
130 | <pre><code>$ tree mytemplates | 130 | <div class="sourceCode" id="cb9"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> tree mytemplates</span> |
131 | mytemplates/ | 131 | <span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="ex">mytemplates/</span></span> |
132 | ├── flake.nix | 132 | <span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> flake.nix</span> |
133 | ├── latex-report-template | 133 | <span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> latex-report-template</span> |
134 | │ ├── flake.nix | 134 | <span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> ├── flake.nix</span> |
135 | │ ├── makefile | 135 | <span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> ├── makefile</span> |
136 | │ └── src | 136 | <span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> └── src</span> |
137 | │ ├── meta.sty | 137 | <span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> ├── meta.sty</span> |
138 | │ └── report.tex | 138 | <span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="ex">│ </span> └── report.tex</span> |
139 | └── rust-hello-template | 139 | <span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a><span class="ex">└──</span> rust-hello-template</span> |
140 | ├── Cargo.toml | 140 | <span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a> <span class="ex">├──</span> Cargo.toml</span> |
141 | ├── flake.nix | 141 | <span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">├──</span> flake.nix</span> |
142 | └── src | 142 | <span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> src</span> |
143 | └── main.rs</code></pre> | 143 | <span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> main.rs</span></code></pre></div> |
144 | <p>And that’s it! Start using your templates with:</p> | 144 | <p>And that’s it! Start using your templates with:</p> |
145 | <pre><code>$ nix flake init -t ~/mytemplates#rust-hello | 145 | <div class="sourceCode" id="cb10"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> ~/mytemplates#rust-hello</span> |
146 | $ tree . | 146 | <span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> tree .</span> |
147 | . | 147 | <span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span></span> |
148 | ├── Cargo.toml | 148 | <span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> Cargo.toml</span> |
149 | ├── flake.nix | 149 | <span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> flake.nix</span> |
150 | └── src | 150 | <span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span class="ex">└──</span> src</span> |
151 | └── main.rs</code></pre> | 151 | <span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> main.rs</span></code></pre></div> |
152 | <p>To avoid writing <code>~/mytemplates</code> each time, simply alias it to a registry:</p> | 152 | <p>To avoid writing <code>~/mytemplates</code> each time, simply alias it to a registry:</p> |
153 | <pre><code># alias it to `biscuits` | 153 | <div class="sourceCode" id="cb11"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co"># alias it to `biscuits`</span></span> |
154 | $ nix registry add biscuits ~/mytemplates | 154 | <span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry add biscuits ~/mytemplates</span> |
155 | 155 | <span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a></span> | |
156 | # you will see it listed under `user` registries | 156 | <span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a><span class="co"># you will see it listed under `user` registries</span></span> |
157 | $ nix registry list | 157 | <span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry list</span> |
158 | . . . | 158 | <span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> |
159 | user flake:biscuits path:/home/np/template-tests | 159 | <span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="ex">user</span> flake:biscuits path:/home/np/template-tests</span> |
160 | . . . | 160 | <span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> |
161 | 161 | <span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a></span> | |
162 | $ nix flake init -t biscuits#latex-report</code></pre> | 162 | <span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake init <span class="at">-t</span> biscuits#latex-report</span></code></pre></div> |
163 | <h2 id="extending-the-official-templates">Extending the official templates</h2> | 163 | <h2 id="extending-the-official-templates">Extending the official templates</h2> |
164 | <p>I personally, would like the <code>biscuits</code> registry to include not just my homemade templates, but also the templates from <code>NixOS/templates</code> (and maybe a couple of other repositories in the wild):</p> | 164 | <p>I personally, would like the <code>biscuits</code> registry to include not just my homemade templates, but also the templates from <code>NixOS/templates</code> (and maybe a couple of other repositories in the wild):</p> |
165 | <pre><code> { | 165 | <div class="sourceCode" id="cb12"><pre class="sourceCode nix"><code class="sourceCode bash"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">{</span></span> |
166 | description = "Pepper's flake templates"; | 166 | <span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">"Pepper's flake templates"</span><span class="kw">;</span></span> |
167 | 167 | <span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> </span> | |
168 | + inputs = { | 168 | <span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> inputs = {</span> |
169 | + official-templates.url = github:NixOS/templates; | 169 | <span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> official-templates.url = github:NixOS/templates<span class="kw">;</span></span> |
170 | + other-templates.url = github:some-other/templates; | 170 | <span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> other-templates.url = github:some-other/templates<span class="kw">;</span></span> |
171 | + }; | 171 | <span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> }<span class="kw">;</span></span> |
172 | 172 | <span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a> </span> | |
173 | outputs = { self, official-templates, other-templates ... }: { | 173 | <span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">outputs</span> = { self, official-templates, other-templates ... }: {</span> |
174 | 174 | <span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a> </span> | |
175 | templates = { | 175 | <span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a> <span class="ex">templates</span> = {</span> |
176 | latex-report = { | 176 | <span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">latex-report</span> = {</span> |
177 | path = ./latex-report-template; | 177 | <span id="cb12-13"><a href="#cb12-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./latex-report-template<span class="kw">;</span></span> |
178 | description = "A latex whitepaper project"; | 178 | <span id="cb12-14"><a href="#cb12-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">"A latex whitepaper project"</span><span class="kw">;</span></span> |
179 | }; | 179 | <span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">};</span></span> |
180 | rust-hello = { | 180 | <span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a> <span class="ex">rust-hello</span> = {</span> |
181 | path = ./rust-hello-template; | 181 | <span id="cb12-17"><a href="#cb12-17" aria-hidden="true" tabindex="-1"></a> <span class="ex">path</span> = ./rust-hello-template<span class="kw">;</span></span> |
182 | description = "Simple Hello World in Rust, with overloaded Rust toolchain"; | 182 | <span id="cb12-18"><a href="#cb12-18" aria-hidden="true" tabindex="-1"></a> <span class="ex">description</span> = <span class="st">"Simple Hello World in Rust, with overloaded Rust toolchain"</span><span class="kw">;</span></span> |
183 | }; | 183 | <span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
184 | } | 184 | <span id="cb12-20"><a href="#cb12-20" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span> |
185 | + // official-templates.templates | 185 | <span id="cb12-21"><a href="#cb12-21" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> // official-templates.templates</span> |
186 | + // other-templates.templates; | 186 | <span id="cb12-22"><a href="#cb12-22" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> // other-templates.templates<span class="kw">;</span></span> |
187 | 187 | <span id="cb12-23"><a href="#cb12-23" aria-hidden="true" tabindex="-1"></a> </span> | |
188 | }; | 188 | <span id="cb12-24"><a href="#cb12-24" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> |
189 | }</code></pre> | 189 | <span id="cb12-25"><a href="#cb12-25" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span></code></pre></div> |
190 | <p>Running <code>nix flake show biscuits</code> will now list templates from the <code>biscuits</code> registry as well as the ones from <code>NixOS/templates</code>. Ensure that the names don’t collide though.</p> | 190 | <p>Running <code>nix flake show biscuits</code> will now list templates from the <code>biscuits</code> registry as well as the ones from <code>NixOS/templates</code>. Ensure that the names don’t collide though.</p> |
191 | 191 | ||
192 | </div> | 192 | </div> |
diff --git a/posts/novice_nix:_flake_templates.md b/posts/novice_nix:_flake_templates.md index 3b38b46..8e25a85 100644 --- a/posts/novice_nix:_flake_templates.md +++ b/posts/novice_nix:_flake_templates.md | |||
@@ -9,7 +9,7 @@ You might already be familiar with `nix flake init`, that | |||
9 | drops a "default" flake expression into your current working | 9 | drops a "default" flake expression into your current working |
10 | directory. If you head over to the manpage: | 10 | directory. If you head over to the manpage: |
11 | 11 | ||
12 | ``` | 12 | ```bash |
13 | nix flake init --help | 13 | nix flake init --help |
14 | ``` | 14 | ``` |
15 | 15 | ||
@@ -23,7 +23,7 @@ does this default originate from? | |||
23 | Quick detour into registries! Registries are a way to alias | 23 | Quick detour into registries! Registries are a way to alias |
24 | popular flakes using identifiers: | 24 | popular flakes using identifiers: |
25 | 25 | ||
26 | ``` | 26 | ```bash |
27 | # list a few predefined registries | 27 | # list a few predefined registries |
28 | $ nix registry list | 28 | $ nix registry list |
29 | . . . | 29 | . . . |
@@ -47,7 +47,7 @@ $ nix flake show git+https://github.com/tweag/nickel | |||
47 | You might notice a registry called `templates` aliased to | 47 | You might notice a registry called `templates` aliased to |
48 | `github:NixOS/templates`. Take a peek with `nix flake show`: | 48 | `github:NixOS/templates`. Take a peek with `nix flake show`: |
49 | 49 | ||
50 | ``` | 50 | ```bash |
51 | $ nix flake show templates | 51 | $ nix flake show templates |
52 | github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee | 52 | github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee |
53 | ├───defaultTemplate: template: A very basic flake | 53 | ├───defaultTemplate: template: A very basic flake |
@@ -63,7 +63,7 @@ Aha! There is a flake output called `defaultTemplate`. This | |||
63 | is the template being sourced when you run `nix flake init`. | 63 | is the template being sourced when you run `nix flake init`. |
64 | Astute readers may conclude the following: | 64 | Astute readers may conclude the following: |
65 | 65 | ||
66 | ``` | 66 | ```bash |
67 | $ nix flake init | 67 | $ nix flake init |
68 | 68 | ||
69 | # is equivalent to | 69 | # is equivalent to |
@@ -78,7 +78,7 @@ $ nix flake init -t git+https://NixOS/templates#defaultTemplate | |||
78 | 78 | ||
79 | Similarly, the other templates can be accessed via: | 79 | Similarly, the other templates can be accessed via: |
80 | 80 | ||
81 | ``` | 81 | ```bash |
82 | $ nix flake init -t templates#c-hello | 82 | $ nix flake init -t templates#c-hello |
83 | $ nix flake init -t templates#simpleContainer | 83 | $ nix flake init -t templates#simpleContainer |
84 | # I think you get the drift ... | 84 | # I think you get the drift ... |
@@ -96,14 +96,14 @@ Alright, so all we need to do is: | |||
96 | Start off by creating a directory to store your templates in | 96 | Start off by creating a directory to store your templates in |
97 | (we will be converting this to a registry later): | 97 | (we will be converting this to a registry later): |
98 | 98 | ||
99 | ``` | 99 | ```bash |
100 | $ mkdir ~/mytemplates | 100 | $ mkdir ~/mytemplates |
101 | ``` | 101 | ``` |
102 | 102 | ||
103 | A flake that exposes a "template" as its output looks | 103 | A flake that exposes a "template" as its output looks |
104 | something like this: | 104 | something like this: |
105 | 105 | ||
106 | ``` | 106 | ```nix |
107 | # inside ~/mytemplates/flake.nix | 107 | # inside ~/mytemplates/flake.nix |
108 | 108 | ||
109 | { | 109 | { |
@@ -132,7 +132,7 @@ populate these directories). | |||
132 | 132 | ||
133 | The output of `nix flake show` should be something like: | 133 | The output of `nix flake show` should be something like: |
134 | 134 | ||
135 | ``` | 135 | ```bash |
136 | $ nix flake show | 136 | $ nix flake show |
137 | path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...} | 137 | path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...} |
138 | └───templates | 138 | └───templates |
@@ -143,7 +143,7 @@ path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...} | |||
143 | Populate your template directories with content, here are my | 143 | Populate your template directories with content, here are my |
144 | template directories for example: | 144 | template directories for example: |
145 | 145 | ||
146 | ``` | 146 | ```bash |
147 | $ tree mytemplates | 147 | $ tree mytemplates |
148 | mytemplates/ | 148 | mytemplates/ |
149 | ├── flake.nix | 149 | ├── flake.nix |
@@ -162,7 +162,7 @@ mytemplates/ | |||
162 | 162 | ||
163 | And that's it! Start using your templates with: | 163 | And that's it! Start using your templates with: |
164 | 164 | ||
165 | ``` | 165 | ```bash |
166 | $ nix flake init -t ~/mytemplates#rust-hello | 166 | $ nix flake init -t ~/mytemplates#rust-hello |
167 | $ tree . | 167 | $ tree . |
168 | . | 168 | . |
@@ -175,7 +175,7 @@ $ tree . | |||
175 | To avoid writing `~/mytemplates` each time, simply alias it | 175 | To avoid writing `~/mytemplates` each time, simply alias it |
176 | to a registry: | 176 | to a registry: |
177 | 177 | ||
178 | ``` | 178 | ```bash |
179 | # alias it to `biscuits` | 179 | # alias it to `biscuits` |
180 | $ nix registry add biscuits ~/mytemplates | 180 | $ nix registry add biscuits ~/mytemplates |
181 | 181 | ||
@@ -195,7 +195,7 @@ not just my homemade templates, but also the templates from | |||
195 | `NixOS/templates` (and maybe a couple of other repositories | 195 | `NixOS/templates` (and maybe a couple of other repositories |
196 | in the wild): | 196 | in the wild): |
197 | 197 | ||
198 | ``` | 198 | ```nix |
199 | { | 199 | { |
200 | description = "Pepper's flake templates"; | 200 | description = "Pepper's flake templates"; |
201 | 201 | ||