1 # Adding Custom Packages {#sec-custom-packages}
3 It's possible that a package you need is not available in NixOS. In that
4 case, you can do two things. Either you can package it with Nix, or you can try
5 to use prebuilt packages from upstream. Due to the peculiarities of NixOS, it
6 is important to note that building software from source is often easier than
7 using pre-built executables.
9 ## Building with Nix {#sec-custom-packages-nix}
11 This can be done either in-tree or out-of-tree. For an in-tree build, you can
12 clone the Nixpkgs repository, add the package to your clone, and (optionally)
13 submit a patch or pull request to have it accepted into the main Nixpkgs
14 repository. This is described in detail in the [Nixpkgs
15 manual](https://nixos.org/nixpkgs/manual). In short, you clone Nixpkgs:
18 $ git clone https://github.com/NixOS/nixpkgs
22 Then you write and test the package as described in the Nixpkgs manual.
23 Finally, you add it to [](#opt-environment.systemPackages), e.g.
27 environment.systemPackages = [ pkgs.my-package ];
31 and you run `nixos-rebuild`, specifying your own Nixpkgs tree:
34 # nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
37 The second possibility is to add the package outside of the Nixpkgs
38 tree. For instance, here is how you specify a build of the
39 [GNU Hello](https://www.gnu.org/software/hello/) package directly in
44 environment.systemPackages =
46 my-hello = with pkgs; stdenv.mkDerivation rec {
49 url = "mirror://gnu/hello/${name}.tar.gz";
50 hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM=";
58 Of course, you can also move the definition of `my-hello` into a
59 separate Nix expression, e.g.
63 environment.systemPackages = [ (import ./my-hello.nix) ];
67 where `my-hello.nix` contains:
70 with import <nixpkgs> {}; # bring all of Nixpkgs into scope
72 stdenv.mkDerivation rec {
75 url = "mirror://gnu/hello/${name}.tar.gz";
76 hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM=";
81 This allows testing the package easily:
84 $ nix-build my-hello.nix
89 ## Using pre-built executables {#sec-custom-packages-prebuilt}
91 Most pre-built executables will not work on NixOS. There are two notable
92 exceptions: flatpaks and AppImages. For flatpaks see the [dedicated
93 section](#module-services-flatpak). AppImages will not run "as-is" on NixOS.
94 First you need to install `appimage-run`: add to `/etc/nixos/configuration.nix`
98 environment.systemPackages = [ pkgs.appimage-run ];
102 Then instead of running the AppImage "as-is", run `appimage-run foo.appimage`.
104 To make other pre-built executables work on NixOS, you need to package them
105 with Nix and special helpers like `autoPatchelfHook` or `buildFHSEnv`. See
106 the [Nixpkgs manual](https://nixos.org/nixpkgs/manual) for details. This
107 is complex and often doing a source build is easier.