Jump to content

Overlays: Difference between revisions

m
imported>Jtojnar
(Replace self/super with new convention final/prev)
Tags: Mobile edit Mobile web edit
 
(7 intermediate revisions by 6 users not shown)
Line 18: Line 18:
== Data flow of overlays ==
== Data flow of overlays ==


The data flow around overlays, especially regarding <tt>prev</tt> and <tt>final</tt> arguments can be a bit confusing if you are not familiar with how overlays work. This graph shows the data flow:
The data flow of overlays, especially regarding <tt>prev</tt> and <tt>final</tt> arguments can be a bit confusing if you are not familiar with how overlays work. This graph shows the data flow:


[[File:Dram-overlay-final-prev.png]]
[[File:Dram-overlay-final-prev.png]]
Line 24: Line 24:
Here the main package set is extended with two overlays, ext-1 and ext-2. <tt>x // y</tt> is represented by a <tt>//</tt> box with <tt>x</tt> coming in from the left and <tt>y</tt> from above.
Here the main package set is extended with two overlays, ext-1 and ext-2. <tt>x // y</tt> is represented by a <tt>//</tt> box with <tt>x</tt> coming in from the left and <tt>y</tt> from above.


As you can see, <tt>final</tt> is the same for every stage, but <tt>prev</tt> comes from only the stage before. So when you define an attribute <tt>foo</tt> in the set to override it, within that overlay <tt>final.foo</tt> will be it's version, and <tt>prev.foo</tt> will be the non-overriden version. This is why you see patterns like <tt>foo = prev.foo.override { ... }</tt>.
As you can see, <tt>final</tt> is the same for every stage, but <tt>prev</tt> comes from only the stage before. So when you define an attribute <tt>foo</tt> in the set to override it, within that overlay <tt>final.foo</tt> will be its version, and <tt>prev.foo</tt> will be the non-overriden version. This is why you see patterns like <tt>foo = prev.foo.override { ... }</tt>.


The names <tt>final</tt> and <tt>prev</tt> might remind you of inheritance in object-oriented languages. In fact, overlays are exactly the same thing as subclasses, with regards to overriding and calling methods. This data flow is also how objects know which method to call. This is probably why the two arguments got their names, too.
The names <tt>final</tt> and <tt>prev</tt> might remind you of inheritance in object-oriented languages. In fact, overlays are exactly the same thing as subclasses, with regards to overriding and calling methods. This data flow is also how objects know which method to call. This is probably how the two arguments got their names, too.


== Data flow of overlays (alternative explanation) ==
== Data flow of overlays (alternative explanation) ==
Line 43: Line 43:




And <syntaxhighlight lang="nix">final: prev: firefox = final.firefox.override { ... };</syntaxhighlight> would cause infinite recursion.
And <syntaxhighlight lang="nix">final: prev: { firefox = final.firefox.override { ... }; }</syntaxhighlight> would cause infinite recursion.


== Using overlays ==
== Using overlays ==
Line 148: Line 148:
</syntaxhighlight>
</syntaxhighlight>


