2021-09-18 10:52:07 +00:00
|
|
|
# Adding Custom Packages {#sec-custom-packages}
|
|
|
|
|
|
|
|
It's possible that a package you need is not available in NixOS. In that
|
2022-07-14 12:49:19 +00:00
|
|
|
case, you can do two things. Either you can package it with Nix, or you can try
|
|
|
|
to use prebuilt packages from upstream. Due to the peculiarities of NixOS, it
|
|
|
|
is important to note that building software from source is often easier than
|
|
|
|
using pre-built executables.
|
|
|
|
|
|
|
|
## Building with Nix {#sec-custom-packages-nix}
|
|
|
|
|
|
|
|
This can be done either in-tree or out-of-tree. For an in-tree build, you can
|
|
|
|
clone the Nixpkgs repository, add the package to your clone, and (optionally)
|
|
|
|
submit a patch or pull request to have it accepted into the main Nixpkgs
|
|
|
|
repository. This is described in detail in the [Nixpkgs
|
|
|
|
manual](https://nixos.org/nixpkgs/manual). In short, you clone Nixpkgs:
|
2021-09-18 10:52:07 +00:00
|
|
|
|
|
|
|
```ShellSession
|
|
|
|
$ git clone https://github.com/NixOS/nixpkgs
|
|
|
|
$ cd nixpkgs
|
|
|
|
```
|
|
|
|
|
|
|
|
Then you write and test the package as described in the Nixpkgs manual.
|
|
|
|
Finally, you add it to [](#opt-environment.systemPackages), e.g.
|
|
|
|
|
|
|
|
```nix
|
2024-04-21 15:54:59 +00:00
|
|
|
{
|
|
|
|
environment.systemPackages = [ pkgs.my-package ];
|
|
|
|
}
|
2021-09-18 10:52:07 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
and you run `nixos-rebuild`, specifying your own Nixpkgs tree:
|
|
|
|
|
|
|
|
```ShellSession
|
|
|
|
# nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
|
|
|
|
```
|
|
|
|
|
|
|
|
The second possibility is to add the package outside of the Nixpkgs
|
|
|
|
tree. For instance, here is how you specify a build of the
|
|
|
|
[GNU Hello](https://www.gnu.org/software/hello/) package directly in
|
|
|
|
`configuration.nix`:
|
|
|
|
|
|
|
|
```nix
|
2024-04-21 15:54:59 +00:00
|
|
|
{
|
|
|
|
environment.systemPackages =
|
|
|
|
let
|
|
|
|
my-hello = with pkgs; stdenv.mkDerivation rec {
|
|
|
|
name = "hello-2.8";
|
|
|
|
src = fetchurl {
|
|
|
|
url = "mirror://gnu/hello/${name}.tar.gz";
|
|
|
|
hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM=";
|
|
|
|
};
|
2021-09-18 10:52:07 +00:00
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
in
|
|
|
|
[ my-hello ];
|
|
|
|
}
|
2021-09-18 10:52:07 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Of course, you can also move the definition of `my-hello` into a
|
|
|
|
separate Nix expression, e.g.
|
|
|
|
|
|
|
|
```nix
|
2024-04-21 15:54:59 +00:00
|
|
|
{
|
|
|
|
environment.systemPackages = [ (import ./my-hello.nix) ];
|
|
|
|
}
|
2021-09-18 10:52:07 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
where `my-hello.nix` contains:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
with import <nixpkgs> {}; # bring all of Nixpkgs into scope
|
|
|
|
|
|
|
|
stdenv.mkDerivation rec {
|
|
|
|
name = "hello-2.8";
|
|
|
|
src = fetchurl {
|
|
|
|
url = "mirror://gnu/hello/${name}.tar.gz";
|
2023-10-09 19:29:22 +00:00
|
|
|
hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM=";
|
2021-09-18 10:52:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This allows testing the package easily:
|
|
|
|
|
|
|
|
```ShellSession
|
|
|
|
$ nix-build my-hello.nix
|
|
|
|
$ ./result/bin/hello
|
|
|
|
Hello, world!
|
|
|
|
```
|
2022-07-14 12:49:19 +00:00
|
|
|
|
|
|
|
## Using pre-built executables {#sec-custom-packages-prebuilt}
|
|
|
|
|
|
|
|
Most pre-built executables will not work on NixOS. There are two notable
|
|
|
|
exceptions: flatpaks and AppImages. For flatpaks see the [dedicated
|
|
|
|
section](#module-services-flatpak). AppImages will not run "as-is" on NixOS.
|
|
|
|
First you need to install `appimage-run`: add to `/etc/nixos/configuration.nix`
|
|
|
|
|
|
|
|
```nix
|
2024-04-21 15:54:59 +00:00
|
|
|
{
|
|
|
|
environment.systemPackages = [ pkgs.appimage-run ];
|
|
|
|
}
|
2022-07-14 12:49:19 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Then instead of running the AppImage "as-is", run `appimage-run foo.appimage`.
|
|
|
|
|
|
|
|
To make other pre-built executables work on NixOS, you need to package them
|
2023-04-29 16:46:19 +00:00
|
|
|
with Nix and special helpers like `autoPatchelfHook` or `buildFHSEnv`. See
|
2022-07-14 12:49:19 +00:00
|
|
|
the [Nixpkgs manual](https://nixos.org/nixpkgs/manual) for details. This
|
|
|
|
is complex and often doing a source build is easier.
|