From 0881e2aa0e99543900ed8c72949ac969518c8faf Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Wed, 11 Jan 2023 16:57:55 +0100 Subject: [PATCH] fix: conditionally use node_modules derivation to avoid spawning to many derivations --- .../builders/strict-builder/default.nix | 26 ++++++++++++------- .../strict-builder/lib/node-modules-tree.nix | 14 ++++------ .../python-builder/app/lib/derivation.py | 10 ------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/subsystems/nodejs/builders/strict-builder/default.nix b/src/subsystems/nodejs/builders/strict-builder/default.nix index ce65a30e..acf66b76 100644 --- a/src/subsystems/nodejs/builders/strict-builder/default.nix +++ b/src/subsystems/nodejs/builders/strict-builder/default.nix @@ -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 diff --git a/src/subsystems/nodejs/builders/strict-builder/lib/node-modules-tree.nix b/src/subsystems/nodejs/builders/strict-builder/lib/node-modules-tree.nix index 81672546..166e402e 100644 --- a/src/subsystems/nodejs/builders/strict-builder/lib/node-modules-tree.nix +++ b/src/subsystems/nodejs/builders/strict-builder/lib/node-modules-tree.nix @@ -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; } diff --git a/src/subsystems/nodejs/builders/strict-builder/python-builder/app/lib/derivation.py b/src/subsystems/nodejs/builders/strict-builder/python-builder/app/lib/derivation.py index a1a8ed88..5b1199f6 100644 --- a/src/subsystems/nodejs/builders/strict-builder/python-builder/app/lib/derivation.py +++ b/src/subsystems/nodejs/builders/strict-builder/python-builder/app/lib/derivation.py @@ -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"))