Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to set package-name and version #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions internal.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ rec {
# Type: String -> Throw
throw = str: builtins.throw "[npmlock2nix] ${str}";

# Description: Custom trace function that ensures our info messages have a common prefix.
# Type: String -> Any
trace = str: retval: builtins.trace "[npmlock2nix] ${str}" retval;

# Description: Turns an npm lockfile dependency into an attribute set as needed by fetchurl
# Type: String -> Set -> Set
makeSourceAttrs = name: dependency:
Expand Down Expand Up @@ -273,7 +277,7 @@ rec {
getAttr = name: from: lib.optionalAttrs (builtins.hasAttr name from) { "${name}" = from.${name}; };
getAttrs = names: from: lib.foldl (a: b: a // (getAttr b from)) { } names;
in
(getAttrs [ "src" "nodejs" ] attrs // node_modules_attrs);
((getAttrs [ "src" "nodejs" ] attrs) // node_modules_attrs);
milahu marked this conversation as resolved.
Show resolved Hide resolved

# Description: Takes a dependency spec and a map of github sources/hashes and returns either the map or 'null'
# Type: Set -> Set -> Set | null
Expand All @@ -300,9 +304,21 @@ rec {
assert (builtins.typeOf preInstallLinks != "set") ->
throw "`preInstallLinks` must be an attributeset of attributesets";
let
cleanArgs = builtins.removeAttrs args [ "src" "packageJson" "packageLockJson" "buildInputs" "nativeBuildInputs" "nodejs" "preBuild" "postBuild" "preInstallLinks" "githubSourceHashMap" ];
cleanArgs = builtins.removeAttrs args [ "src" "packageJson" "packageLockJson" "buildInputs" "nativeBuildInputs" "nodejs" "preBuild" "postBuild" "preInstallLinks" "githubSourceHashMap" "pname" "version" ];
lockfile = readLockfile packageLockJson;

# allow to set package-name and version, if name or version are missing in package-lock.json
milahu marked this conversation as resolved.
Show resolved Hide resolved
pname =
if (args ? pname && args.pname != "") then makeValidDrvName args.pname
else if (lockfile ? name && lockfile.name != "") then makeValidDrvName lockfile.name
else null; # pname is required, throw later
version =
if (args ? version && args.version != "") then args.version
else if (lockfile ? version && lockfile.version != "") then lockfile.version
else
trace "No version in package-lock.json, using version 0.0.0. Optionally, set version in node_modules_attrs."
"0.0.0";

milahu marked this conversation as resolved.
Show resolved Hide resolved
preinstall_node_modules = writeTextFile {
name = "prepare";
destination = "/node_modules/.hooks/prepare";
Expand Down Expand Up @@ -340,10 +356,9 @@ rec {
};

in
assert (pname == null) -> throw "A package name is required. Either set `name` in `package-lock.json`, or set `pname` in `node_modules_attrs`.";
stdenv.mkDerivation ({
inherit (lockfile) version;
pname = makeValidDrvName lockfile.name;
inherit buildInputs preBuild postBuild;
inherit pname version buildInputs preBuild postBuild;
dontUnpack = true;

nativeBuildInputs = nativeBuildInputs ++ [
Expand Down