core/pkgs/build-support/node/build-npm-package/default.nix

104 lines
2.9 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +00:00
{
lib,
stdenv,
fetchNpmDeps,
buildPackages,
nodejs,
darwin,
}@topLevelArgs:
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
{
name ? "${args.pname}-${args.version}",
src ? null,
srcs ? null,
sourceRoot ? null,
prePatch ? "",
patches ? [ ],
postPatch ? "",
nativeBuildInputs ? [ ],
buildInputs ? [ ],
2024-05-02 00:46:19 +00:00
# The output hash of the dependencies for this project.
# Can be calculated in advance with prefetch-npm-deps.
2024-06-30 08:16:52 +00:00
npmDepsHash ? "",
2024-05-02 00:46:19 +00:00
# Whether to force the usage of Git dependencies that have install scripts, but not a lockfile.
# Use with care.
2024-06-30 08:16:52 +00:00
forceGitDeps ? false,
2024-05-02 00:46:19 +00:00
# Whether to force allow an empty dependency cache.
# This can be enabled if there are truly no remote dependencies, but generally an empty cache indicates something is wrong.
2024-06-30 08:16:52 +00:00
forceEmptyCache ? false,
2024-05-02 00:46:19 +00:00
# Whether to make the cache writable prior to installing dependencies.
# Don't set this unless npm tries to write to the cache directory, as it can slow down the build.
2024-06-30 08:16:52 +00:00
makeCacheWritable ? false,
2024-05-02 00:46:19 +00:00
# The script to run to build the project.
2024-06-30 08:16:52 +00:00
npmBuildScript ? "build",
2024-05-02 00:46:19 +00:00
# Flags to pass to all npm commands.
2024-06-30 08:16:52 +00:00
npmFlags ? [ ],
2024-05-02 00:46:19 +00:00
# Flags to pass to `npm ci`.
2024-06-30 08:16:52 +00:00
npmInstallFlags ? [ ],
2024-05-02 00:46:19 +00:00
# Flags to pass to `npm rebuild`.
2024-06-30 08:16:52 +00:00
npmRebuildFlags ? [ ],
2024-05-02 00:46:19 +00:00
# Flags to pass to `npm run ${npmBuildScript}`.
2024-06-30 08:16:52 +00:00
npmBuildFlags ? [ ],
2024-05-02 00:46:19 +00:00
# Flags to pass to `npm pack`.
2024-06-30 08:16:52 +00:00
npmPackFlags ? [ ],
2024-05-02 00:46:19 +00:00
# Flags to pass to `npm prune`.
2024-06-30 08:16:52 +00:00
npmPruneFlags ? npmInstallFlags,
2024-05-02 00:46:19 +00:00
# Value for npm `--workspace` flag and directory in which the files to be installed are found.
2024-06-30 08:16:52 +00:00
npmWorkspace ? null,
nodejs ? topLevelArgs.nodejs,
npmDeps ? fetchNpmDeps {
inherit
forceGitDeps
forceEmptyCache
src
srcs
sourceRoot
prePatch
patches
postPatch
;
name = "${name}-npm-deps";
hash = npmDepsHash;
},
2024-05-02 00:46:19 +00:00
# Custom npmConfigHook
2024-06-30 08:16:52 +00:00
npmConfigHook ? null,
2024-05-02 00:46:19 +00:00
# Custom npmBuildHook
2024-06-30 08:16:52 +00:00
npmBuildHook ? null,
2024-05-02 00:46:19 +00:00
# Custom npmInstallHook
2024-06-30 08:16:52 +00:00
npmInstallHook ? null,
...
}@args:
2024-05-02 00:46:19 +00:00
let
# .override {} negates splicing, so we need to use buildPackages explicitly
2024-06-30 08:16:52 +00:00
npmHooks = buildPackages.npmHooks.override { inherit nodejs; };
2024-05-02 00:46:19 +00:00
in
2024-06-30 08:16:52 +00:00
stdenv.mkDerivation (
args
// {
inherit npmDeps npmBuildScript;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
nativeBuildInputs =
nativeBuildInputs
++ [
nodejs
# Prefer passed hooks
(if npmConfigHook != null then npmConfigHook else npmHooks.npmConfigHook)
(if npmBuildHook != null then npmBuildHook else npmHooks.npmBuildHook)
(if npmInstallHook != null then npmInstallHook else npmHooks.npmInstallHook)
nodejs.python
]
++ lib.optionals stdenv.isDarwin [ darwin.cctools ];
buildInputs = buildInputs ++ [ nodejs ];
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
strictDeps = true;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Stripping takes way too long with the amount of files required by a typical Node.js project.
dontStrip = args.dontStrip or true;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
meta = (args.meta or { }) // {
platforms = args.meta.platforms or nodejs.meta.platforms;
};
}
)