diff --git a/examples/packages/single-language/nodejs-project/default.nix b/examples/packages/single-language/nodejs-project/default.nix index 8bfb0eab..cb745c1a 100644 --- a/examples/packages/single-language/nodejs-project/default.nix +++ b/examples/packages/single-language/nodejs-project/default.nix @@ -7,7 +7,7 @@ system = config.deps.stdenv.system; in { imports = [ - dream2nix.modules.dream2nix.nodejs-package-json + dream2nix.modules.dream2nix.nodejs-package-json-v3 dream2nix.modules.dream2nix.nodejs-granular-v3 ]; diff --git a/modules/dream2nix/nodejs-package-json-v3/default.nix b/modules/dream2nix/nodejs-package-json-v3/default.nix new file mode 100644 index 00000000..1e0fad70 --- /dev/null +++ b/modules/dream2nix/nodejs-package-json-v3/default.nix @@ -0,0 +1,73 @@ +{ + config, + lib, + ... +}: let + l = lib // builtins; + cfg = config.nodejs-package-json; + + writers = import ../../../pkgs/writers { + inherit lib; + inherit + (config.deps) + bash + coreutils + gawk + path + writeScript + writeScriptBin + ; + }; + + npmArgs = l.concatStringsSep " " (map (arg: "'${arg}'") cfg.npmArgs); +in { + imports = [ + ./interface.nix + ../nodejs-package-lock-v3 + ]; + config = { + deps = {nixpkgs, ...}: + l.mapAttrs (_: l.mkDefault) { + inherit + (nixpkgs) + bash + coreutils + gawk + path + writeScript + writeScriptBin + ; + npm = nixpkgs.nodejs.pkgs.npm; + }; + + lock.fields.package-lock.script = + writers.writePureShellScript + [ + config.deps.coreutils + config.deps.npm + ] + '' + source=${cfg.source} + + pushd $TMPDIR + + cp -r $source/* ./ + chmod -R +w ./ + rm -f package-lock.json + npm install --package-lock-only ${npmArgs} + + mv package-lock.json $out + + popd + ''; + + lock.invalidationData = { + packageJson = lib.importJSON (config.nodejs-package-json.source + /package.json); + }; + + nodejs-package-lock-v3 = { + packageLockFile = null; + packageLock = l.mkForce config.lock.content.package-lock; + }; + }; +} diff --git a/modules/dream2nix/nodejs-package-json-v3/interface.nix b/modules/dream2nix/nodejs-package-json-v3/interface.nix new file mode 100644 index 00000000..191d9676 --- /dev/null +++ b/modules/dream2nix/nodejs-package-json-v3/interface.nix @@ -0,0 +1,31 @@ +{ + config, + lib, + ... +}: let + l = lib // builtins; + t = l.types; +in { + imports = [ + ../nodejs-package-lock-v3/interface.nix + ]; + options.nodejs-package-json = l.mapAttrs (_: l.mkOption) { + source = { + type = t.either t.path t.package; + description = "Source of the package"; + default = config.mkDerivation.src; + defaultText = "config.mkDerivation.src"; + }; + npmArgs = { + type = t.listOf t.str; + description = "extra arguments to pass to 'npm install'"; + default = []; + }; + }; + options.deps = l.mapAttrs (_: l.mkOption) { + npm = { + type = t.package; + description = "The npm package used to build the lock file"; + }; + }; +} diff --git a/modules/dream2nix/nodejs-package-json/default.nix b/modules/dream2nix/nodejs-package-json/default.nix index 1e0fad70..2c4a51a9 100644 --- a/modules/dream2nix/nodejs-package-json/default.nix +++ b/modules/dream2nix/nodejs-package-json/default.nix @@ -6,6 +6,11 @@ l = lib // builtins; cfg = config.nodejs-package-json; + npm = + if l.versionOlder config.deps.npm.version "9" + then config.deps.npm + else throw "The version of config.deps.npm must be < 9"; + writers = import ../../../pkgs/writers { inherit lib; inherit @@ -23,7 +28,7 @@ in { imports = [ ./interface.nix - ../nodejs-package-lock-v3 + ../nodejs-package-lock ]; config = { deps = {nixpkgs, ...}: @@ -37,14 +42,13 @@ in { writeScript writeScriptBin ; - npm = nixpkgs.nodejs.pkgs.npm; }; lock.fields.package-lock.script = writers.writePureShellScript [ config.deps.coreutils - config.deps.npm + npm ] '' source=${cfg.source} @@ -61,11 +65,7 @@ in { popd ''; - lock.invalidationData = { - packageJson = lib.importJSON (config.nodejs-package-json.source + /package.json); - }; - - nodejs-package-lock-v3 = { + nodejs-package-lock = { packageLockFile = null; packageLock = l.mkForce config.lock.content.package-lock; }; diff --git a/modules/dream2nix/nodejs-package-json/interface.nix b/modules/dream2nix/nodejs-package-json/interface.nix index 191d9676..ee125be3 100644 --- a/modules/dream2nix/nodejs-package-json/interface.nix +++ b/modules/dream2nix/nodejs-package-json/interface.nix @@ -7,14 +7,13 @@ t = l.types; in { imports = [ - ../nodejs-package-lock-v3/interface.nix + ../nodejs-package-lock/interface.nix ]; options.nodejs-package-json = l.mapAttrs (_: l.mkOption) { source = { type = t.either t.path t.package; description = "Source of the package"; default = config.mkDerivation.src; - defaultText = "config.mkDerivation.src"; }; npmArgs = { type = t.listOf t.str;