Skip to content

Commit

Permalink
Introduce preInstallCustomCommands attribute for node modules
Browse files Browse the repository at this point in the history
preInstallCustomCommands allows to run arbitrary shell scripts, running per node module pinned to given version - such approach, allows for fixing node modules which would otherwise be unfit for running on NixOS
  • Loading branch information
AleksanderGondek committed May 14, 2021
1 parent 7a321e2 commit 525b97c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ npmlock2nix.node_modules {
# };
# };
# You can run arbitrary shell operation for given module in given version,
# using preInstallCustomCommands attribute. Below you see how you can
# override path to esbuild module, depending on which version in needed.
# This approach comes in handy, if node_modules have two competing versions
# of the same module.
# Please note: set key must adhere to the pattern <module_name>@<module_version>.
# preInstallCustomCommands = {
# "[email protected]" = [''
# sed -i -e 's|process.env.ESBUILD_BINARY_PATH|"${esbuild_0_8_57}/bin/esbuild"|g' ./install.js
# ''];
# "[email protected]" = [''
# sed -i -e 's|process.env.ESBUILD_BINARY_PATH|"${esbuild_0_11_12}/bin/esbuild"|g' ./install.js
# ''];
# };
# You can set any desired environment by just adding them to this set just
# like you would do in a regular `stdenv.mkDerivation` invocation:
# MY_ENVIRONMENT_VARIABLE = "foo";
Expand Down
16 changes: 15 additions & 1 deletion internal.nix
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,15 @@ rec {
, preBuild ? ""
, postBuild ? ""
, preInstallLinks ? { } # set that describes which files should be linked in a specific packages folder
, preInstallCustomCommands ? { }
, ...
}@args:
assert (builtins.typeOf preInstallLinks != "set") ->
throw "[npmlock2nix] `preInstallLinks` must be an attributeset of attributesets";
assert (builtins.typeOf preInstallCustomCommands != "set") ->
throw "[npmlock2nix] `preInstallCustomCommands` must be an attributeset of list of commands";
let
cleanArgs = builtins.removeAttrs args [ "src" "packageJson" "packageLockJson" "buildInputs" "nativeBuildInputs" "nodejs" "preBuild" "postBuild" "preInstallLinks" ];
cleanArgs = builtins.removeAttrs args [ "src" "packageJson" "packageLockJson" "buildInputs" "nativeBuildInputs" "nodejs" "preBuild" "postBuild" "preInstallLinks" "preInstallCustomCommands" ];
lockfile = readLockfile packageLockJson;

preinstall_node_modules = writeTextFile {
Expand All @@ -261,11 +264,22 @@ rec {
'')
preInstallLinks
);
preInstallCustomCmds = lib.concatStringsSep "\n" (
(lib.mapAttrsToList
(name_at_version: cmds: ''
if [ "$npm_package_name@$npm_package_version" == "${name_at_version}" ]; then
${lib.concatStringsSep "\n" cmds}
fi''
)
)
preInstallCustomCommands
);
in
''
#! ${stdenv.shell}
${preInstallLinkCommands}
${preInstallCustomCmds}
if grep -I -q -r '/bin/' .; then
source $TMP/preinstall-env
Expand Down

0 comments on commit 525b97c

Please sign in to comment.