From 89b779f6bfbd476d5302e740a4ad3263a15a8974 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 5 Oct 2021 22:21:58 +0530 Subject: new post: "Novice Nix: Flake Templates" --- docs/index.html | 16 +- docs/index.xml | 152 ++++++++++++++ docs/posts/index.html | 17 ++ docs/posts/novice_nix:_flake_templates/index.html | 221 +++++++++++++++++++++ posts/novice_nix:_flake_templates.md | 229 ++++++++++++++++++++++ 5 files changed, 627 insertions(+), 8 deletions(-) create mode 100644 docs/posts/novice_nix:_flake_templates/index.html create mode 100644 posts/novice_nix:_flake_templates.md diff --git a/docs/index.html b/docs/index.html index c314061..d8fd844 100644 --- a/docs/index.html +++ b/docs/index.html @@ -42,15 +42,15 @@
- 11/04 — 2021 + 05/10 — 2021
- - SDL2 Devlog + + Novice Nix: Flake Templates - 10.0 + 5.5 min @@ -59,15 +59,15 @@
- 17/10 — 2020 + 11/04 — 2021
- - Self-hosting Git + + SDL2 Devlog - 5.4 + 10.0 min diff --git a/docs/index.xml b/docs/index.xml index 06bbc87..d45ba57 100644 --- a/docs/index.xml +++ b/docs/index.xml @@ -12,6 +12,158 @@ en-us Creative Commons BY-NC-SA 4.0 +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> +<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> +<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> +<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> +<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> +<h2 id="rolling-your-own-templates">Rolling your own templates</h2> +<p>Alright, so all we need to do is:</p> +<ul> +<li>create a flake with a <code>templates</code> output</li> +<li>populate our template directories with content</li> +<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> +<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> +<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> +<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> +<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> +<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> +<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> +<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 +https://peppe.rs/posts/novice_nix:_flake_templates/ + + SDL2 Devlog <p>I have been working on an editor for the <a href="https://git.peppe.rs/graphics/obi/about">One Bit Image</a> file format in Rust and SDL2. This entry in my blog follows my progress on the editor. The days are listed in reverse chronological order, begin from the bottom, if this is your first time on this page.</p> <h3 id="day-20">Day 20</h3> diff --git a/docs/posts/index.html b/docs/posts/index.html index dc13873..d15342d 100644 --- a/docs/posts/index.html +++ b/docs/posts/index.html @@ -24,6 +24,23 @@
+ + + + +
+
+ 05/10 — 2021 +
+ + Novice Nix: Flake Templates + +
+ + 5.5 + + min +
diff --git a/docs/posts/novice_nix:_flake_templates/index.html b/docs/posts/novice_nix:_flake_templates/index.html new file mode 100644 index 0000000..f71c13a --- /dev/null +++ b/docs/posts/novice_nix:_flake_templates/index.html @@ -0,0 +1,221 @@ + + + + + + + + + + + + + + + Novice Nix: Flake Templates · peppe.rs + +
+
+ Home + / + Posts + / + Novice Nix: Flake Templates + View Raw +
+
+ 05/10 — 2021 +
+ + 91.51 + + cm +   + + 5.5 + + min +
+
+

+ Novice Nix: Flake Templates +

+
+

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
+

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
+

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
+

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
+

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

Rolling your own templates

+

Alright, so all we need to do is:

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

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

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

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
+

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
+

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

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

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

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.

+ +
+ +
+ Hi. + +

I'm Akshay, I go by nerd or nerdypepper on the internet.

+

+ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. + I write open-source stuff to pass time. + I also design fonts: + scientifica, + curie. +

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

+
+ + Home + / + Posts + / + Novice Nix: Flake Templates + View Raw +
+
+ + diff --git a/posts/novice_nix:_flake_templates.md b/posts/novice_nix:_flake_templates.md new file mode 100644 index 0000000..3b38b46 --- /dev/null +++ b/posts/novice_nix:_flake_templates.md @@ -0,0 +1,229 @@ +Flakes are very handy to setup entirely pure, +project-specific dependencies (not just dependencies, but +build steps, shell environments and more) in a declarative +way. Writing Flake expressions can get repetitive though, +oftentimes, you'd much rather start off with a skeleton. +Luckily, `nix` already supports templates! + +You might already be familiar with `nix flake init`, that +drops a "default" flake expression into your current working +directory. If you head over to the manpage: + +``` +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 +``` + +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 +``` + +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 +``` + +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 ... +``` + +## Rolling your own templates + +Alright, so all we need to do is: + + - create a flake with a `templates` output + - populate our template directories with content + - (**optionally**) alias our custom templates flake to an + identifier using registries, for easier access + +Start off by creating a directory to store your templates in +(we will be converting this to a registry later): + +``` +$ 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"; + }; + }; + }; +} +``` + +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 +``` + +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 +``` + +And that's it! Start using your templates with: + +``` +$ 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 +``` + +## 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; + + }; + } +``` + +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. -- cgit v1.2.3