Skip to content

Commit

Permalink
make pnpmStore and nodeModules accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
nzbr committed Mar 15, 2023
1 parent c93f363 commit 6ae61e2
Showing 1 changed file with 89 additions and 84 deletions.
173 changes: 89 additions & 84 deletions derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,88 +29,93 @@ in
, extraBuildInputs ? [ ]
, ...
}@attrs:
let
pnpmStore = runCommand "${name}-pnpm-store"
{
nativeBuildInputs = [ nodejs pnpm ];
} ''
mkdir -p $out
store=$(pnpm store path)
mkdir -p $(dirname $store)
ln -s $out $(pnpm store path)
pnpm store add ${concatStringsSep " " (dependencyTarballs { inherit registry; lockfile = pnpmLockYaml; })}
'';

nodeModules = stdenv.mkDerivation {
name = "${name}-node-modules";
nativeBuildInputs = [ nodejs pnpm ];

unpackPhase = ''
${concatStringsSep "\n" (
map (v:
let
nv = if isAttrs v then v else { name = "."; value = v; };
in
"cp -vr ${nv.value} ${nv.name}"
)
([
{ name = "package.json"; value = packageJSON; }
{ name = "pnpm-lock.yaml"; value = pnpmLockYaml; }
] ++ extraNodeModuleSources)
)}
'';

buildPhase = ''
store=$(pnpm store path)
mkdir -p $(dirname $store)
# solve pnpm: EACCES: permission denied, copyfile '/build/.pnpm-store
${if !copyPnpmStore
then "ln -s"
else "cp -RL"
} ${pnpmStore} $(pnpm store path)
pnpm install --frozen-lockfile --offline
'';

installPhase = ''
cp -r node_modules/. $out
'';
};
in
stdenv.mkDerivation ({
inherit src name;

nativeBuildInputs = [ nodejs pnpm ] ++ extraBuildInputs;

configurePhase = ''
runHook preConfigure
${if !copyNodeModules
then "ln -s"
else "cp -r"
} ${nodeModules}/. node_modules
runHook postConfigure
'';

buildPhase = ''
runHook preBuild
pnpm run ${script}
runHook postBuild
'';

installPhase = ''
runHook preInstall
mv ${distDir} $out
runHook postInstall
'';

} // (attrs // { extraNodeModuleSources = null; }));
stdenv.mkDerivation (
recursiveUpdate
(rec {
inherit src name;

nativeBuildInputs = [ nodejs pnpm ] ++ extraBuildInputs;

configurePhase = ''
runHook preConfigure
${if !copyNodeModules
then "ln -s"
else "cp -r"
} ${passthru.nodeModules}/. node_modules
runHook postConfigure
'';

buildPhase = ''
runHook preBuild
pnpm run ${script}
runHook postBuild
'';

installPhase = ''
runHook preInstall
mv ${distDir} $out
runHook postInstall
'';

passthru = {
pnpmStore = runCommand "${name}-pnpm-store"
{
nativeBuildInputs = [ nodejs pnpm ];
} ''
mkdir -p $out
store=$(pnpm store path)
mkdir -p $(dirname $store)
ln -s $out $(pnpm store path)
pnpm store add ${concatStringsSep " " (dependencyTarballs { inherit registry; lockfile = pnpmLockYaml; })}
'';

nodeModules = stdenv.mkDerivation {
name = "${name}-node-modules";
nativeBuildInputs = [ nodejs pnpm ];

unpackPhase = ''
${concatStringsSep "\n" (
map (v:
let
nv = if isAttrs v then v else { name = "."; value = v; };
in
"cp -vr ${nv.value} ${nv.name}"
)
([
{ name = "package.json"; value = packageJSON; }
{ name = "pnpm-lock.yaml"; value = pnpmLockYaml; }
] ++ extraNodeModuleSources)
)}
'';

buildPhase = ''
store=$(pnpm store path)
mkdir -p $(dirname $store)
# solve pnpm: EACCES: permission denied, copyfile '/build/.pnpm-store
${if !copyPnpmStore
then "ln -s"
else "cp -RL"
} ${passthru.pnpmStore} $(pnpm store path)
pnpm install --frozen-lockfile --offline
'';

installPhase = ''
cp -r node_modules/. $out
'';
};
};

})
(attrs // { extraNodeModuleSources = null; })
);
}

0 comments on commit 6ae61e2

Please sign in to comment.