inheritancenixdiamond-problemfixpoint-combinatorsnixpkgs

How to prevent a Nixpkgs overlay from being applied more than once?


Suppose I have some Nixpkgs overlays composed from other overlays:

overlayFoo = self: super: {
  # ...
};
overlayBar = pkgs.lib.fixedPoints.composeExtensions overlayFoo (self: super: {
  # ...
};
overlayBaz = pkgs.lib.fixedPoints.composeExtensions overlayFoo (self: super: {
  # ...
});

And the user of the overlays would like to use them like this:

pkgs = import nixpkgs {
  inherit system;
  overlays = [overlayBar overlayBaz];
};

Since both overlayBar and overlayBaz depend on overlayFoo, how can I prevent overlayFoo from being applied more than once?


Solution

  • As mentioned in https://discourse.nixos.org/t/how-to-prevent-a-overlay-from-being-applied-more-than-once/27312/2?u=atry

    You could add a marker to nixpkgs to check if the overlay has been applied already:

    overlayFoo = self: super: self.lib.optionalAttrs (!super.__fooHasBeenApplied or false) {
      __fooHasBeenApplied = true;
      # ...
    };