Lib.optional considered harmful

This has been bugging me for a while, but I’ve never gotten around to writing something coherent about it. So I’ll just put the idea here since I’m curious if the community agrees.

I don’t like lib.optional. I think its highly counterintuitive that it wraps the result in a list. Here’s an example of an abuse: Merge pull request #80206 from Thra11/guile-gnutls · NixOS/nixpkgs@bbb8132 · GitHub

Can’t we just use lib.optionals instead? Yes, it requires two additional characters ([]) but I think that is well worth the clarity.

15 Likes

generally I use optionals + [ ], even for single elements, just to avoid this scenario. Also makes adding new elements easier on the diffs.

5 Likes

I like the idea.

Btw, stats:

$ rg  "optional " | wc -l
4576
$ rg  "optionals " | wc -l
2460
4 Likes

Personally I prefer optional. It feels “cleaner” and is easier to type. I don’t mind if this becomes policy with enough people agreeing that it’s a good thing though.

What I’m not keen on is people changing this without it being a documented policy, because otherwise we can end up with it being changed back and forth, wasting people’s time and introducing meaningless code churn.

So, people who feel strongly about this: please work on making it a policy (deprecate lib.optional?) before changing it willy-nilly in nixpkgs.

7 Likes