Skip to content
LogoLogo

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 --template works too.

How a template is published

nixcafe-develop/nodeinput templates-node,flake = falsetemplates/node/default.nixtemplates.nodenix flake init -tbeans#node

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.nix calls inputs.purr.lib.mkFlake with src = ./develop for language templates, or src = ./. for configuration templates. Take nixpkgs, purr, and git-hooks from FlakeHub.

  • Inline formatter. Set the formatter in flake.nix with outputsBuilder:

    outputsBuilder =
      { pkgs, ... }:
      {
        formatter = pkgs.nixfmt-tree;
      };

    Use nixfmt-tree, not plain nixfmt. Plain nixfmt reads stdin when nix fmt runs without arguments.

  • Hooks are evaluated once. Define the hooks in checks/git-hooks/default.nix and have the dev shell reuse that check for its shellHook and 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 system argument. Files under checks/, shells/, and apps/, and the outputsBuilder function, take system from purr as a function argument. Modules and packages read pkgs.stdenv.hostPlatform.system. Never use pkgs.system.

  • No flake.lock and no LICENSE. Users get fresh inputs on their first Nix command and pick their own license.

  • An .envrc with use flake and a .gitignore that covers .direnv/, result*, and /.pre-commit-config.yaml.

Add a template

  1. Create the source repository under nixcafe-develop and mark it as a template repository on GitHub.
  2. Add a templates-<name> input with flake = false to flake.nix in develop-templates.
  3. Add templates/<name>/default.nix with description, path, and welcomeText.
  4. Run nix flake lock to pin the new input.
  5. Add the template to the table in README.md and to the docs.

Keep welcomeText to commands the template supports:

CommandMention it when
nix developThe template has a dev shell
nix fmtThe flake has a formatter
nix flake checkThe template has the git-hooks check
nix buildThe 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 check

Try 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 check

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