Modifying Nix Packages

There are a lot of already written Nix package definitions covering the most commonly used applications in Linux and macOS. However, for some reason or another, from time to time, you may wish to install a package with a slightly customized definition.

The example below will install yarn without explicitely depending on node, i.e., it will use whatever node version you have available on the environment.

 1let pkgs = import <nixpkgs> {};
 2in pkgs.callPackage (
 3  { stdenv, fetchzip, makeWrapper }:
 4
 5  stdenv.mkDerivation rec {
 6    name = "yarn-${version}";
 7    version = "1.10.1";
 8
 9    src = fetchzip {
10      url = "https://github.com/yarnpkg/yarn/releases/download/v${version}/yarn-v${version}.tar.gz";
11      sha256 = "0j9y53k0wxgwjvvpbs6rr4nbydg3bn4khcxsrnzyrakym0ihgmr1";
12    };
13
14    buildInputs = [makeWrapper];
15
16    installPhase = ''
17      mkdir -p $out/{bin,libexec/yarn/}
18      cp -R . $out/libexec/yarn
19      makeWrapper $out/libexec/yarn/bin/yarn.js $out/bin/yarn
20    '';
21
22    meta = with stdenv.lib; {
23      homepage = https://yarnpkg.com/;
24      description = "Fast, reliable, and secure dependency management for javascript";
25      license = licenses.bsd2;
26      maintainers = [ maintainers.offline ];
27    };
28  }
29) {}

To install simply run nix-env -f yarn.nix -i.

More generally, when extending a package.nix, you can follow the template below and run with nix-env -f your_file.nix -i

1let pkgs = import <nixpkgs> {};
2in pkgs.callPackage (
3  # whatever is in the original package.nix
4) {}