1# Customising Packages {#sec-customising-packages}
2
3The Nixpkgs configuration for a NixOS system is set by the {option}`nixpkgs.config` option.
4
5::::{.example}
6# Globally allow unfree packages
7
8```nix
9{
10 nixpkgs.config = {
11 allowUnfree = true;
12 };
13}
14```
15
16:::{.note}
17This only allows unfree software in the given NixOS configuration.
18For users invoking Nix commands such as [`nix-build`](https://nixos.org/manual/nix/stable/command-ref/nix-build), Nixpkgs is configured independently.
19See the [Nixpkgs manual section on global configuration](https://nixos.org/manual/nixpkgs/unstable/#chap-packageconfig) for details.
20:::
21::::
22
23<!-- TODO(@fricklerhandwerk)
24all of the following should go to the Nixpkgs manual, it has nothing to do with NixOS
25-->
26
27Some packages in Nixpkgs have options to enable or disable optional functionality, or change other aspects of the package.
28
29::: {.warning}
30Unfortunately, Nixpkgs currently lacks a way to query available package configuration options.
31:::
32
33::: {.note}
34For example, many packages come with extensions one might add.
35Examples include:
36- [`passExtensions.pass-otp`](https://search.nixos.org/packages?query=passExtensions.pass-otp)
37- [`python312Packages.requests`](https://search.nixos.org/packages?query=python312Packages.requests)
38
39You can use them like this:
40```nix
41{
42 environment.systemPackages = with pkgs; [
43 sl
44 (pass.withExtensions (
45 subpkgs: with subpkgs; [
46 pass-audit
47 pass-otp
48 pass-genphrase
49 ]
50 ))
51 (python3.withPackages (subpkgs: with subpkgs; [ requests ]))
52 cowsay
53 ];
54}
55```
56:::
57
58Apart from high-level options, it's possible to tweak a package in
59almost arbitrary ways, such as changing or disabling dependencies of a
60package. For instance, the Emacs package in Nixpkgs by default has a
61dependency on GTK 2. If you want to build it against GTK 3, you can
62specify that as follows:
63
64```nix
65{ environment.systemPackages = [ (pkgs.emacs.override { gtk = pkgs.gtk3; }) ]; }
66```
67
68The function `override` performs the call to the Nix function that
69produces Emacs, with the original arguments amended by the set of
70arguments specified by you. So here the function argument `gtk` gets the
71value `pkgs.gtk3`, causing Emacs to depend on GTK 3. (The parentheses
72are necessary because in Nix, function application binds more weakly
73than list construction, so without them,
74[](#opt-environment.systemPackages)
75would be a list with two elements.)
76
77Even greater customisation is possible using the function
78`overrideAttrs`. While the `override` mechanism above overrides the
79arguments of a package function, `overrideAttrs` allows changing the
80*attributes* passed to `mkDerivation`. This permits changing any aspect
81of the package, such as the source code. For instance, if you want to
82override the source code of Emacs, you can say:
83
84```nix
85{
86 environment.systemPackages = [
87 (pkgs.emacs.overrideAttrs (oldAttrs: {
88 name = "emacs-25.0-pre";
89 src = /path/to/my/emacs/tree;
90 }))
91 ];
92}
93```
94
95Here, `overrideAttrs` takes the Nix derivation specified by `pkgs.emacs`
96and produces a new derivation in which the original's `name` and `src`
97attribute have been replaced by the given values by re-calling
98`stdenv.mkDerivation`. The original attributes are accessible via the
99function argument, which is conventionally named `oldAttrs`.
100
101The overrides shown above are not global. They do not affect the
102original package; other packages in Nixpkgs continue to depend on the
103original rather than the customised package. This means that if another
104package in your system depends on the original package, you end up with
105two instances of the package. If you want to have everything depend on
106your customised instance, you can apply a *global* override as follows:
107
108```nix
109{
110 nixpkgs.config.packageOverrides = pkgs: {
111 emacs = pkgs.emacs.override { gtk = pkgs.gtk3; };
112 };
113}
114```
115
116The effect of this definition is essentially equivalent to modifying the
117`emacs` attribute in the Nixpkgs source tree. Any package in Nixpkgs
118that depends on `emacs` will be passed your customised instance.
119(However, the value `pkgs.emacs` in `nixpkgs.config.packageOverrides`
120refers to the original rather than overridden instance, to prevent an
121infinite recursion.)