Then, add the following contents to <tt>/etc/nixos/overlays-compat/overlays.nix</tt><ref>Based on [https://gitlab.com/samueldr/nixos-configuration/blob/3febd83b15210282d6435932944d426cd0a9e0ca/modules/overlays-compat/overlays.nix [[User:samueldr|@samueldr]]'s configuration: overlays-compat]</ref>:
Then, add the following contents to <tt>/etc/nixos/overlays-compat/overlays.nix</tt><ref>Based on [https://gitlab.com/samueldr/nixos-configuration/blob/3febd83b15210282d6435932944d426cd0a9e0ca/modules/overlays-compat/overlays.nix][[User:Samueldr|@samueldr]]<span>'s configuration: overlays-compat</span></ref>:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 225: Line 225:
final: prev: {
final: prev: {
   # elements of pkgs.gnome must be taken from gfinal and gprev
   # elements of pkgs.gnome must be taken from gfinal and gprev
   gnome = prev.gnome.overrideScope' (gfinal: gprev: {
   gnome = prev.gnome.overrideScope (gfinal: gprev: {
     mutter = gprev.mutter.overrideAttrs (oldAttrs: {
     mutter = gprev.mutter.overrideAttrs (oldAttrs: {
       patches = oldAttrs.patches ++ [
       patches = oldAttrs.patches ++ [
Line 253: Line 253:
</syntaxhighlight>
</syntaxhighlight>


=== Overriding a package inside an attribute set ===
=== Overriding a package inside an extensible attribute set ===


Here is an example of adding plugins to `vimPlugins`.
Here is an example of adding plugins to `vimPlugins`.
Line 262: Line 262:
   });
   });
}
}
</syntaxhighlight>
=== Overrding a package inside a plain attribute set ===
Here's an example of overriding the source of <code>obs-studio-plugins.obs-backgroundremoval</code>.
<syntaxhighlight lang="nix">
    final: prev: {
      obs-studio-plugins = prev.obs-studio-plugins // {
        obs-backgroundremoval =
          prev.obs-studio-plugins.obs-backgroundremoval.overrideAttrs (old: {
            version = "0.5.17";
            src = prev.fetchFromGitHub {
              owner = "royshil";
              repo = "obs-backgroundremoval";
              rev = "v0.5.17";
              hash = "";
            };
          });
      };
    };
</syntaxhighlight>
</syntaxhighlight>


Line 307: Line 328:
=== R Packages Overlay ===
=== R Packages Overlay ===


Here is an example of an R packages overlay, in which it can be seen how to provide different versions of packages then those available in the current R version. It should be noted that in the case of R and Python the argument to <code>override</code> is named differently. Names of these can be find using <code>nix repl</code> and evaluating e.g. <code>python.override.__functionArgs</code>.
Here is an example of an R packages overlay, in which it can be seen how to provide different versions of packages then those available in the current R version. It should be noted that in the case of R and Python the argument to <code>override</code> is named differently. Names of these can be found using <code>nix repl</code> and evaluating e.g. <code>python.override.__functionArgs</code>.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 376: Line 397:
* [https://nixos.org/manual/nixpkgs/unstable/#using-community-maintained-rust-toolchains Details in the Nixpkgs manual for using Rust overlays]
* [https://nixos.org/manual/nixpkgs/unstable/#using-community-maintained-rust-toolchains Details in the Nixpkgs manual for using Rust overlays]
* [https://github.com/peter-sa/nixos-rocm Overlay for Radeon Open-Compute packages]
* [https://github.com/peter-sa/nixos-rocm Overlay for Radeon Open-Compute packages]
* [https://github.com/garbas/nixpkgs-python Overlay by Rok Garbas for a set of python packages built by pypi2nix]
* [https://github.com/garbas/nixpkgs-python Overlay by Rok Garbas for a set of python packages built by pypi2nix (archived)]


== See also ==
== See also ==
Line 382: Line 403:
* [https://nixos.org/nixpkgs/manual/#chap-overlays Overlays  in nixpkgs manual]
* [https://nixos.org/nixpkgs/manual/#chap-overlays Overlays  in nixpkgs manual]
* [https://blog.flyingcircus.io/2017/11/07/nixos-the-dos-and-donts-of-nixpkgs-overlays/ Blog post "The DOs and DON’Ts of nixpkgs overlays"]
* [https://blog.flyingcircus.io/2017/11/07/nixos-the-dos-and-donts-of-nixpkgs-overlays/ Blog post "The DOs and DON’Ts of nixpkgs overlays"]
* [https://www.youtube.com/watch?v=s2fkgkN55vk&list=PLgknCdxP89ReD6gxl755B6G_CI65z4J2e Nixpkgs Overlays – A place for all excluded packages] - Talk by Nicolas B. Pierron at NixCon 2017


==== References ====
==== References ====
1

edit