Sometimes one wants to override parts of nixpkgs, e.g. derivation attributes, the results of derivations.
These functions are used to make changes to packages, returning only single packages. Overlays, on the other hand, can be used to combine the overridden packages across the entire package set of Nixpkgs.
<pkg>.override
The function override is usually available for all the derivations in the nixpkgs expression (pkgs).
It is used to override the arguments passed to a function.
Example usages:
pkgs.foo.override { arg1 = val1; arg2 = val2; # ...}It’s also possible to access the previous arguments.
pkgs.foo.override (previous: { arg1 = previous.arg1; # ...})import pkgs.path { overlays = [ (self: super: { foo = super.foo.override { barSupport = true; }; }) ];}{ mypkg = pkgs.callPackage ./mypkg.nix { mydep = pkgs.mydep.override { # ... }; };}In the first example, pkgs.foo is the result of a function call with some default arguments, usually a derivation. Using pkgs.foo.override will call the same function with the given new arguments.
Many packages, like the foo example above, provide package options with default values in their arguments, to facilitate overriding.
Because it’s not usually feasible to test that packages build with all combinations of options, you might find that a package doesn’t build if you override options to non-default values.
Package maintainers are not expected to fix arbitrary combinations of options. If you find that something doesn’t work, please submit a fix, ideally with a regression test. If you want to ensure that things keep working, consider becoming a maintainer for the package.
<pkg>.overrideAttrs
The function overrideAttrs allows overriding the attribute set passed to a stdenv.mkDerivation call, producing a new derivation based on the original one. This function is available on all derivations produced by the stdenv.mkDerivation function, which is most packages in the Nixpkgs expression pkgs.
Example usages:
{ helloBar = pkgs.hello.overrideAttrs ( finalAttrs: previousAttrs: { pname = previousAttrs.pname + "-bar"; } );}In the above example, “-bar” is appended to the pname attribute, while all other attributes will be retained from the original hello package.
The argument previousAttrs is conventionally used to refer to the attr set originally passed to stdenv.mkDerivation.
The argument finalAttrs refers to the final attributes passed to mkDerivation, plus the finalPackage attribute which is equal to the result of mkDerivation or subsequent overrideAttrs calls.
If only a one-argument function is written, the argument has the meaning of previousAttrs.
Function arguments can be omitted entirely if there is no need to access previousAttrs or finalAttrs.
{ helloWithDebug = pkgs.hello.overrideAttrs { separateDebugInfo = true; }; }In the above example, the separateDebugInfo attribute is overridden to be true, thus building debug info for helloWithDebug.
<pkg>.overrideDerivation
The function overrideDerivation creates a new derivation based on an existing one by overriding the original’s attributes with the attribute set produced by the specified function. This function is available on all derivations defined using the makeOverridable function. Most standard derivation-producing functions, such as stdenv.mkDerivation, are defined using this function, which means most packages in the Nixpkgs expression, pkgs, have this function.
Example usage:
{ mySed = pkgs.gnused.overrideDerivation (oldAttrs: { name = "sed-4.2.2-pre"; src = fetchurl { url = "ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2"; hash = "sha256-MxBJRcM2rYzQYwJ5XKxhXTQByvSg5jZc5cSHEZoB2IY="; }; patches = [ ]; });}In the above example, the name, src, and patches of the derivation will be overridden, while all other attributes will be retained from the original derivation.
The argument oldAttrs is used to refer to the attribute set of the original derivation.
lib.makeOverridable
The function lib.makeOverridable is used to make the result of a function easily customizable. This utility only makes sense for functions that accept an argument set and return an attribute set.
Example usage:
{ f = { a, b }: { result = a + b; }; c = lib.makeOverridable f { a = 1; b = 2; };}The variable c is the value of the f function applied with some default arguments. Hence the value of c.result is 3, in this example.
The variable c however also has some additional functions, like
c.override which can be used to override the
default arguments. In this example the value of
(c.override { a = 4; }).result is 6.