nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 fetchNpmDeps,
5 buildPackages,
6 nodejs,
7 cctools,
8}@topLevelArgs:
9
10lib.extendMkDerivation {
11 constructDrv = stdenv.mkDerivation;
12
13 extendDrvArgs =
14 finalAttrs:
15 {
16 name ? "${args.pname}-${args.version}",
17 src ? null,
18 srcs ? null,
19 sourceRoot ? null,
20 prePatch ? "",
21 patches ? [ ],
22 postPatch ? "",
23 patchFlags ? [ ],
24 nativeBuildInputs ? [ ],
25 buildInputs ? [ ],
26 # The output hash of the dependencies for this project.
27 # Can be calculated in advance with prefetch-npm-deps.
28 npmDepsHash ? "",
29 # Fetcher format version for npmDeps. Set to 2 to enable packument caching
30 # for workspace support. Changing this will invalidate npmDepsHash.
31 npmDepsFetcherVersion ? 1,
32 # Whether to force the usage of Git dependencies that have install scripts, but not a lockfile.
33 # Use with care.
34 forceGitDeps ? false,
35 # Whether to force allow an empty dependency cache.
36 # This can be enabled if there are truly no remote dependencies, but generally an empty cache indicates something is wrong.
37 forceEmptyCache ? false,
38 # Whether to make the cache writable prior to installing dependencies.
39 # Don't set this unless npm tries to write to the cache directory, as it can slow down the build.
40 makeCacheWritable ? false,
41 # The script to run to build the project.
42 npmBuildScript ? "build",
43 # Flags to pass to all npm commands.
44 npmFlags ? [ ],
45 # Flags to pass to `npm ci`.
46 npmInstallFlags ? [ ],
47 # Flags to pass to `npm rebuild`.
48 npmRebuildFlags ? [ ],
49 # Flags to pass to `npm run ${npmBuildScript}`.
50 npmBuildFlags ? [ ],
51 # Flags to pass to `npm pack`.
52 npmPackFlags ? [ ],
53 # Flags to pass to `npm prune`.
54 npmPruneFlags ? npmInstallFlags,
55 # Value for npm `--workspace` flag and directory in which the files to be installed are found.
56 npmWorkspace ? null,
57 nodejs ? topLevelArgs.nodejs,
58 npmDeps ? fetchNpmDeps {
59 inherit
60 forceGitDeps
61 forceEmptyCache
62 src
63 srcs
64 sourceRoot
65 prePatch
66 patches
67 postPatch
68 patchFlags
69 ;
70 name = "${name}-npm-deps";
71 hash = npmDepsHash;
72 fetcherVersion = npmDepsFetcherVersion;
73 },
74 # Custom npmConfigHook
75 npmConfigHook ? null,
76 # Custom npmBuildHook
77 npmBuildHook ? null,
78 # Custom npmInstallHook
79 npmInstallHook ? null,
80 ...
81 }@args:
82
83 let
84 # .override {} negates splicing, so we need to use buildPackages explicitly
85 npmHooks = buildPackages.npmHooks.override {
86 inherit nodejs;
87 };
88 in
89 {
90 inherit npmDeps npmBuildScript;
91
92 env = (args.env or { }) // {
93 NIX_NPM_FETCHER_VERSION = npmDepsFetcherVersion;
94 };
95
96 nativeBuildInputs =
97 nativeBuildInputs
98 ++ [
99 nodejs
100 # Prefer passed hooks
101 (if npmConfigHook != null then npmConfigHook else npmHooks.npmConfigHook)
102 (if npmBuildHook != null then npmBuildHook else npmHooks.npmBuildHook)
103 (if npmInstallHook != null then npmInstallHook else npmHooks.npmInstallHook)
104 nodejs.python
105 ]
106 ++ lib.optionals stdenv.hostPlatform.isDarwin [ cctools ];
107 buildInputs = buildInputs ++ [ nodejs ];
108
109 strictDeps = true;
110
111 # Stripping takes way too long with the amount of files required by a typical Node.js project.
112 dontStrip = args.dontStrip or true;
113
114 meta = (args.meta or { }) // {
115 platforms = args.meta.platforms or nodejs.meta.platforms;
116 };
117 };
118}