1# Adding Custom Packages {#sec-custom-packages}
2
3It's possible that a package you need is not available in NixOS. In that
4case, you can do two things. Either you can package it with Nix, or you can try
5to use prebuilt packages from upstream. Due to the peculiarities of NixOS, it
6is important to note that building software from source is often easier than
7using pre-built executables.
8
9## Building with Nix {#sec-custom-packages-nix}
10
11This can be done either in-tree or out-of-tree. For an in-tree build, you can
12clone the Nixpkgs repository, add the package to your clone, and (optionally)
13submit a patch or pull request to have it accepted into the main Nixpkgs
14repository. This is described in detail in the [Nixpkgs
15manual](https://nixos.org/nixpkgs/manual). In short, you clone Nixpkgs:
16
17```ShellSession
18$ git clone https://github.com/NixOS/nixpkgs
19$ cd nixpkgs
20```
21
22Then you write and test the package as described in the Nixpkgs manual.
23Finally, you add it to [](#opt-environment.systemPackages), e.g.
24
25```nix
26environment.systemPackages = [ pkgs.my-package ];
27```
28
29and you run `nixos-rebuild`, specifying your own Nixpkgs tree:
30
31```ShellSession
32# nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
33```
34
35The second possibility is to add the package outside of the Nixpkgs
36tree. For instance, here is how you specify a build of the
37[GNU Hello](https://www.gnu.org/software/hello/) package directly in
38`configuration.nix`:
39
40```nix
41environment.systemPackages =
42 let
43 my-hello = with pkgs; stdenv.mkDerivation rec {
44 name = "hello-2.8";
45 src = fetchurl {
46 url = "mirror://gnu/hello/${name}.tar.gz";
47 sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
48 };
49 };
50 in
51 [ my-hello ];
52```
53
54Of course, you can also move the definition of `my-hello` into a
55separate Nix expression, e.g.
56
57```nix
58environment.systemPackages = [ (import ./my-hello.nix) ];
59```
60
61where `my-hello.nix` contains:
62
63```nix
64with import <nixpkgs> {}; # bring all of Nixpkgs into scope
65
66stdenv.mkDerivation rec {
67 name = "hello-2.8";
68 src = fetchurl {
69 url = "mirror://gnu/hello/${name}.tar.gz";
70 sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
71 };
72}
73```
74
75This allows testing the package easily:
76
77```ShellSession
78$ nix-build my-hello.nix
79$ ./result/bin/hello
80Hello, world!
81```
82
83## Using pre-built executables {#sec-custom-packages-prebuilt}
84
85Most pre-built executables will not work on NixOS. There are two notable
86exceptions: flatpaks and AppImages. For flatpaks see the [dedicated
87section](#module-services-flatpak). AppImages will not run "as-is" on NixOS.
88First you need to install `appimage-run`: add to `/etc/nixos/configuration.nix`
89
90```nix
91environment.systemPackages = [ pkgs.appimage-run ];
92```
93
94Then instead of running the AppImage "as-is", run `appimage-run foo.appimage`.
95
96To make other pre-built executables work on NixOS, you need to package them
97with Nix and special helpers like `autoPatchelfHook` or `buildFHSEnv`. See
98the [Nixpkgs manual](https://nixos.org/nixpkgs/manual) for details. This
99is complex and often doing a source build is easier.