fix: conditionally use node_modules derivation to avoid spawning to many derivations

This commit is contained in:
hsjobeki 2023-01-11 16:57:55 +01:00
parent 4edc64c4e4
commit 0881e2aa0e
3 changed files with 22 additions and 28 deletions

View File

@ -100,7 +100,6 @@
inherit pkgs lib getDependencies packageVersions name version;
nodeModulesBuilder = "${nodejsBuilder}/bin/d2nNodeModules";
})
nodeModulesTree
mkNodeModules
;
@ -112,18 +111,29 @@
depsTree
;
# path of the current package.json
# needed to check if a package has 'pre-/post-/install-scripts'
packageJSON = "${src}/package.json";
packageInfo = builtins.fromJSON (builtins.readFile packageJSON);
# type:
# hasScripts :: Bool
# build flag shows if package has the pre-pos-install scripts.
# only sub-packages with those scripts need their own node_modules derivation
hasScripts = !(packageInfo ? scripts) || (l.any (script: l.any (q: script == q) ["install" "preinstall" "postinstall"]) (b.attrNames packageInfo.scripts));
needNodeModules = hasScripts || isMain;
# type: devShellNodeModules :: Derivation
devShellNodeModules = mkNodeModules {
isMain = true;
installMethod = "copy";
packageJSON = "${src}/package.json";
inherit pname version depsTree nodeModulesTree;
inherit pname version depsTree packageJSON;
};
# type: nodeModules :: Derivation
nodeModules = mkNodeModules {
inherit installMethod isMain depsTree nodeModulesTree;
inherit pname version;
packageJSON = "${src}/package.json";
inherit installMethod;
inherit isMain;
inherit pname version depsTree packageJSON;
};
installMethod =
@ -178,7 +188,7 @@
fi
'';
configurePhase = ''
configurePhase = l.optionalString needNodeModules ''
runHook preConfigure
cp -r ${nodeModules} ./node_modules
@ -230,8 +240,6 @@
fi
fi
export NODE_MODULES_PATH=${nodeModules}
${nodejsBuilder}/bin/d2nMakeOutputs
runHook postInstall

View File

@ -33,7 +33,6 @@
dependencies :: Dependencies,
}
}
*/
resolveChildren = {
name,
@ -84,6 +83,7 @@
directDeps = getDependencies name version;
# type: { ${name} :: String } # e.g { "prettier" = "1.2.3"; }
# set with all direct dependencies contains every 'root' package with only one version
directDepsAttrs = l.listToAttrs (b.map (dep: l.nameValuePair dep.name dep.version) directDeps);
# build the node_modules tree from all known rootPackages
@ -99,10 +99,10 @@
inherit version dependencies;
}
)
# filter out the 'self' package (e.g. "my-app")
(l.filterAttrs (n: v: n != name) rootPackages);
/*
Type:
mkNodeModules :: {
pname :: String,
@ -111,6 +111,7 @@
installMethod :: "copy" | "symlink",
depsTree :: DependencyTree,
nodeModulesTree :: NodeModulesTree,
packageJSON :: Path
}
*/
mkNodeModules = {
@ -119,7 +120,6 @@
pname,
version,
depsTree,
nodeModulesTree,
packageJSON,
}:
# dependency tree as JSON needed to build node_modules
@ -128,7 +128,7 @@
nmTreeJSON = b.toJSON nodeModulesTree;
in
pkgs.runCommandLocal "node-modules" {
pname = "${pname}-node_modules";
pname = "node_modules-${pname}";
inherit version;
buildInputs = with pkgs; [python3];
@ -147,11 +147,7 @@
if [ ! -d "$out" ]; then
mkdir -p $out
fi
# for debuging comment this out
# cp $depsTreeJSONPath $out/.d2n-resolved-dependency-tree.json
# cp $nmTreeJSONPath $out/.d2n-resolved-folder-structure.json
'';
in {
inherit nodeModulesTree mkNodeModules;
inherit mkNodeModules;
}

View File

@ -12,11 +12,6 @@ from dataclasses import dataclass
env: dict[str, str] = os.environ.copy()
@dataclass
class Input:
node_modules: Path
@dataclass
class Output:
out: Path
@ -31,11 +26,6 @@ def get_outputs() -> Output:
return Output(outputs["out"], outputs["lib"])
def get_inputs() -> Input:
node_modules_path = Path(get_env("NODE_MODULES_PATH"))
return Input(node_modules_path)
def is_main_package() -> bool:
"""Returns True or False depending on the 'isMain' env variable."""
return bool(get_env("isMain"))