Why is my lib overlay not available in args?

I’m trying to provide some helper functions for easy usage across my nixos configuration using the nixpkgs.overlays functionality. Something like this:

{ ... }:
{
  # Helpers avaialble under pkgs.lib.
  nixpkgs.overlays = [
    (prev: final: {
      lib = final.lib // {
        helper = arg: something something;
      };
    })
  ];
}

This works if I access the helper via pkgs.lib.helper, but it does not if I try to access it via lib.helper:

{ lib, ... }:

{
  environment.etc.test.text = lib.helper "wtf";
}
error: attribute 'helper' missing, at /etc/nixos/configuration.nix:X:Y

Why is this? And is it even possible to provide my helper in the lib available in the arguments?

Ideally I would love to be able to do it like this:

{ helper, ... }:

But I assume that’s not doable.

You can use specialArgs or _module.args for that. Like:

    nixosConfigurations = {
      nixos = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        specialArgs = { inherit helper; };

        modules = [
          ./configuration.nix
        ];
      };

But I don’t know how to do it without flakes.

1 Like

nixpkgs redefines lib, so editing it in an overlay doesn’t affect the lib from the NixOS module system. You can set _module.args.helper to add a helper argument to the NixOS module system if you want (be warned, this sometimes makes confusing infinite recursion errors if you’re not careful).

Also, tangential FYI, I think you have prev and final backwards in your overlay. The first argument represents the final pkgs, and the second argument represents pkgs before this overlay is applied.

1 Like

You can set _module.args.helper to add a helper argument to the NixOS module system if you want (be warned, this sometimes makes confusing infinite recursion errors if you’re not careful).

Wow, this works well! Though it does seem like I’m touching something internal that might break in the future, but it does simplify passing of various helpers a LOT. Thanks a bunch.

@ilkecan I don’t use flakes, but I do have to get into them eventually. Thanks.