nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, fetchurl, openssl, python, zlib, libuv, util-linux, http-parser
2, pkg-config, which, buildPackages
3# for `.pkgs` attribute
4, callPackage
5# Updater dependencies
6, writeScript, coreutils, gnugrep, jq, curl, common-updater-scripts, nix, runtimeShell
7, gnupg
8, darwin, xcbuild
9, procps, icu
10}:
11
12with lib;
13
14{ enableNpm ? true, version, sha256, patches ? [] } @args:
15
16let
17 inherit (darwin.apple_sdk.frameworks) CoreServices ApplicationServices;
18
19 majorVersion = versions.major version;
20 minorVersion = versions.minor version;
21
22 pname = if enableNpm then "nodejs" else "nodejs-slim";
23
24 useSharedHttpParser = !stdenv.isDarwin && versionOlder "${majorVersion}.${minorVersion}" "11.4";
25
26 sharedLibDeps = { inherit openssl zlib libuv; } // (optionalAttrs useSharedHttpParser { inherit http-parser; });
27
28 sharedConfigureFlags = concatMap (name: [
29 "--shared-${name}"
30 "--shared-${name}-libpath=${getLib sharedLibDeps.${name}}/lib"
31 /** Closure notes: we explicitly avoid specifying --shared-*-includes,
32 * as that would put the paths into bin/nodejs.
33 * Including pkg-config in build inputs would also have the same effect!
34 */
35 ]) (builtins.attrNames sharedLibDeps) ++ [
36 "--with-intl=system-icu"
37 ];
38
39 copyLibHeaders =
40 map
41 (name: "${getDev sharedLibDeps.${name}}/include/*")
42 (builtins.attrNames sharedLibDeps);
43
44 extraConfigFlags = optionals (!enableNpm) [ "--without-npm" ];
45 self = stdenv.mkDerivation {
46 inherit pname version;
47
48 src = fetchurl {
49 url = "https://nodejs.org/dist/v${version}/node-v${version}.tar.xz";
50 inherit sha256;
51 };
52
53 CC_host = "cc";
54 CXX_host = "c++";
55 depsBuildBuild = [ buildPackages.stdenv.cc openssl libuv zlib ];
56
57 buildInputs = optionals stdenv.isDarwin [ CoreServices ApplicationServices ]
58 ++ [ zlib libuv openssl http-parser icu ];
59
60 nativeBuildInputs = [ which pkg-config python ]
61 ++ optionals stdenv.isDarwin [ xcbuild ];
62
63 outputs = [ "out" "libv8" ];
64 setOutputFlags = false;
65 moveToDev = false;
66
67 configureFlags = let
68 isCross = stdenv.hostPlatform != stdenv.buildPlatform;
69 inherit (stdenv.hostPlatform) gcc isAarch32;
70 in sharedConfigureFlags ++ [
71 "--without-dtrace"
72 ] ++ (optionals isCross [
73 "--cross-compiling"
74 "--without-intl"
75 "--without-snapshot"
76 "--dest-cpu=${let platform = stdenv.hostPlatform; in
77 if platform.isAarch32 then "arm"
78 else if platform.isAarch64 then "arm64"
79 else if platform.isMips32 && platform.isLittleEndian then "mipsel"
80 else if platform.isMips32 && !platform.isLittleEndian then "mips"
81 else if platform.isMips64 && platform.isLittleEndian then "mips64el"
82 else if platform.isPower && platform.is32bit then "ppc"
83 else if platform.isPower && platform.is64bit then "ppc64"
84 else if platform.isx86_64 then "x86_64"
85 else if platform.isx86_32 then "x86"
86 else if platform.isS390 && platform.is64bit then "s390x"
87 else if platform.isRiscV && platform.is64bit then "riscv64"
88 else throw "unsupported cpu ${stdenv.hostPlatform.uname.processor}"}"
89 ]) ++ (optionals (isCross && isAarch32 && hasAttr "fpu" gcc) [
90 "--with-arm-fpu=${gcc.fpu}"
91 ]) ++ (optionals (isCross && isAarch32 && hasAttr "float-abi" gcc) [
92 "--with-arm-float-abi=${gcc.float-abi}"
93 ]) ++ extraConfigFlags;
94
95 configurePlatforms = [];
96
97 dontDisableStatic = true;
98
99 enableParallelBuilding = true;
100
101 passthru.interpreterName = "nodejs";
102
103 passthru.pkgs = callPackage ../../node-packages/default.nix {
104 nodejs = self;
105 };
106
107 setupHook = ./setup-hook.sh;
108
109 pos = builtins.unsafeGetAttrPos "version" args;
110
111 inherit patches;
112
113 postPatch = ''
114 patchShebangs .
115
116 # fix tests
117 for a in test/parallel/test-child-process-env.js \
118 test/parallel/test-child-process-exec-env.js \
119 test/parallel/test-child-process-default-options.js \
120 test/fixtures/syntax/good_syntax_shebang.js \
121 test/fixtures/syntax/bad_syntax_shebang.js ; do
122 substituteInPlace $a \
123 --replace "/usr/bin/env" "${coreutils}/bin/env"
124 done
125 '' + optionalString stdenv.isDarwin ''
126 sed -i 's/raise.*No Xcode or CLT version detected.*/version = "7.0.0"/' tools/gyp/pylib/gyp/xcode_emulation.py
127 sed -i -e "s|tr1/type_traits|type_traits|g" \
128 -e "s|std::tr1|std|" src/util.h
129 '';
130
131 checkInputs = [ procps ];
132 doCheck = false; # fails 4 out of 1453 tests
133
134 postInstall = ''
135 PATH=$out/bin:$PATH patchShebangs $out
136
137 ${optionalString (enableNpm && stdenv.hostPlatform == stdenv.buildPlatform) ''
138 mkdir -p $out/share/bash-completion/completions/
139 HOME=$TMPDIR $out/bin/npm completion > $out/share/bash-completion/completions/npm
140 for dir in "$out/lib/node_modules/npm/man/"*; do
141 mkdir -p $out/share/man/$(basename "$dir")
142 for page in "$dir"/*; do
143 ln -rs $page $out/share/man/$(basename "$dir")
144 done
145 done
146 ''}
147
148 # install the missing headers for node-gyp
149 cp -r ${concatStringsSep " " copyLibHeaders} $out/include/node
150
151 # assemble a static v8 library and put it in the 'libv8' output
152 mkdir -p $libv8/lib
153 pushd out/Release/obj.target
154 find . -path "./torque_*/**/*.o" -or -path "./v8*/**/*.o" | sort -u >files
155 ${if stdenv.buildPlatform.isGnu then ''
156 ar -cqs $libv8/lib/libv8.a @files
157 '' else ''
158 cat files | while read -r file; do
159 ar -cqS $libv8/lib/libv8.a $file
160 done
161 ''}
162 popd
163
164 # copy v8 headers
165 cp -r deps/v8/include $libv8/
166
167 # create a pkgconfig file for v8
168 major=$(grep V8_MAJOR_VERSION deps/v8/include/v8-version.h | cut -d ' ' -f 3)
169 minor=$(grep V8_MINOR_VERSION deps/v8/include/v8-version.h | cut -d ' ' -f 3)
170 patch=$(grep V8_PATCH_LEVEL deps/v8/include/v8-version.h | cut -d ' ' -f 3)
171 mkdir -p $libv8/lib/pkgconfig
172 cat > $libv8/lib/pkgconfig/v8.pc << EOF
173 Name: v8
174 Description: V8 JavaScript Engine
175 Version: $major.$minor.$patch
176 Libs: -L$libv8/lib -lv8 -pthread -licui18n
177 Cflags: -I$libv8/include
178 EOF
179 '' + optionalString (stdenv.isDarwin && enableNpm) ''
180 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
181 '';
182
183 passthru.updateScript = import ./update.nix {
184 inherit writeScript coreutils gnugrep jq curl common-updater-scripts gnupg nix runtimeShell;
185 inherit lib;
186 inherit majorVersion;
187 };
188
189 meta = {
190 description = "Event-driven I/O framework for the V8 JavaScript engine";
191 homepage = "https://nodejs.org";
192 changelog = "https://github.com/nodejs/node/releases/tag/v${version}";
193 license = licenses.mit;
194 maintainers = with maintainers; [ goibhniu gilligan cko marsam ];
195 platforms = platforms.linux ++ platforms.darwin;
196 mainProgram = "node";
197 knownVulnerabilities = optional (versionOlder version "14") "This NodeJS release has reached its end of life. See https://nodejs.org/en/about/releases/.";
198 };
199
200 passthru.python = python; # to ensure nodeEnv uses the same version
201 };
202in self