From e00cfe671f3ef227d4206718c760b60dbbadfe61 Mon Sep 17 00:00:00 2001 From: Stefan Frijters Date: Mon, 9 Mar 2026 12:44:32 +0100 Subject: [PATCH] build-support/node/import-npm-lock: use structuredAttrs instead of passAsFile --- .../node/import-npm-lock/default.nix | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/pkgs/build-support/node/import-npm-lock/default.nix b/pkgs/build-support/node/import-npm-lock/default.nix index c299ca46928d..409ab3999134 100644 --- a/pkgs/build-support/node/import-npm-lock/default.nix +++ b/pkgs/build-support/node/import-npm-lock/default.nix @@ -12,6 +12,7 @@ let match elemAt toJSON + toFile removeAttrs ; inherit (lib) importJSON mapAttrs; @@ -157,18 +158,15 @@ lib.fix (self: { { inherit pname version; - passAsFile = [ - "package" - "packageLock" - ]; + package = toFile (toJSON packageJSON'); + packageLock = toFile (toJSON packageLock'); - package = toJSON packageJSON'; - packageLock = toJSON packageLock'; + __structuredAttrs = true; } '' mkdir $out - cp "$packagePath" $out/package.json - cp "$packageLockPath" $out/package-lock.json + cp "${package}" > $out/package.json + cp "${packageLock}" > $out/package-lock.json ''; # Build node modules from package.json & package-lock.json @@ -180,6 +178,11 @@ lib.fix (self: { nodejs, derivationArgs ? { }, }: + let + # Backwards compatibility: if derivationArgs contains passAsFile, + # we can't force structuredAttrs here yet. + __structuredAttrs = !(derivationArgs ? passAsFile); + in stdenv.mkDerivation ( { pname = derivationArgs.pname or "${getName package}-node-modules"; @@ -213,17 +216,29 @@ lib.fix (self: { ++ lib.optionals stdenv.hostPlatform.isDarwin [ cctools ] ++ derivationArgs.nativeBuildInputs or [ ]; + postPatch = + ( + if __structuredAttrs then + '' + echo -n "$package" > package.json + echo -n "$packageLock" > package-lock.json + '' + else + '' + cp --no-preserve=mode "$packagePath" package.json + cp --no-preserve=mode "$packageLockPath" package-lock.json + '' + ) + + derivationArgs.postPatch or ""; + + inherit __structuredAttrs; + } + // lib.optionalAttrs (!__structuredAttrs) { passAsFile = [ "package" "packageLock" ] - ++ derivationArgs.passAsFile or [ ]; - - postPatch = '' - cp --no-preserve=mode "$packagePath" package.json - cp --no-preserve=mode "$packageLockPath" package-lock.json - '' - + derivationArgs.postPatch or ""; + ++ derivationArgs.passAsFile; } );