Jump to content

Overlays: Difference between revisions

m
imported>Figsoda
m (update information for using rust overlays)
Tags: Mobile edit Mobile web edit
 
(8 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<blockquote>Overlays are Nix functions which accept two arguments, conventionally called self and super, and return a set of packages. ... Overlays are similar to other methods for customizing Nixpkgs, in particular the packageOverrides ... Indeed, packageOverrides acts as an overlay with only the super argument. It is therefore appropriate for basic use, but overlays are more powerful and easier to distribute.</blockquote>
<blockquote>Overlays are Nix functions which accept two arguments, conventionally called <code>final</code> and <code>prev</code> (formerly also <code>self</code> and <code>super</code>), and return a set of packages. ... Overlays are similar to other methods for customizing Nixpkgs, in particular the packageOverrides ... Indeed, packageOverrides acts as an overlay with only the <code>prev</code> (<code>super</code>) argument. It is therefore appropriate for basic use, but overlays are more powerful and easier to distribute.</blockquote>


<cite>From the [https://nixos.org/manual/nixpkgs/stable/#sec-overlays-definition Nixpkgs manual]</cite>
<cite>From the [https://nixos.org/manual/nixpkgs/stable/#sec-overlays-definition Nixpkgs manual]</cite>
Line 8: Line 8:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super: {
final: prev: {
   google-chrome = super.google-chrome.override {
   google-chrome = prev.google-chrome.override {
     commandLineArgs =
     commandLineArgs =
       "--proxy-server='https=127.0.0.1:3128;http=127.0.0.1:3128'";
       "--proxy-server='https=127.0.0.1:3128;http=127.0.0.1:3128'";
Line 18: Line 18:
== Data flow of overlays ==
== Data flow of overlays ==


The data flow around overlays, especially regarding <tt>super</tt> and <tt>self</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-self-super.png]]
[[File:Dram-overlay-final-prev.png]]


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>self</tt> is the same for every stage, but <tt>super</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>self.foo</tt> will be it's version, and <tt>super.foo</tt> will be the non-overriden version. This is why you see patterns like <tt>foo = super.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>self</tt> and <tt>super</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 80: Line 80:
{
{
   # [...]
   # [...]
   nixpkgs.overlays = [ (self: super: /* overlay goes here */) ];
   nixpkgs.overlays = [ (final: prev: /* overlay goes here */) ];
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 95: Line 95:
{
{
   # [...]
   # [...]
   nixpkgs.overlays = [ (self: super: /* overlay goes here */) ];
   nixpkgs.overlays = [ (final: prev: /* overlay goes here */) ];
}
}
</syntaxhighlight>
</syntaxhighlight>
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">
self: super:
final: prev:
with super.lib;
with prev.lib;
let
let
   # Load the system config and get the `nixpkgs.overlays` option
   # Load the system config and get the `nixpkgs.overlays` option
Line 158: Line 158:
in
in
   # Apply all overlays to the input of the current "main" overlay
   # Apply all overlays to the input of the current "main" overlay
   foldl' (flip extends) (_: super) overlays self
   foldl' (flip extends) (_: prev) overlays final
</syntaxhighlight>
</syntaxhighlight>


Line 169: Line 169:
This method will fail if the build system changed or new dependencies are required.
This method will fail if the build system changed or new dependencies are required.
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super:
final: prev:
{
{
   sl = super.sl.overrideAttrs (old: {
   sl = prev.sl.overrideAttrs (old: {
     src = super.fetchFromGitHub {
     src = prev.fetchFromGitHub {
       owner = "mtoyoda";
       owner = "mtoyoda";
       repo = "sl";
       repo = "sl";
Line 193: Line 193:
It is easy to add patches to a nix package:
It is easy to add patches to a nix package:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super:
final: prev:
{
{
   sl = super.sl.overrideAttrs (old: {
   sl = prev.sl.overrideAttrs (old: {
     patches = (old.patches or []) ++ [
     patches = (old.patches or []) ++ [
       (super.fetchpatch {
       (prev.fetchpatch {
         url = "https://github.com/charlieLehman/sl/commit/e20abbd7e1ee26af53f34451a8f7ad79b27a4c0a.patch";
         url = "https://github.com/charlieLehman/sl/commit/e20abbd7e1ee26af53f34451a8f7ad79b27a4c0a.patch";
         hash = "07sx98d422589gxr8wflfpkdd0k44kbagxl3b51i56ky2wfix7rc";
         hash = "07sx98d422589gxr8wflfpkdd0k44kbagxl3b51i56ky2wfix7rc";
Line 213: Line 213:
Some packages provide compilation options. Those are not easily disoverable; to find them you need to have a look at the source. For example, with <code>nix edit -f "<nixpkgs>" pass</code> one can see that pass can be compiled with or without dependencies on X11 with the <code>x11Support</code> argument. Here is how you can remove X11 dependencies:
Some packages provide compilation options. Those are not easily disoverable; to find them you need to have a look at the source. For example, with <code>nix edit -f "<nixpkgs>" pass</code> one can see that pass can be compiled with or without dependencies on X11 with the <code>x11Support</code> argument. Here is how you can remove X11 dependencies:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super:
final: prev:
{
{
   pass = super.pass.override { x11Support = false; };
   pass = prev.pass.override { x11Support = false; };
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 222: Line 222:
Some packages are not in the top level of nixpkgs but inside a ''scope''. For example all [[GNOME]] packages are in the <code>gnome</code> attribute set and [[Xfce]] packages inside <code>xfce</code>. These attributes are often ''scopes'' and must be overriden specially. Here is an example of patching <code>gnome.mutter</code> and <code>gnome.gnome-control-center</code>.
Some packages are not in the top level of nixpkgs but inside a ''scope''. For example all [[GNOME]] packages are in the <code>gnome</code> attribute set and [[Xfce]] packages inside <code>xfce</code>. These attributes are often ''scopes'' and must be overriden specially. Here is an example of patching <code>gnome.mutter</code> and <code>gnome.gnome-control-center</code>.
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# elements of nixpkgs must be taken from self and super
# elements of nixpkgs must be taken from final and prev
self: super: {
final: prev: {
   # elements of pkgs.gnome must be taken from gself and gsuper
   # elements of pkgs.gnome must be taken from gfinal and gprev
   gnome = super.gnome.overrideScope' (gself: gsuper: {
   gnome = prev.gnome.overrideScope (gfinal: gprev: {
     mutter = gsuper.mutter.overrideAttrs (oldAttrs: {
     mutter = gprev.mutter.overrideAttrs (oldAttrs: {
       patches = oldAttrs.patches ++ [
       patches = oldAttrs.patches ++ [
         # https://salsa.debian.org/gnome-team/mutter/-/blob/ubuntu/master/debian/patches/x11-Add-support-for-fractional-scaling-using-Randr.patch
         # https://salsa.debian.org/gnome-team/mutter/-/blob/ubuntu/master/debian/patches/x11-Add-support-for-fractional-scaling-using-Randr.patch
         (super.fetchpatch {
         (prev.fetchpatch {
           url = "https://salsa.debian.org/gnome-team/mutter/-/raw/91d9bdafd5d624fe1f40f4be48663014830eee78/debian/patches/x11-Add-support-for-fractional-scaling-using-Randr.patch";
           url = "https://salsa.debian.org/gnome-team/mutter/-/raw/91d9bdafd5d624fe1f40f4be48663014830eee78/debian/patches/x11-Add-support-for-fractional-scaling-using-Randr.patch";
           hash = "m6PKjVxhGVuzsMBVA82UyJ6Cb1s6SMI0eRooa+F2MY8=";
           hash = "m6PKjVxhGVuzsMBVA82UyJ6Cb1s6SMI0eRooa+F2MY8=";
Line 235: Line 235:
     ];
     ];
     });
     });
     gnome-control-center = gsuper.gnome-control-center.overrideAttrs (oldAttrs: {
     gnome-control-center = gprev.gnome-control-center.overrideAttrs (oldAttrs: {
       patches = oldAttrs.patches ++ [
       patches = oldAttrs.patches ++ [
         # https://salsa.debian.org/gnome-team/gnome-control-center/-/blob/ubuntu/master/debian/patches/ubuntu/display-Support-UI-scaled-logical-monitor-mode.patch
         # https://salsa.debian.org/gnome-team/gnome-control-center/-/blob/ubuntu/master/debian/patches/ubuntu/display-Support-UI-scaled-logical-monitor-mode.patch
         (super.fetchpatch {
         (prev.fetchpatch {
           url = "https://salsa.debian.org/gnome-team/gnome-control-center/-/raw/f185f33fb200cc963c062c7a82920a085f696978/debian/patches/ubuntu/display-Support-UI-scaled-logical-monitor-mode.patch";
           url = "https://salsa.debian.org/gnome-team/gnome-control-center/-/raw/f185f33fb200cc963c062c7a82920a085f696978/debian/patches/ubuntu/display-Support-UI-scaled-logical-monitor-mode.patch";
           hash = "XBMD0chaV6GGg3R9/rQnsBejXspomVZz/a4Bvv/AHCA=";
           hash = "XBMD0chaV6GGg3R9/rQnsBejXspomVZz/a4Bvv/AHCA=";
         })
         })
         # https://salsa.debian.org/gnome-team/gnome-control-center/-/blob/ubuntu/master/debian/patches/ubuntu/display-Allow-fractional-scaling-to-be-enabled.patch
         # https://salsa.debian.org/gnome-team/gnome-control-center/-/blob/ubuntu/master/debian/patches/ubuntu/display-Allow-fractional-scaling-to-be-enabled.patch
         (super.fetchpatch {
         (prev.fetchpatch {
           url = "https://salsa.debian.org/gnome-team/gnome-control-center/-/raw/f185f33fb200cc963c062c7a82920a085f696978/debian/patches/ubuntu/display-Allow-fractional-scaling-to-be-enabled.patch";
           url = "https://salsa.debian.org/gnome-team/gnome-control-center/-/raw/f185f33fb200cc963c062c7a82920a085f696978/debian/patches/ubuntu/display-Allow-fractional-scaling-to-be-enabled.patch";
           hash = "Pm6PTmsL2bW9JAHD1u0oUEqD1PCIErOlcuqlwvP593I=";
           hash = "Pm6PTmsL2bW9JAHD1u0oUEqD1PCIErOlcuqlwvP593I=";
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`.
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super: {
final: prev: {
   vimPlugins = super.vimPlugins.extend (self': super': {
   vimPlugins = prev.vimPlugins.extend (final': prev': {
     indent-blankline-nvim-lua = super.callPackage ../packages/indent-blankline-nvim-lua { };
     indent-blankline-nvim-lua = prev.callPackage ../packages/indent-blankline-nvim-lua { };
   });
   });
}
}
</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 272: Line 293:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super:
final: prev:
# Within the overlay we use a recursive set, though I think we can use `self` as well.
# Within the overlay we use a recursive set, though I think we can use `final` as well.
rec {
rec {
   # nix-shell -p python.pkgs.my_stuff
   # nix-shell -p python.pkgs.my_stuff
   python = super.python.override {
   python = prev.python.override {
     # Careful, we're using a different self and super here!
     # Careful, we're using a different final and prev here!
     packageOverrides = self: super: {
     packageOverrides = final: prev: {
       my_stuff = super.buildPythonPackage rec {
       my_stuff = prev.buildPythonPackage rec {
         pname = "pyaes";
         pname = "pyaes";
         version = "1.6.0";
         version = "1.6.0";
         src = super.fetchPypi {
         src = prev.fetchPypi {
           inherit pname version;
           inherit pname version;
           hash = "0bp9bjqy1n6ij1zb86wz9lqa1dhla8qr1d7w2kxyn7jbj56sbmcw";
           hash = "0bp9bjqy1n6ij1zb86wz9lqa1dhla8qr1d7w2kxyn7jbj56sbmcw";
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">
self: super:
final: prev:


{
{
   rPackages = super.rPackages.override {
   rPackages = prev.rPackages.override {
     overrides = {
     overrides = {


       rprojroot = super.rPackages.buildRPackage rec {
       rprojroot = prev.rPackages.buildRPackage rec {
         name = "rprojroot-${version}";
         name = "rprojroot-${version}";
         version = "2.0.2";
         version = "2.0.2";
         src = super.fetchurl {
         src = prev.fetchurl {
           url =
           url =
             "https://github.com/r-lib/rprojroot/archive/refs/tags/v2.0.2.tar.gz";
             "https://github.com/r-lib/rprojroot/archive/refs/tags/v2.0.2.tar.gz";
           hash = "1i0s1f7hla91yw1fdx0rn7c18dp6jwmg2mlww8dix1kk7qbxfjww";
           hash = "1i0s1f7hla91yw1fdx0rn7c18dp6jwmg2mlww8dix1kk7qbxfjww";
         };
         };
         nativeBuildInputs = [ super.R ];
         nativeBuildInputs = [ prev.R ];
       };
       };


       here = super.rPackages.buildRPackage rec {
       here = prev.rPackages.buildRPackage rec {
         name = "here-${version}";
         name = "here-${version}";
         version = "1.0.1";
         version = "1.0.1";
         src = super.fetchurl {
         src = prev.fetchurl {
           url = "https://github.com/r-lib/here/archive/refs/tags/v1.0.1.tar.gz";
           url = "https://github.com/r-lib/here/archive/refs/tags/v1.0.1.tar.gz";
           hash = "0ky6sq6n8px3b70s10hy99sccf3vcjjpdhamql5dr7i9igsf8nqy";
           hash = "0ky6sq6n8px3b70s10hy99sccf3vcjjpdhamql5dr7i9igsf8nqy";
         };
         };
         nativeBuildInputs = [ super.R self.rPackages.rprojroot ];
         nativeBuildInputs = [ prev.R final.rPackages.rprojroot ];
         propagatedBuildInputs = [ self.rPackages.rprojroot ];
         propagatedBuildInputs = [ final.rPackages.rprojroot ];
       };
       };
     };
     };
Line 349: Line 370:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
self: super: {
final: prev: {
   rnix-lsp = super.rnix-lsp.overrideAttrs (oldAttrs: rec {
   rnix-lsp = prev.rnix-lsp.overrideAttrs (oldAttrs: rec {
     version = "master";
     version = "master";
    
    
     src = super.fetchFromGitHub {
     src = prev.fetchFromGitHub {
       owner = "nix-community";
       owner = "nix-community";
       repo = "rnix-lsp";
       repo = "rnix-lsp";
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