1{ stdenv, fetchurl, openssl, python2, zlib, libuv, utillinux, http-parser
2, pkgconfig, which
3# Updater dependencies
4, writeScript, coreutils, gnugrep, jq, curl, common-updater-scripts, nix, runtimeShell
5, gnupg
6, darwin, xcbuild
7, procps, icu
8}:
9
10with stdenv.lib;
11
12{ enableNpm ? true, version, sha256, patches ? [] } @args:
13
14let
15 inherit (darwin.apple_sdk.frameworks) CoreServices ApplicationServices;
16
17 majorVersion = versions.major version;
18 minorVersion = versions.minor version;
19
20 baseName = if enableNpm then "nodejs" else "nodejs-slim";
21
22 useSharedHttpParser = !stdenv.isDarwin && versionOlder "${majorVersion}.${minorVersion}" "11.4";
23
24 sharedLibDeps = { inherit openssl zlib libuv; } // (optionalAttrs useSharedHttpParser { inherit http-parser; });
25
26 sharedConfigureFlags = concatMap (name: [
27 "--shared-${name}"
28 "--shared-${name}-libpath=${getLib sharedLibDeps.${name}}/lib"
29 /** Closure notes: we explicitly avoid specifying --shared-*-includes,
30 * as that would put the paths into bin/nodejs.
31 * Including pkgconfig in build inputs would also have the same effect!
32 */
33 ]) (builtins.attrNames sharedLibDeps) ++ [
34 "--with-intl=system-icu"
35 ];
36
37 copyLibHeaders =
38 map
39 (name: "${getDev sharedLibDeps.${name}}/include/*")
40 (builtins.attrNames sharedLibDeps);
41
42 extraConfigFlags = optionals (!enableNpm) [ "--without-npm" ];
43in
44
45 stdenv.mkDerivation {
46 inherit version;
47
48 name = "${baseName}-${version}";
49
50 src = fetchurl {
51 url = "https://nodejs.org/dist/v${version}/node-v${version}.tar.xz";
52 inherit sha256;
53 };
54
55 buildInputs = optionals stdenv.isDarwin [ CoreServices ApplicationServices ]
56 ++ [ zlib libuv openssl http-parser icu ];
57
58 nativeBuildInputs = [ which utillinux pkgconfig python2 ]
59 ++ optionals stdenv.isDarwin [ xcbuild ];
60
61 configureFlags = let
62 isCross = stdenv.hostPlatform != stdenv.buildPlatform;
63 host = stdenv.hostPlatform.platform;
64 isAarch32 = stdenv.hostPlatform.isAarch32;
65 in sharedConfigureFlags ++ [
66 "--without-dtrace"
67 ] ++ (optionals isCross [
68 "--cross-compiling"
69 "--without-intl"
70 "--without-snapshot"
71 ]) ++ (optionals (isCross && isAarch32 && hasAttr "fpu" host.gcc) [
72 "--with-arm-fpu=${host.gcc.fpu}"
73 ]) ++ (optionals (isCross && isAarch32 && hasAttr "float-abi" host.gcc) [
74 "--with-arm-float-abi=${host.gcc.float-abi}"
75 ]) ++ (optionals (isCross && isAarch32) [
76 "--dest-cpu=arm"
77 ]) ++ extraConfigFlags;
78
79 configurePlatforms = [];
80
81 dontDisableStatic = true;
82
83 enableParallelBuilding = true;
84
85 passthru.interpreterName = "nodejs";
86
87 setupHook = ./setup-hook.sh;
88
89 pos = builtins.unsafeGetAttrPos "version" args;
90
91 inherit patches;
92
93 postPatch = ''
94 patchShebangs .
95
96 # fix tests
97 for a in test/parallel/test-child-process-env.js \
98 test/parallel/test-child-process-exec-env.js \
99 test/parallel/test-child-process-default-options.js \
100 test/fixtures/syntax/good_syntax_shebang.js \
101 test/fixtures/syntax/bad_syntax_shebang.js ; do
102 substituteInPlace $a \
103 --replace "/usr/bin/env" "${coreutils}/bin/env"
104 done
105 '' + optionalString stdenv.isDarwin ''
106 sed -i 's/raise.*No Xcode or CLT version detected.*/version = "7.0.0"/' tools/gyp/pylib/gyp/xcode_emulation.py
107 sed -i -e "s|tr1/type_traits|type_traits|g" \
108 -e "s|std::tr1|std|" src/util.h
109 '';
110
111 checkInputs = [ procps ];
112 doCheck = false; # fails 4 out of 1453 tests
113
114 postInstall = ''
115 PATH=$out/bin:$PATH patchShebangs $out
116
117 ${optionalString (enableNpm && stdenv.hostPlatform == stdenv.buildPlatform) ''
118 mkdir -p $out/share/bash-completion/completions/
119 $out/bin/npm completion > $out/share/bash-completion/completions/npm
120 for dir in "$out/lib/node_modules/npm/man/"*; do
121 mkdir -p $out/share/man/$(basename "$dir")
122 for page in "$dir"/*; do
123 ln -rs $page $out/share/man/$(basename "$dir")
124 done
125 done
126 ''}
127
128 # install the missing headers for node-gyp
129 cp -r ${concatStringsSep " " copyLibHeaders} $out/include/node
130 '' + optionalString (stdenv.isDarwin && enableNpm) ''
131 sed -i 's/raise.*No Xcode or CLT version detected.*/version = "7.0.0"/' $out/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py
132 '';
133
134 passthru.updateScript = import ./update.nix {
135 inherit writeScript coreutils gnugrep jq curl common-updater-scripts gnupg nix runtimeShell;
136 inherit (stdenv) lib;
137 inherit majorVersion;
138 };
139
140 meta = {
141 description = "Event-driven I/O framework for the V8 JavaScript engine";
142 homepage = https://nodejs.org;
143 license = licenses.mit;
144 maintainers = with maintainers; [ goibhniu gilligan cko ];
145 platforms = platforms.linux ++ platforms.darwin;
146 };
147
148 passthru.python = python2; # to ensure nodeEnv uses the same version
149}