From 5acd4c77a0ad5499fbd558e8f93f461f84ab26d1 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 6 Oct 2021 08:03:01 +0530 Subject: add missing syntax highlighting --- docs/index.xml | 242 +++++++++++----------- docs/posts/novice_nix:_flake_templates/index.html | 242 +++++++++++----------- 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 @@ Novice Nix: Flake Templates <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> <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> -<pre><code>nix flake init --help</code></pre> +<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> <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> <h2 id="flake-registries">Flake Registries</h2> <p>Quick detour into registries! Registries are a way to alias popular flakes using identifiers:</p> -<pre><code># 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</code></pre> +<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> +<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry list</span> +<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . . </span> +<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> +<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> +<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> +<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> +<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> +<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> +<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="co"># you can do </span></span> +<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix flake show nickel</span> +<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="co"># instead of </span></span> +<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> +<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="co"># which is short for</span></span> +<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> <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> -<pre><code>$ 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</code></pre> +<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> +<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ex">github:NixOS/templates/79f48a7b822f35c068c5e235da2e9fbd154cecee</span></span> +<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> +<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="ex">└───templates</span></span> +<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> +<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> +<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> +<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> +<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> <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> -<pre><code>$ 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://NixOS/templates#defaultTemplate</code></pre> +<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> +<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co"># is equivalent to</span></span> +<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> +<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="co"># is equivalent to</span></span> +<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> +<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="co"># which is short for</span></span> +<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> <p>Similarly, the other templates can be accessed via:</p> -<pre><code>$ nix flake init -t templates#c-hello -$ nix flake init -t templates#simpleContainer -# I think you get the drift ...</code></pre> +<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> +<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> +<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> <h2 id="rolling-your-own-templates">Rolling your own templates</h2> <p>Alright, so all we need to do is:</p> <ul> @@ -70,94 +70,94 @@ $ nix flake init -t templates#simpleContainer <li>(<strong>optionally</strong>) alias our custom templates flake to an identifier using registries, for easier access</li> </ul> <p>Start off by creating a directory to store your templates in (we will be converting this to a registry later):</p> -<pre><code>$ mkdir ~/mytemplates</code></pre> +<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> <p>A flake that exposes a “template” as its output looks something like this:</p> -<pre><code># inside ~/mytemplates/flake.nix - -{ - description = &quot;Pepper&#39;s flake templates&quot;; - - outputs = { self, ... }: { - templates = { - latex-report = { - path = ./latex-report-template; - description = &quot;A latex whitepaper project&quot;; - }; - rust-hello = { - path = ./rust-hello-template; - description = &quot;Simple Hello World in Rust&quot;; - }; - }; - }; -}</code></pre> +<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> +<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="kw">{</span></span> +<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> +<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a></span> +<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="ex">outputs</span> = { self, ... }: {</span> +<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">templates</span> = {</span> +<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="ex">latex-report</span> = {</span> +<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> +<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> +<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="kw">};</span></span> +<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">rust-hello</span> = {</span> +<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> +<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> +<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> +<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> +<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> +<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a><span class="er">}</span></span></code></pre></div> <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> <p>The output of <code>nix flake show</code> should be something like:</p> -<pre><code>$ 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</code></pre> +<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> +<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> +<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="ex">└───templates</span></span> +<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> +<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> <p>Populate your template directories with content, here are my template directories for example:</p> -<pre><code>$ 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</code></pre> +<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> +<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="ex">mytemplates/</span></span> +<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> flake.nix</span> +<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> latex-report-template</span> +<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a><span class="ex">│  </span> ├── flake.nix</span> +<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="ex">│  </span> ├── makefile</span> +<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="ex">│  </span> └── src</span> +<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="ex">│  </span> ├── meta.sty</span> +<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="ex">│  </span> └── report.tex</span> +<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a><span class="ex">└──</span> rust-hello-template</span> +<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a> <span class="ex">├──</span> Cargo.toml</span> +<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">├──</span> flake.nix</span> +<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> src</span> +<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> main.rs</span></code></pre></div> <p>And that’s it! Start using your templates with:</p> -<pre><code>$ nix flake init -t ~/mytemplates#rust-hello -$ tree . -. -├── Cargo.toml -├── flake.nix -└── src - └── main.rs</code></pre> +<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> +<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> tree .</span> +<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span></span> +<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> Cargo.toml</span> +<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="ex">├──</span> flake.nix</span> +<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span class="ex">└──</span> src</span> +<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">└──</span> main.rs</span></code></pre></div> <p>To avoid writing <code>~/mytemplates</code> each time, simply alias it to a registry:</p> -<pre><code># 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</code></pre> +<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> +<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry add biscuits ~/mytemplates</span> +<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a></span> +<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> +<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> nix registry list</span> +<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> +<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> +<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a><span class="bu">.</span> . .</span> +<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a></span> +<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> <h2 id="extending-the-official-templates">Extending the official templates</h2> <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> -<pre><code> { - description = &quot;Pepper&#39;s flake templates&quot;; - -+ 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 = &quot;A latex whitepaper project&quot;; - }; - rust-hello = { - path = ./rust-hello-template; - description = &quot;Simple Hello World in Rust, with overloaded Rust toolchain&quot;; - }; - } -+ // official-templates.templates -+ // other-templates.templates; - - }; - }</code></pre> +<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> +<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> +<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> </span> +<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> inputs = {</span> +<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> +<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> +<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> }<span class="kw">;</span></span> +<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a> </span> +<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> +<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a> </span> +<span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a> <span class="ex">templates</span> = {</span> +<span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">latex-report</span> = {</span> +<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> +<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> +<span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">};</span></span> +<span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a> <span class="ex">rust-hello</span> = {</span> +<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> +<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> +<span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> +<span id="cb12-20"><a href="#cb12-20" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span> +<span id="cb12-21"><a href="#cb12-21" aria-hidden="true" tabindex="-1"></a><span class="ex">+</span> // official-templates.templates</span> +<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> +<span id="cb12-23"><a href="#cb12-23" aria-hidden="true" tabindex="-1"></a> </span> +<span id="cb12-24"><a href="#cb12-24" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span><span class="kw">;</span></span> +<span id="cb12-25"><a href="#cb12-25" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span></code></pre></div> <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> https://peppe.rs/posts/novice_nix:_flake_templates/ Tue, 05 Oct 2021 16:49:00 +0000 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 @@

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:

