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 26{ environment.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 41{ 42 environment.systemPackages = 43 let 44 my-hello = 45 with pkgs; 46 stdenv.mkDerivation rec { 47 name = "hello-2.8"; 48 src = fetchurl { 49 url = "mirror://gnu/hello/${name}.tar.gz"; 50 hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM="; 51 }; 52 }; 53 in 54 [ my-hello ]; 55} 56``` 57 58Of course, you can also move the definition of `my-hello` into a 59separate Nix expression, e.g. 60 61```nix 62{ environment.systemPackages = [ (import ./my-hello.nix) ]; } 63``` 64 65where `my-hello.nix` contains: 66 67```nix 68with import <nixpkgs> { }; # bring all of Nixpkgs into scope 69 70stdenv.mkDerivation rec { 71 name = "hello-2.8"; 72 src = fetchurl { 73 url = "mirror://gnu/hello/${name}.tar.gz"; 74 hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM="; 75 }; 76} 77``` 78 79This allows testing the package easily: 80 81```ShellSession 82$ nix-build my-hello.nix 83$ ./result/bin/hello 84Hello, world! 85``` 86 87## Using pre-built executables {#sec-custom-packages-prebuilt} 88 89Most pre-built executables will not work on NixOS. There are two notable 90exceptions: flatpaks and AppImages. For flatpaks see the [dedicated 91section](#module-services-flatpak). AppImages can run "as-is" on NixOS. 92 93First you need to enable AppImage support: add to `/etc/nixos/configuration.nix` 94 95```nix 96{ 97 programs.appimage.enable = true; 98 programs.appimage.binfmt = true; 99} 100``` 101 102Then you can run the AppImage "as-is" or with `appimage-run foo.appimage`. 103 104If there are shared libraries missing add them with 105 106```nix 107{ 108 programs.appimage.package = pkgs.appimage-run.override { 109 extraPkgs = pkgs: [ 110 # missing libraries here, e.g.: `pkgs.libepoxy` 111 ]; 112 }; 113} 114``` 115 116To make other pre-built executables work on NixOS, you need to package them 117with Nix and special helpers like `autoPatchelfHook` or `buildFHSEnv`. See 118the [Nixpkgs manual](https://nixos.org/nixpkgs/manual) for details. This 119is complex and often doing a source build is easier.