nix-playgraound/notes/017-templates.md

1.1 KiB

Templates

This note covers 14-template/, which exposes templates.default so nix flake init -t can scaffold a new flake from files in the repository.


1. What a Template Output Is

A template output is metadata plus a path:

templates.default = {
  path = ./template;
  description = "Scaffold a tiny flake with one dev shell.";
};

The path points at the directory that should be copied into the new project. The description is what users see in nix flake show.


2. Why the Template Lives in Its Own Directory

The template is just ordinary files. In this example, template/ contains:

  • a small flake.nix, and
  • a short README.md.

That separation matters because the example flake and the template flake are doing different jobs:

  • 14-template/flake.nix publishes the template, and
  • 14-template/template/flake.nix is the scaffold that gets copied into a new directory.

3. Commands to Try

cd 14-template

nix flake show

mkdir /tmp/template-demo
cd /tmp/template-demo
nix flake init -t /path/to/nix-playground/14-template#default