NixOS / nix.dev

Official documentation for getting things done with Nix.
https://nix.dev
Creative Commons Attribution Share Alike 4.0 International
2.44k stars 245 forks source link

Overlays #116

Open amanjeev opened 3 years ago

amanjeev commented 3 years ago

Overlays are nice but are they the only way to patch a package for yourself? If not, then what are the other ways and how do they differ when compared to Overlays? When do we use Overlays? Pros Cons?

amanjeev commented 3 years ago

There are a few articles like https://blog.thomasheartman.com/posts/nix-override-packages-with-overlays and https://blog.flyingcircus.io/2017/11/07/nixos-the-dos-and-donts-of-nixpkgs-overlays/

amanjeev commented 3 years ago

I would love if the example for overlay and changing versions can be done with Jetbrains, say Clion https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/applications/editors/jetbrains/default.nix

I cannot figure this out to save my life.

nicknovitski commented 3 years ago

I also think it'd be good to have a basic explanation of overlays, and recommendations for their use; I use them very often when pinning dependencies for a project.

This is the simple explanation as I understand it: the top-level function of nixpkgs has an overlays argument, which can be used when you import it. An overlay is a function which takes two arguments and returns a set of packages which add to or override the imported package set, and if you override a package which other packages depend on, those other packages use that override.

So, building on the page describing how to use niv to pin dependencies, there could be an example like this:

{ sources ? import ./nix/sources.nix
}:   
let
  pkgs = import sources.nixpkgs { overlays = [ (self: super: {
    ruby = super.ruby_2_7;  # set `ruby` attribute to next minor version
  })]};
in
pkgs.mkShell {
  buildInputs = [ 
    pkgs.ruby # this is version 2.7.x
    # all of these tools are linked against ruby v2.7.x
    pkgs.cocoapods 
    pkgs.bundler 
    pkgs.bundler-audit
  ]; 
}