nix icon indicating copy to clipboard operation
nix copied to clipboard

More options for flake `outputs.templates`

Open wuyoli opened this issue 3 years ago • 2 comments

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:

  1. run nix flake init -t myTemplateFlake#templateName
  2. run git init
  3. manually replace all placeholder names with the name of the project
  4. run git add . to make nix aware of all required files
  5. run nix develop to 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>.initGit to decide, whether to automatically init a git repository
  • templates.<name>.replace as a list of strings, which will automatically be replaced. Their replacement value could be given to them in the call of nix init: nix init -t myTemplateFlake#templateName --replace <string>=<futureString>, but this could break the environment again.

wuyoli avatar Oct 12 '22 18:10 wuyoli

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

wuyoli avatar Jan 25 '23 15:01 wuyoli

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

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.

mainrs avatar May 22 '24 10:05 mainrs

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.

Libadoxon avatar Nov 14 '24 17:11 Libadoxon