This is just a sample template that includes a flake.nix
file
to help template designers know how to leverage Nix to
provide a reproducible Copier build for
their template consumers.
Of course, the purpose is to show you how you can use Nix for this, so I expect you to have Nix installed on your system. Instructions here.
Now, check that the template cannot render with standard Copier:
➤ nix run github:copier-org/copier/v7.1.0 -- git+https://gist.github.com/3f935292a326ff22192b4b497a8b6d86.git ./result
No git tags found in template; using HEAD as ref
Copier could not load some Jinja extensions:
No module named 'jinja2_time'
Make sure to install these extensions alongside Copier itself.
See the docs at https://copier.readthedocs.io/en/latest/configuring/#jinja_extensions
However, you can render it with the Copier app packaged within the template, which includes the required extension:
➤ nix run git+https://gist.github.com/3f935292a326ff22192b4b497a8b6d86.git -- git+https://gist.github.com/3f935292a326ff22192b4b497a8b6d86.git ./result
No git tags found in template; using HEAD as ref
Copying from template version 0.0.0.post3.dev0+cb34a4b
create .
create now.txt
The above command could seem redundant because you're repeating the Gist URL twice. What's happening? You will understand it better now:
➤ nix run git+https://gist.github.com/3f935292a326ff22192b4b497a8b6d86.git -- --version
copier 7.1.0.dev20230407161614+nix.git.91f21c7
So 1st time you're telling Nix where's the flake it has to run. The 2nd time
(after --
), you're already running Copier, so you're telling Copier where's
the template source and destination. It turns out both URLs are the same, but
have different meaning.
We're using flakes in this example, so your nix installation should have them enabled. Although flakes is an experimental feature, it is quite stable at the time of writing. If you don't have it enabled, make sure to add these flags to all commands:
nix --extra-experimental-features 'nix-command flakes' [the rest of commands]
The template requires Jinja2 Time extension. I picked this one because it is available in Nixpkgs. If you need to include an extension that isn't packaged for Nix, you'd have a slightly more complex use case because it involves packaging it. It's not very hard, once you understand Nix. Tools like poetry2nix or dream2nix can help you get landed fast by combining with Poetry.
You can use also
buildPythonPackage
from Nixpkgs.
FWIW that's how the Jinja2-Time extension is packaged currently.
The flake.lock
file is generated automatically by running nix flake lock
,
and ensures 100% build reproducibility.