From 0c0ce5d6d669eb731500bbc6ad2e014523548b6d Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Sun, 21 Aug 2022 23:45:19 +0200 Subject: [PATCH] move extra info to subsystemAttrs --- .../nodejs/builders/granular/default.nix | 25 ++++--- .../translators/package-lock-v2/default.nix | 68 +++++++++++-------- .../translators/package-lock-v2/v2-parse.nix | 34 +++++----- 3 files changed, 72 insertions(+), 55 deletions(-) diff --git a/src/subsystems/nodejs/builders/granular/default.nix b/src/subsystems/nodejs/builders/granular/default.nix index 2c0e222fec..500a993d85 100644 --- a/src/subsystems/nodejs/builders/granular/default.nix +++ b/src/subsystems/nodejs/builders/granular/default.nix @@ -209,7 +209,7 @@ ( dep: let p = allPackages."${dep.name}"."${dep.version}"; - s = p.sourceInfo; + s = p.extraInfo; in # this dep is a cyclee !(isCyclee dep.name dep.version) @@ -244,7 +244,7 @@ myDeps = lib.filter (dep: let - s = dep.sourceInfo; + s = dep.extraInfo; in (withOptionals || !(s.optional or false)) && (!isMain || (withDev || !(s.dev or false)))) @@ -349,14 +349,13 @@ then null else pkgs."electron_${electronVersionMajor}".headers; - sourceInfo = let - e = getSourceSpec name version; - try = builtins.tryEval (builtins.deepSeq e e); - in - if try.success - then try.value - else {}; - hasInstall = !(sourceInfo.noInstall or false); + hasExtraInfo = subsystemAttrs ? extraInfo; + extraInfo = subsystemAttrs.extraInfo.${name}.${version} or {}; + # If the translator doesn't provide extraInfo, assume scripts + hasInstall = + if hasExtraInfo + then extraInfo.hasInstallScript or false + else true; isMain = isMainPackage name version; pkg = produceDerivation name (stdenv.mkDerivation rec { @@ -587,6 +586,10 @@ # Runs the install command which defaults to 'npm run postinstall'. # Allows using custom install command by overriding 'buildScript'. # TODO this logic supposes a build script, which is not documented + # for installing, we only need to run `npm run install` (pre and post scripts run automatically) + # https://github.com/npm/npm/issues/5919 + # TODO build first if has build, give it devModules during build + buildPhase = '' runHook preBuild @@ -657,7 +660,7 @@ }); in pkg - // {inherit sourceInfo;}; + // {inherit extraInfo;}; in outputs; } diff --git a/src/subsystems/nodejs/translators/package-lock-v2/default.nix b/src/subsystems/nodejs/translators/package-lock-v2/default.nix index a8d6eeedef..9c8e5a0917 100644 --- a/src/subsystems/nodejs/translators/package-lock-v2/default.nix +++ b/src/subsystems/nodejs/translators/package-lock-v2/default.nix @@ -47,17 +47,28 @@ rootDependencies = resolved.self.deps; - identifyGitSource = dependencyObject: + identifyGitSource = dep: # TODO: when integrity is there, and git url is github then use tarball instead - # ! (dependencyObject ? integrity) && - dlib.identifyGitUrl dependencyObject.url; + # ! (dep ? integrity) && + dlib.identifyGitUrl dep.url; - getVersion = dependencyObject: dependencyObject.version; + getVersion = dep: dep.version; - getPath = dependencyObject: - lib.removePrefix "file:" dependencyObject.url; + getPath = dep: + lib.removePrefix "file:" dep.url; - stripDep = dep: l.removeAttrs dep ["pname" "version" "deps"]; + getSource = { + url, + hash, + ... + }: {inherit url hash;}; + + # TODO check that this works with workspaces + extraInfo = b.foldl' (acc: dep: + if dep.extra != {} + then l.recursiveUpdate acc {${dep.pname}.${dep.version} = dep.extra;} + else acc) {} + resolved.allDeps; in utils.simpleTranslate ({ @@ -81,40 +92,41 @@ subsystemName = "nodejs"; - subsystemAttrs = {nodejsVersion = args.nodejs;}; + subsystemAttrs = { + inherit extraInfo; + nodejsVersion = args.nodejs; + }; # functions serializePackages = inputData: inputData; - getName = dependencyObject: dependencyObject.pname; + getName = dep: dep.pname; inherit getVersion; - # TODO handle npm link maybe? - getSourceType = dependencyObject: - if identifyGitSource dependencyObject - then "git" - else if lib.hasPrefix "file:" dependencyObject.url + # TODO handle npm link maybe? not sure what it looks like in lock + getSourceType = dep: + if lib.hasPrefix "file:" dep.url then "path" + else if identifyGitSource dep + then "git" else "http"; sourceConstructors = { - git = dependencyObject: - (stripDep dependencyObject) - // (dlib.parseGitUrl dependencyObject.url); - - http = dependencyObject: (stripDep dependencyObject); - - path = dependencyObject: - (stripDep dependencyObject) - // (dlib.construct.pathSource { - path = getPath dependencyObject; - rootName = project.name; - rootVersion = packageVersion; - }); + git = dep: + (getSource dep) + // (dlib.parseGitUrl dep.url); + + http = dep: (getSource dep); + + path = dep: (dlib.construct.pathSource { + path = getPath dep; + rootName = project.name; + rootVersion = packageVersion; + }); }; - getDependencies = dependencyObject: dependencyObject.deps; + getDependencies = dep: dep.deps; }); in rec { version = 2; diff --git a/src/subsystems/nodejs/translators/package-lock-v2/v2-parse.nix b/src/subsystems/nodejs/translators/package-lock-v2/v2-parse.nix index 91dbf41b16..27a2a9f17d 100644 --- a/src/subsystems/nodejs/translators/package-lock-v2/v2-parse.nix +++ b/src/subsystems/nodejs/translators/package-lock-v2/v2-parse.nix @@ -58,6 +58,21 @@ assert (lib.elem lock.lockfileVersion [2 3]); let if n == "" then lockName else n; + + extraAttrs = { + # platforms this package works on + os = 1; + # this is a dev dependency + dev = 1; + # this is an optional dependency + optional = 1; + # this is an optional dev dependency + devOptional = 1; + # set of binary scripts { name = relativePath } + bin = 1; # pkg needs to run install scripts + hasInstallScript = 1; + }; + getExtra = pkg: b.intersectAttrs extraAttrs pkg; in { version ? "unknown", @@ -65,25 +80,13 @@ assert (lib.elem lock.lockfileVersion [2 3]); let resolved ? "file://${source}", # hash for content integrity ? null, - # platforms this package works on - os ? null, - # this is a dev dependency - dev ? false, - # this is an optional dependency - optional ? false, - # this is an optional dev dependency - devOptional ? false, - # set of binary scripts { name = relativePath } - bin ? {}, - # pkg needs to run install scripts - hasInstallScript ? false, dependencies ? null, devDependencies ? null, peerDependencies ? null, optionalDependencies ? null, peerDependenciesMeta ? null, ... - }: let + } @ pkg: let deps = lib.unique ((resolveDeps dependencies parts false) @@ -92,11 +95,10 @@ assert (lib.elem lock.lockfileVersion [2 3]); let ++ (resolveDeps peerDependencies parts true) ++ (resolveDeps peerDependenciesMeta parts true)); in { - inherit pname version deps os dev optional devOptional bin; + inherit pname version deps; url = resolved; hash = integrity; - # Storing negation so other translators don't have to have this feature - noInstall = !hasInstallScript; + extra = getExtra pkg; }; allDeps = lib.mapAttrsToList mapPkg pkgs;