What's the best way to add my own utility functions?

Over the process of building my configuration for several different hosts, and now fooling with NixOps, I’ve built up a collection of helpful utility functions.

The problem is, I haven’t found a good way to make them accessible in any sort of global way. I’d like to be able to add them to lib, say in lib.<my-namespace>.someUtilFunc. That doesn’t seem to be possible…any kind of assignment to lib fails.

I end up just doing:

let
  my-lib = import ../path/to/my-lib.nix
in { ... }

…in every file where I need the utils. That’s kind of annoying, and seems clumsy. Or, I just end up rewriting the same functions in several different places.

Is there a way to do what I’d like and just add them to lib somehow?

(It just occurred to me that I could import my-lib early on, and pass it around as a sister arg to lib…that also seems a bit awkward, though).

Use an overlay that does lib = super.lib // { my-namespace = import funcs.nix; }

1 Like

I’m not personally familiar with the nuance of its usage, but you might also look at existing uses of extends and makeExtensible within nixpkgs:

I’m only passingly aware of it after seeing Henrik’s usage here:

Perfect, that helps! I’ll give it a shot. Thanks!