2021-07-01 13:36:41 +02:00
|
|
|
# Customising Packages {#sec-customising-packages}
|
|
|
|
|
2024-04-19 16:06:32 +02:00
|
|
|
The Nixpkgs configuration for a NixOS system is set by the {option}`nixpkgs.config` option.
|
|
|
|
|
|
|
|
::::{.example}
|
|
|
|
# Globally allow unfree packages
|
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
nixpkgs.config = {
|
|
|
|
allowUnfree = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
:::{.note}
|
|
|
|
This only allows unfree software in the given NixOS configuration.
|
|
|
|
For users invoking Nix commands such as [`nix-build`](https://nixos.org/manual/nix/stable/command-ref/nix-build), Nixpkgs is configured independently.
|
|
|
|
See the [Nixpkgs manual section on global configuration](https://nixos.org/manual/nixpkgs/unstable/#chap-packageconfig) for details.
|
|
|
|
:::
|
|
|
|
::::
|
|
|
|
|
|
|
|
<!-- TODO(@fricklerhandwerk)
|
|
|
|
all of the following should go to the Nixpkgs manual, it has nothing to do with NixOS
|
|
|
|
-->
|
|
|
|
|
|
|
|
Some packages in Nixpkgs have options to enable or disable optional functionality, or change other aspects of the package.
|
2021-07-01 13:36:41 +02:00
|
|
|
|
|
|
|
::: {.warning}
|
2024-04-19 16:06:32 +02:00
|
|
|
Unfortunately, Nixpkgs currently lacks a way to query available package configuration options.
|
2021-07-01 13:36:41 +02:00
|
|
|
:::
|
|
|
|
|
2023-06-06 22:37:42 +02:00
|
|
|
::: {.note}
|
2023-09-26 17:00:45 +02:00
|
|
|
For example, many packages come with extensions one might add.
|
2023-06-06 22:37:42 +02:00
|
|
|
Examples include:
|
|
|
|
- [`passExtensions.pass-otp`](https://search.nixos.org/packages/query=passExtensions.pass-otp)
|
|
|
|
- [`python310Packages.requests`](https://search.nixos.org/packages/query=python310Packages.requests)
|
|
|
|
|
|
|
|
You can use them like this:
|
|
|
|
```nix
|
2024-03-27 19:10:27 +01:00
|
|
|
{
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
sl
|
|
|
|
(pass.withExtensions (subpkgs: with subpkgs; [
|
|
|
|
pass-audit
|
|
|
|
pass-otp
|
|
|
|
pass-genphrase
|
|
|
|
]))
|
|
|
|
(python3.withPackages (subpkgs: with subpkgs; [
|
|
|
|
requests
|
|
|
|
]))
|
|
|
|
cowsay
|
|
|
|
];
|
|
|
|
}
|
2023-06-06 22:37:42 +02:00
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
2021-07-01 13:36:41 +02:00
|
|
|
Apart from high-level options, it's possible to tweak a package in
|
|
|
|
almost arbitrary ways, such as changing or disabling dependencies of a
|
|
|
|
package. For instance, the Emacs package in Nixpkgs by default has a
|
|
|
|
dependency on GTK 2. If you want to build it against GTK 3, you can
|
|
|
|
specify that as follows:
|
|
|
|
|
|
|
|
```nix
|
2024-03-27 19:10:27 +01:00
|
|
|
{
|
|
|
|
environment.systemPackages = [ (pkgs.emacs.override { gtk = pkgs.gtk3; }) ];
|
|
|
|
}
|
2021-07-01 13:36:41 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
The function `override` performs the call to the Nix function that
|
|
|
|
produces Emacs, with the original arguments amended by the set of
|
|
|
|
arguments specified by you. So here the function argument `gtk` gets the
|
|
|
|
value `pkgs.gtk3`, causing Emacs to depend on GTK 3. (The parentheses
|
|
|
|
are necessary because in Nix, function application binds more weakly
|
|
|
|
than list construction, so without them,
|
2021-07-04 04:12:05 +02:00
|
|
|
[](#opt-environment.systemPackages)
|
2021-07-01 13:36:41 +02:00
|
|
|
would be a list with two elements.)
|
|
|
|
|
|
|
|
Even greater customisation is possible using the function
|
|
|
|
`overrideAttrs`. While the `override` mechanism above overrides the
|
|
|
|
arguments of a package function, `overrideAttrs` allows changing the
|
|
|
|
*attributes* passed to `mkDerivation`. This permits changing any aspect
|
|
|
|
of the package, such as the source code. For instance, if you want to
|
|
|
|
override the source code of Emacs, you can say:
|
|
|
|
|
|
|
|
```nix
|
2024-03-27 19:10:27 +01:00
|
|
|
{
|
|
|
|
environment.systemPackages = [
|
|
|
|
(pkgs.emacs.overrideAttrs (oldAttrs: {
|
|
|
|
name = "emacs-25.0-pre";
|
|
|
|
src = /path/to/my/emacs/tree;
|
|
|
|
}))
|
|
|
|
];
|
|
|
|
}
|
2021-07-01 13:36:41 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Here, `overrideAttrs` takes the Nix derivation specified by `pkgs.emacs`
|
|
|
|
and produces a new derivation in which the original's `name` and `src`
|
|
|
|
attribute have been replaced by the given values by re-calling
|
|
|
|
`stdenv.mkDerivation`. The original attributes are accessible via the
|
|
|
|
function argument, which is conventionally named `oldAttrs`.
|
|
|
|
|
|
|
|
The overrides shown above are not global. They do not affect the
|
|
|
|
original package; other packages in Nixpkgs continue to depend on the
|
|
|
|
original rather than the customised package. This means that if another
|
|
|
|
package in your system depends on the original package, you end up with
|
|
|
|
two instances of the package. If you want to have everything depend on
|
|
|
|
your customised instance, you can apply a *global* override as follows:
|
|
|
|
|
|
|
|
```nix
|
2024-03-27 19:10:27 +01:00
|
|
|
{
|
|
|
|
nixpkgs.config.packageOverrides = pkgs:
|
|
|
|
{ emacs = pkgs.emacs.override { gtk = pkgs.gtk3; };
|
|
|
|
};
|
|
|
|
}
|
2021-07-01 13:36:41 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
The effect of this definition is essentially equivalent to modifying the
|
|
|
|
`emacs` attribute in the Nixpkgs source tree. Any package in Nixpkgs
|
|
|
|
that depends on `emacs` will be passed your customised instance.
|
|
|
|
(However, the value `pkgs.emacs` in `nixpkgs.config.packageOverrides`
|
|
|
|
refers to the original rather than overridden instance, to prevent an
|
|
|
|
infinite recursion.)
|