More options for flake `outputs.templates`
Problem
I want to make a template for programming in in a environment managed by a nix Flake. This works pretty good with the templates.<name> output. However, it is still a bit clumsy to use:
- run
nix flake init -t myTemplateFlake#templateName - run
git init - manually replace all placeholder names with the name of the project
- run
git add .to make nix aware of all required files - run
nix developto enter the development environment
Solutions
script option
I want to suggest to add a templates.<name>.script option which takes a path to a script (or a string containing a script) which is executed after the content of templates.<name>.path is copied to the target folder. This allows the creators of the template to decide, what they want to do (they might implement steps 1-4 of the above list) and prompt the user for possible inputs. But this might be problematic in regards to the pure and reproducible environment.
specialized options for every aspect
this would mean implementing many options like:
-
templates.<name>.initGitto decide, whether to automatically init a git repository -
templates.<name>.replaceas a list of strings, which will automatically be replaced. Their replacement value could be given to them in the call ofnix init:nix init -t myTemplateFlake#templateName --replace <string>=<futureString>, but this could break the environment again.
I tried generating the template dynamically by defining a derivation and setting templates.<name>.path = testTemplateDrv in different similar configurations, but it didn't work.
templates.<name>.path = testTemplateDrv -> error: 'templates.<name>.path' is not a string but a set
templates.<name>.path = testTemplateDrv.out -> error: 'templates.<name>.path' is not a string but a set
templates.<name>.path = testTemplateDrv.outPath -> error: 'templates.<name>.path' is not a string but a set
templates.<name>.path = "${testTemplateDrv}" -> error: opening directory '/nix/store/ipsmqp2yayjijb7mbnd2mk1bc6zwfp9x-test': No such file or directory
I tried generating the template dynamically by defining a derivation and setting
templates.<name>.path = testTemplateDrvin different similar configurations, but it didn't work.templates.<name>.path = testTemplateDrv->error: 'templates.<name>.path' is not a string but a settemplates.<name>.path = testTemplateDrv.out->error: 'templates.<name>.path' is not a string but a settemplates.<name>.path = testTemplateDrv.outPath->error: 'templates.<name>.path' is not a string but a settemplates.<name>.path = "${testTemplateDrv}"->error: opening directory '/nix/store/ipsmqp2yayjijb7mbnd2mk1bc6zwfp9x-test': No such file or directory
As far as I can tell the path has to be path inside the nix store itself and not a derivation. But if you have a derivation you should be able to call drvPath on it.
How is the stance on this? I think it would make templates a lot more useful. This could be a similar option to system.activationScripts in NixOs. I think it would also make sense to allow such templates only in empty directories to avoid these scripts deleting/modifying that they should. This would also be better for declarativity. I don't think extra options, like the ones the @wuyoli suggest would be necessary, eg. ratio between usefulness and cost of maintaining is to high in my opinion.