-
nix flake init --help
+
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:

-
# 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
+
# 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:

-
$ 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
+
$ 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:

-
$ 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://NixOS/templates#defaultTemplate
+
$ 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://NixOS/templates#defaultTemplate

Similarly, the other templates can be accessed via:

-
$ nix flake init -t templates#c-hello
-$ nix flake init -t templates#simpleContainer
-# I think you get the drift ...
+
$ 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:

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

-
$ mkdir ~/mytemplates
+
$ mkdir ~/mytemplates

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

-
# 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";
-      };
-    };
-  };
-}
+
# 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:

-
$ 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
+
$ 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:

-
$ 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
+
$ 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:

-
$ nix flake init -t ~/mytemplates#rust-hello
-$ tree .
-.
-├── Cargo.toml
-├── flake.nix
-└── src
-    └── main.rs
+
$ 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:

-
# 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
+
# 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):

-
 {
-   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;
- 
-   };
- }
+
 {
+   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.

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 drops a "default" flake expression into your current working directory. If you head over to the manpage: -``` +```bash nix flake init --help ``` @@ -23,7 +23,7 @@ does this default originate from? Quick detour into registries! Registries are a way to alias popular flakes using identifiers: -``` +```bash # list a few predefined registries $ nix registry list . . . @@ -47,7 +47,7 @@ $ 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 @@ -63,7 +63,7 @@ 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 @@ -78,7 +78,7 @@ $ nix flake init -t git+https://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 ... @@ -96,14 +96,14 @@ Alright, so all we need to do is: 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 { @@ -132,7 +132,7 @@ 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 @@ -143,7 +143,7 @@ path:/home/np/code/nix-stuff/template-tests?narHash=sha256-{...} Populate your template directories with content, here are my template directories for example: -``` +```bash $ tree mytemplates mytemplates/ ├── flake.nix @@ -162,7 +162,7 @@ mytemplates/ And that's it! Start using your templates with: -``` +```bash $ nix flake init -t ~/mytemplates#rust-hello $ tree . . @@ -175,7 +175,7 @@ $ tree . To avoid writing `~/mytemplates` each time, simply alias it to a registry: -``` +```bash # alias it to `biscuits` $ nix registry add biscuits ~/mytemplates @@ -195,7 +195,7 @@ 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"; -- cgit v1.2.3