Can someone explain the magic of `makeOverridable` to me?

I am trying to understand the makeOverridable that being used in nixpkgs, and recreated one for learning purpose:

The original code:

My attempts:

      mkExtendable = f: origArgs:
        ((f origArgs) // {
          extend = newArgs:
            (mkExtendable f
              (nixpkgs.lib.attrsets.recursiveUpdate origArgs newArgs));
        });

And I am trying to use this to create nixpkgs.lib.nixosSystem:

      mkSystem = mkExtendable ({ system, inputs, modules, extraModules ? [ ] }:
        inputs.nixpkgs.lib.nixosSystem {
          inherit system;
          modules = modules ++ extraModules;
          specialArgs = { inherit inputs; };
        });

But when I run this from my flake, I got an error from here:

Which obviously the extend I have there is a bad attrs for a proper Nix Module. My question is, how can makeOverridable get away with override? It should be considered as bad attrs as well isn’t it?

I suggest you read the Nix Pills and particularly Chapter 14. Override Design Pattern.

1 Like