Convenient way to capture functions in "let" sections?

So I run into this a lot. I will be writing some script and find a really useful function defined in the “let” section of another package or module; but really the only way to use it is to copy and paste the whole thing into my file.

Is there a cleaner way to call on those functions that I’m unaware of?
Otherwise it might be worthwhile to move several of those functions into “lib” style files for this purpose. Customizing documentation code was specifically what got me thinking about this, but I find that It’s a common occurrence.

Ex: Lets say I want to borrow an arbitrary function foo.

{config, lib, pkgs, ... }:
let
    foo = x: { ... loads of useful code ... };
in mkDerivation { ... }

I agree! At least a convention of putting function definitions into a lib.nix file alongside a default.nix, as an example, could be a good start. Some thought is needed for how to organize useful functions in the wider world of nixpkgs, but at least making them importable seems like a positive step.

2 Likes

In my head I had thought of just adding a ton of functions to the existing “lib” files, but I like your idea of pairing “lib.nix” and “default.nix” a lot more. It would help keep things organized for sure and keep functions accessible.