Allow custom Zig init

I always found that Zig init is a bit annoying, it is either too big, or too has nothing if you use -m flag. For me it is nice to have basic build and then main with hello world, but you can’t keep adding flags for each setup, so I think it would be nice if Zig added some “zig init -c” (–custom) flag where, you would be able to set an environment variable or smth, to point to a folder where you have your perfect init, and it would just copy that. Maybe replace the name the name with the name of the current folder and that’s it.

I guess you could create a tool for that or just use cp, but having build in way would be nice.

5 Likes

There are similar tools, check out this topic: Liza: Zig codebase initializer

4 Likes

I think zig init is made mostly for newcomers to the language, so they have an easy starting point for playing with Zig as a language. If you are already at the level that you are considering making your own template I would recommend doing just that.

I have multiple “init” templates for multiple languages, all I do is clone my template repository and then switch the remote to the new repository.

My recommendation for making your own template is because any template won’t be perfect for every use case and for every person, making your own means you can make it perfect for yourself

8 Likes

I agree i have a nice little bash function hat looks like this for cloning them :

function ginit {
    git clone "git@github:aleod/tempalte-$1.git"
    git remote rm origin
}

and i call it like this:

ginit zig
# or with some deps for graphical apps for example
ginit zig-graphics
1 Like

Would that mean all of your repositories share the same root/initial commit as the template?

Yes but it’s not really a problem is it? And if it’s really a problem you can amend the commit so that the sha changes.

1 Like

can you elaborate? in what way would having it built in be better than a third party tool, or making your own utility?

1 Like

I had this same thought before, and ended up just making my own script to do it. After doing so, I realized it was better this way; there is just too many minor variations in what someone may want specifically for their project, and it will never satisfy anyone.

My little script works perfectly well for me, and initializes to my personal preferences, but they likely would not be perfect for you, in which case you would be making modifications and right back where we started.

Sure, there could be some third-party tools, but by the time you configure it to your personal tastes, you arguably could have just implemented it yourself: it is essentially just printing boilerplate text to files and running a few shell commands, depending on what you want.

My script logic:

  • mkdir and cd to name specified on commandline
  • Because I am too dumb to know how to get the fingerprint value for build.zig.zon, I just use zig init -m to create it, then delete everything else
  • Print my own boilerplate to root.zig if this is lib
  • Print my own boilerplate to main.zig if this is exe
  • Print my own boilerplate to build.zig depending if lib, exe, or both
  • Add LICENSE, CHANGELOG.md, and README.md
  • Add .gitignore with boilerplate
  • Run git init, git add ., and commit boilerplate with custom message

As you can see, nothing surprising, and all extremely simple, BUT each and every step of it is personalized. Almost everyone would like to change some aspect of this for themselves, at which point it would often be easier to just make it themselves instead of configuration files and/or elaborate CLI arguments.

4 Likes

You could probably make a custom tool for this, but I consider this to be a very useful that brings a bunch of benefits. I also think that custom init templates are working like, the more people use them, the more value they bring so that is why it would be beneficial to have them in Zig itself and not a, no offence, tool that 90% of the community doesn’t know about. And as example I can bring myself, probably should have done more research, but I did know smth like Lisa has existed.

For why I think they are useful:

  1. I think the idea of custom templates is in itself is useful. Like to be able to start a project from somewhere already and not having to setup everything is just nice.
  2. Allows for easier sharing of projects. Instead of going to a repo, fetching the project as dependency, reading examples from there. You can just get some template and it would put the dependencies, and starting code to do a bit of smth and you will be able to run it and see how it works. It would just be a smoother experience then rn I feel like.
  3. (Personal) I don’t like small, hyperspecific tools and scripts bloating my system, I already have a lot of them to a point I forget about them all the time.

I forgot who told me this[1], and I can’t find the thread, but zig init just copies the lib/init directory from your Zig install dir (replacing the placeholder values as well). You can modify the files in lib/init to create your perfect init, then zig init will copy them over.

If you’re like me and you update your Zig often to track the master branch, this becomes a bit more tedious, but still very doable, and, IMO, better than any other third-party tools.

[1] Found it: Default or implicit `build.zig` - #2 by biggest_endian

4 Likes

When I started my last project, I totally forgot zig init is a thing, so I wrote my own build.zig completely relearning from the documentation. :melting_face:

Honestly, at least for me, I don’t see the need for a custom zig init at all. How often do you all setup a new project, that this would be a meaningful change?

If I do setup a new project I usually just do zig init, vim build.zig, run %s,//.*\n, in it, let zig format it and delete the lines I don’t need. And possibly delete src/root.zig, or src/main.zig. This takes at most ~1min.

I agree though that zig init -m could maybe do a bit more like having the default optimize and options, maybe templating some module, or setting paths in build.zig.zon to something reasonable. But at this point just deleting from the “normal” one becomes an option again.

Personally, if zig init had a “no comments” flag it would be very usable for me, another idea is for zig to offer a few more zig inits prepared although I don’t have something specific to suggest unfortunately, It feels like the default has too much and the minimal has too little for me.

6 Likes

This is the only change I would need to be happy. The default zig init is great, and I get that the comments are to help first time users, but it would be nice to remove most of the comments (keep the fingerprint warning because that needs to stay) as a flag.

9 Likes

For the build in init function that’s def the only change I see as useful. For more complex stuff custom scripts/programmes are better solution