NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
11.6k stars 1.45k forks source link

More options for flake `outputs.templates` #7164

Open wuyoli opened 1 year ago

wuyoli commented 1 year ago

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:

wuyoli commented 1 year ago

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

mainrs commented 2 months ago

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.