NixOS / nix

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

What should be the default template? Or maybe there shouldn't be any default template when bootstrapping a Nix project. #4504

Open garbas opened 3 years ago

garbas commented 3 years ago

Currently when creating a project (using nix flake init or nix flake new) a simple and minimal default project is created. It contains only flake.nix. Current situation is probably suitable mostly for very advanced users.

I would suggest that we should not select any default template. Nix is used in many different and assuming the defaults in this space would be a mistake.

Rather then assuming let us ask the user which template to use. Here is how this could look like:

$ nix init
No template provided. Use `--template` to select the template.
For more templates please visit: https://search.nixos.org/templates.
Here are currently available templates:
- bash          | A project to write bash commands
- python       | A project for python commands
- minimal     | A minimal project
- ...

Nix could also detect if fzf binary is present on the system and provide a filter.

$ nix init
No template provided. Use `--template` to select the template.
For more templates please visit: https://search.nixos.org/templates.
Here are currently available templates:
> |
bash          | A project to write bash commands
python       | A project for python commands
minimal     | A minimal project

We must know that creating a project is not a common task (like nix build or nix develop). Also this command is going to primarily benefit new comers and users learning Nix. While it might be useful for advanced users, they should not be a target audience.

tomberek commented 3 years ago
diff --git a/src/nix/flake.cc b/src/nix/flake.cc
index 64fcfc000..80ae6d901 100644
--- a/src/nix/flake.cc
+++ b/src/nix/flake.cc
@@ -638,6 +638,12 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand

         auto [templateFlakeRef, templateName] = parseFlakeRefWithFragment(templateUrl, absPath("."));

+        if (templateName == "") {
+            notice("No template provided. Use `--template` or `-t` to select a template.");
+            notice("For available templates visit: https://github.com/NixOS/templates");
+            return;
+        }
+
         auto installable = InstallableFlake(nullptr,
             evalState, std::move(templateFlakeRef),
             Strings{templateName == "" ? "defaultTemplate" : templateName},