Templates
Flake files are relatively brief, but you will often want to expose one or more dev shells, maybe some packages to install, possibly some app entries to provide an easy way of launching programs or scripts provided by the flake.
Similarly, you may always want to pin nixpkgs
to a given release,
add another flake from your organization, define some helper functions
and the like.
In short, you may find that your flakes tend to have a certain amount of boiler-plate.
Normally, initializing a new flake is done via nix flake init
,
which produces a minimal flake.nix
file. However, you can refer
to a template by running
nix flake init --template <flake-ref>#<template>
.
Below is a minimal flake providing a single template, starter-22-11, which itself provides a somewhat more useful starting point:
{
description = "flake to provide an example template";
inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; };
outputs = { self, nixpkgs }: {
# nix flake init --template <flake-ref>#<template-name>
templates = {
starter-22-11 = {
path = ./templates/starter-22.11;
description = "starter for 22.11";
};
};
};
}
Try out this flake for yourself, create a new directory, and run
nix flake init --template "github:nix4noobs/template-ex#starter-22-11"
.
The template writes several files, including a nicer starting point
for your own flakes.
Note
The template is the entire directory. It is perfectly possible
to ship a series of files in addition to the flake.nix
and
flake.lock
files.