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

74 Views Asked by At

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?

1

There are 1 best solutions below

0
Yang Bo On BEST ANSWER

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;
  # ...
};