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
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ The `node_modules` function parses `package.json` and `package-lock.json` and ge
The `node_modules` function takes an attribute set with the following attributes:

- **src** *(mandatory)*: Path to the source containing `package.json` and `package-lock.json`
- **packageJson** *(default `src+"/package.json"`)*: Path to `package.json`
- **packageLockJson** *(default `src+"/package-lock.json")*: Path to `package-lock.json`
- **packageJson** *(default *`src+"/package.json"`*)*: Path to `package.json`
- **packageLockJson** *(default *`src+"/package-lock.json"`*)*: Path to `package-lock.json`
- **buildInputs** *(default `[]`)*: Additional build dependencies
- **githubSourceHashMap** *(default: `{}`)*: Dependency hashes for evaluation in restricted mode (See [Concepts](#concepts) for details).
- **preInstallLinks** *(default: `{}`)*: Map of symlinks to create inside npm dependencies in the `node_modules` output (See [Concepts](#concepts) for details).
- **pname** *(default: detect or error)*: Force package name. Default value is the lockfile `name` field, which can be empty, but `node_modules` calls `stdenv.mkDerivation`, which requires a pname.
- **version** *(default: detect or `"0.0.0"`)*: Force derivation version. Default value is the lockfile `version` field, which can be empty.

#### Notes
- You may provide additional arguments accepted by `mkDerivation` all of which are going to be passed on.
Expand Down
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