Flake templates: generated or shared content

AFAIU flake templates create a directory hierarchy by simply copying the contents of the template’s directory.

Is there any way to

  • share content between multiple templates in the same flake, without having to cp it into each template’s directory? For example, I want a bunch of templates which are variations on a theme. Much of the code in them will be identical. If I fix a bug in this shared code, it needs to be fixed in all the templates. How can I have a single source of truth for such cases?
  • have the template generate the content by filling in some gaps. For example, I want templates which generate a new project for a client. Instead of giving the client a list of places where dummy values need to be replaced with project-specific values, I would like the template to generate the files with the project-specific values already in place, taken from information provided by the user at the time the template is used.

I can probably hack together some home-grown scripts to mitigate the amount of repetitive and error-prone work that needs to be done in these cases, but I’d rather not re-invent any wheels.

Does anything exist to help with cases such as these?

1 Like

Templates are useless. Write an app that copies stuff from a Dev based on command line args.

1 Like

This makes sense.

But it’s not obvious to me exactly from where the app should copy data. To keep things simple, as a first step, I just want to replace the template with an app that merely copies the template directory’s contents onto the user’s filesystem (and maybe adds a git init and and a first commit).

As this, in general, runs on a client’s machine, I guess that the template directory’s contents have to be copied into the nix store in order to be available to the app. I don’t know

  • how to place the required files in the nix store
  • where in the nix store the app should look for them

Or am I barking up the wrong tree?

You should have access to the self flake input from your app, either passed in as a function arg or from the flake context. With that you can do something like

cp ${self}/template .
1 Like