How to inject custom module arguments?

Hi there,

I’d like to inject a custom library myLib into the module arguments so that every module that I import using imports = [ ]; (and that in turn imports other modules the same way) can access it like so:

{ config, pkgs, lib, myLib, ...}:

  # ...

I’ve thought of something like an overlay but for modules instead of packages…
Is this possible and if so, how can I achieve it?

You could set _module.args.myLib = {...} in some module.
More info in Passing in extra arguments to configuration.nix

1 Like

Thank you @ayyess, that’s exactly what I was looking for. However, it feels like I’m abusing some internals of Nix here (the underscore gives me the impression of “this is private / internal”). It is also not mentioned in the options search. Is it safe to use it or can it change without warning?

It’s safe to use, the underscore is, afaik, more to indicate that it’s kind of internal to the module evaluation, meaning that it will not show up in the final config value.

Specifically for your case though, you could also use the lib option: NixOS Search

You assign your functions to it, and then access it in any other module as config.lib.

I think the main reason for the underscore is that because this is an option that’s available by default to all module evaluations, it could easily cause conflicts with options defined by a user. E.g. if it was called module.args instead and somebody happens to declare a module option (of which there are a few!), you’d get some conflict error and users would have to pick a different name. By using _module, the entire namespace of alphabetic option names is available to module system users.

Ah, I see. Thank you all for your replies :smiling_face: