Setting priority of a package from an overlay

I’m trying out Nix by setting up my environment with nix-env. I do C++ development so I would like to have both gcc and clang installed on my machine. So my first attempt is to just naively run

nix-env -iA nixpkgs.gcc

This runs just fine, but the problem comes after i ran

nix-env -iA nixpkgs.clang

The message error basically says that there’s a conflict since both gcc and clang are trying to create a symlink with the name cc, and suggests that I set the priority for one of the two. So I look around to see how do i set the priority of a package (and it was extremely unclear how I should do this).

I found out that overlay is the way to go, and I create a file ~/.config/nixpkgs/overlays.nix with its content as follows:

[
  (self: super: {
    clang = super.lowPrio super.clang;
  })
]

which didn’t work since super.lowPrio sets the package’s priority to 10 which is same as before. So I tried

[
  (self: super: {
    clang = super.setPrio 15 super.clang;
  })
]

which also didn’t work with the error saying setPrio doesn’t exist, and I failed to understand why. Afterward, I looked at how setPrio is defined and came up with this

[
  (self: super: {
    clang = super.clang.overrideAttrs (oldAttrs: rec{
      meta = oldAttrs.meta // { priority = 15; };
    });
  })
]

which works and allows me to install both gcc and clang. So I just want to share my experience, in case anyone fumbles through the same problem since it took quite a while to figure this out as a beginner.

Since those functions are defined lib/meta.nix, you will find them under the lib attribute of pkgs (also self and super in an overlay). The reason lowPrio and hiPrio are also defined as an attribute of pkgs is they are explicitly inherited in here.

So this should work

[
  (self: super: {
    clang = super.lib.setPrio 15 super.clang;
  })
]
2 Likes

Maybe this is simpler way:

{
  packageOverrides = pkgs: {
    clang = pkgs.clang.overrideAttrs (attrs: {
      # Lower priority than gcc.
      meta.priority = pkgs.gcc.meta.priority + 1;
    });
  };
}

from configuration/nixpkgs.nix at 46c7498833eecd40a70454835c68246a70b4b516 · splintah/configuration · GitHub

found with Sign in to GitHub · GitHub