Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parametrize templates #7

Open
4 of 5 tasks
srid opened this issue Jun 7, 2024 · 5 comments
Open
4 of 5 tasks

Parametrize templates #7

srid opened this issue Jun 7, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@srid
Copy link
Member

srid commented Jun 7, 2024

Enrich nix flake init with parameters

Considerations for parameters

Currently, the parameters introduced in #5 do a simple placeholder replacement across files (content & filename). But we may also need other kinds of substitutions, viz.:

Other considerations:

@srid srid added the enhancement New feature or request label Jun 7, 2024
@srid srid added this to the General Availability milestone Jun 7, 2024
srid added a commit that referenced this issue Jun 7, 2024
This includes https://github.com/srid/haskell-template in full piece
until we do the boolean type of #7
srid added a commit that referenced this issue Jun 7, 2024
This includes https://github.com/srid/haskell-template in full piece
until we do the boolean type of #7
@roberth
Copy link
Contributor

roberth commented Jun 11, 2024

Not sure if reinterpreting .templates is a good idea.
What if we had a module that exposes two flake attributes,

  • templates for nix flake init compatibility
  • templateGenerators for flakreate, containing a rich specification and functions that create the template
    the generation logic could be evaluation based (needing Add builtins.toDir NixOS/nix#3497), or derivation based (a bit slower, but more powerful)

Ultimately though, I think the solution isn't templates but "actions" that change an existing configuration. That way you start with a basic flake parts flake, and run a tool that can make various additions to it, or just add new module files to keep it simple.
They'd be a lot like templates, but unlike templates, you can use them in an automated way after you've initialized a project.
The initial template could write a mkFlake with imports from builtins.readDir ./nix/flake, and subsequent actions could write to new files in that directory and/or the rest of the project. This limits the amount of syntax tree editing to the flake.nix inputs, where something fairly simplistic could get the job done. All the other composition is handled by the module system.

@srid
Copy link
Member Author

srid commented Jul 3, 2024

@roberth Just coming back to this. Do you know how the templateGenerators thing would concretely look like?

Instead of the following, for example,

templates/flake.nix

Lines 33 to 45 in 440ec16

templates = rec {
nix-dev-home = inputs.nix-dev-home.templates.default // {
tags = [ "home-manager" "juspay" "development" ];
params = [
{
name = "Username";
help = "Your username as shown by by $USER";
default = "runner";
required = true;
files = [
"flake.nix"
];
}

what would the new templateGenerators-based flake look like?


{
  templates.nix-dev-home = inputs.nix-dev-home.templates.default;
  templateGenerators.nix-dev-home = ...;  # What's this?
}

I feel like there's some overlap with your nix dev idea (NixOS/nix#6241) mentioned in the flake-parts-cli RFC (flake-parts/cli#1 (comment)). What if flake-parts-cli itself provided a subcommand (i.e., "run arbitrary project-specific command") that did this initialization? It would be no different to any other subcommand, like one for autoformatting project tree.

Is this something we can implement today as part of flake-parts-cli (and potentially upstreamed, in some capacity, to nix in future, as part of nix dev perhaps)?

@srid
Copy link
Member Author

srid commented Jul 15, 2024

what would the new templateGenerators-based flake look like?

Posted a concrete idea juspay/omnix#152

Note: github:juspay/flakreate will be subsubed under github:juspay/omnix (which may also use flake-parts-cli at some point in the future).

@srid
Copy link
Member Author

srid commented Jul 24, 2024

Okay, I think this templateGenerators proposal sounds interesting, inasmuch as it can be upstreamed. Then, we can have a concrete generator in omnix, which I'm working on as part of: juspay/omnix#152

Since templateGenerators probably won't be in upstream anytime soon, I'll go ahead with a custom implementation in om init for now. But once upstreamed, we can switch om init to use the new nix flake init obviously.

@srid
Copy link
Member Author

srid commented Oct 1, 2024

I'll go ahead with a custom implementation in om init for now.

https://omnix.page/om/init.html#spec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants