Contributing
Two places hold the templates:
- nixcafe/develop-templates is the flake
you pass to
nix flake init -t. It contains no template files itself. - nixcafe-develop has one repository per template
with the actual files. Each one is a GitHub template repository, so
gh repo create --templateworks too.
How a template is published
flake.nix declares each source repository as a non-flake input. Nix fetches it as a
plain source tree and does not evaluate its flake.nix:
# flake.nix
templates-node = {
url = "github:nixcafe-develop/node";
flake = false;
};develop-templates is itself a purr flake with src = ./.. purr imports every
templates/<name>/default.nix and exposes it as templates.<name>:
# templates/node/default.nix
{ inputs, ... }:
{
description = "Node.js dev shell with corepack, ni, Bun, and Deno";
path = "${inputs.templates-node}";
welcomeText = ''
# Node.js
- `nix develop` opens a shell with Node.js, corepack, ni, Bun, and Deno.
- `nix fmt` formats the Nix files.
- `nix flake check` runs nixfmt, deadnix, and statix.
'';
}The secrets template reads the nix-secrets repository. purr and purr-flake-parts
use ${inputs.purr}/template/default and ${inputs.purr}/template/flake-parts.
flake.lock pins every source repository, so a template only changes for users after
the lock file is updated with nix flake update <input> and pushed.
Rules for template repositories
-
purr layout.
flake.nixcallsinputs.purr.lib.mkFlakewithsrc = ./developfor language templates, orsrc = ./.for configuration templates. Takenixpkgs,purr, andgit-hooksfrom FlakeHub. -
Inline formatter. Set the formatter in
flake.nixwithoutputsBuilder:outputsBuilder = { pkgs, ... }: { formatter = pkgs.nixfmt-tree; };Use
nixfmt-tree, not plainnixfmt. Plainnixfmtreads stdin whennix fmtruns without arguments. -
Hooks are evaluated once. Define the hooks in
checks/git-hooks/default.nixand have the dev shell reuse that check for itsshellHookand packages:# develop/shells/default/default.nix { inputs, pkgs, system, ... }: let gitHooks = inputs.self.checks.${system}.git-hooks; in pkgs.mkShell { packages = gitHooks.enabledPackages; inherit (gitHooks) shellHook; }Add a tool as a hook rather than as a separate shell package when a hook exists for it.
-
The
systemargument. Files underchecks/,shells/, andapps/, and theoutputsBuilderfunction, takesystemfrom purr as a function argument. Modules and packages readpkgs.stdenv.hostPlatform.system. Never usepkgs.system. -
No
flake.lockand noLICENSE. Users get fresh inputs on their first Nix command and pick their own license. -
An
.envrcwithuse flakeand a.gitignorethat covers.direnv/,result*, and/.pre-commit-config.yaml.
Add a template
- Create the source repository under
nixcafe-developand mark it as a template repository on GitHub. - Add a
templates-<name>input withflake = falsetoflake.nixin develop-templates. - Add
templates/<name>/default.nixwithdescription,path, andwelcomeText. - Run
nix flake lockto pin the new input. - Add the template to the table in
README.mdand to the docs.
Keep welcomeText to commands the template supports:
| Command | Mention it when |
|---|---|
nix develop | The template has a dev shell |
nix fmt | The flake has a formatter |
nix flake check | The template has the git-hooks check |
nix build | The template has packages.default |
Verify
nix develop # nixfmt, deadnix, statix, and the hooks
nixfmt --check $(git ls-files '*.nix')
deadnix -L --fail
statix check
nix flake check --all-systems --no-build # evaluates every template on every system
nix flake checkTry a template end to end from your checkout before you open a pull request:
cd "$(mktemp -d)"
nix flake init -t ~/src/develop-templates#node
git init && git add -A
nix flake checkCI runs both nix flake check commands on every pull request, and a push to main
publishes a rolling release to FlakeHub. Nix only sees files that git tracks, so run
git add -N on new files before you evaluate. Update inputs with nix flake update, never
by editing flake.lock.
Documentation changes for this site go to
nixcafe/nixcafe-docs under
apps/develop-templates.