Skip to content

Commit

Permalink
testing out fetchPnpmDeps nix libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chickensoupwithrice committed Mar 19, 2024
1 parent 2015a5d commit 479ea46
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
inherit (pkgs) lib;
inherit (pkgs) mkPnpmPackage;
inherit callPackage dream2nix pkgs;
inherit (callPackage ./pkgs/build-support/node/fetch-pnpm-deps { }) fetchPnpmDeps pnpmConfigHook;
};

explicitPkgs = import ./pkgs {
Expand Down
73 changes: 73 additions & 0 deletions pkgs/build-support/node/fetch-pnpm-deps/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{ lib
, stdenvNoCC
, nodePackages
, jq
, moreutils
, cacert
, makeSetupHook
}: {
fetchPnpmDeps =
{ src
, hash ? ""
, pname
, pnpm ? nodePackages.pnpm
, ...
} @ args:
let
args' = builtins.removeAttrs args [ "hash" "pname" "pnpm" "supportedArchitectures" ];
hash' =
if hash != "" then {
outputHash = hash;
} else {
outputHash = "";
outputHashAlgo = "sha256";
};
in
# NOTE: This requires pnpm 8.10.0 or newer
# https://github.com/pnpm/pnpm/pull/7214
assert lib.versionAtLeast pnpm.version "8.10.0";
stdenvNoCC.mkDerivation (args' // {
name = "${pname}-pnpm-deps";

nativeBuildInputs = [
jq
moreutils
pnpm
cacert
];

# https://github.com/NixOS/nixpkgs/blob/763e59ffedb5c25774387bf99bc725df5df82d10/pkgs/applications/misc/pot/default.nix#L56
installPhase = ''
runHook preInstall
export HOME=$(mktemp -d)
pnpm config set store-dir $out
# pnpm is going to warn us about using --force
# --force allows us to fetch all dependencies including ones that aren't meant for our host platform
pnpm install --frozen-lockfile --ignore-script --force
runHook postInstall
'';

fixupPhase = ''
runHook preFixup
rm -rf $out/v3/tmp
for f in $(find $out -name "*.json"); do
sed -i -E -e 's/"checkedAt":[0-9]+,//g' $f
jq --sort-keys . $f | sponge $f
done
runHook postFixup
'';

dontConfigure = true;
dontBuild = true;
outputHashMode = "recursive";
} // hash');

pnpmConfigHook = makeSetupHook
{
name = "pnpm-config-hook";
} ./pnpm-config-hook.sh;
}
36 changes: 36 additions & 0 deletions pkgs/build-support/node/fetch-pnpm-deps/pnpm-config-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# shellcheck shell=bash

pnpmConfigHook() {
echo "Executing pnpmConfigHook"

if [ -z "${pnpmDeps-}" ]; then
echo "Error: 'npmDeps' should be set when using npmConfigHook."
exit 1
fi

echo "Configuring pnpm store"

export HOME=$(mktemp -d)
export STORE_PATH=$(mktemp -d)

cp -Tr "$pnpmDeps" "$STORE_PATH"
chmod -R +w "$STORE_PATH"

pnpm config set store-dir "$STORE_PATH"

echo "Installing dependencies"

if ! pnpm install --offline --frozen-lockfile --ignore-script; then
echo
echo "ERROR: pnpm failed to install dependencies"
echo

exit 1
fi

patchShebangs node_modules/{*,.*}

echo "Finished pnpmConfigHook"
}

postConfigureHooks+=(pnpmConfigHook)
15 changes: 12 additions & 3 deletions pkgs/by-name/atomic-browser/package.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
mkDerivation,
fetchFromGitHub,
lib,
mkPnpmPackage,
fetchPnpmDeps,
pnpmConfigHook,
}: let
inherit
(lib)
licenses
maintainers
;
in
mkPnpmPackage rec {
mkDerivation rec {
pname = "atomic-browser";
version = "v0.37.0";

Expand All @@ -21,11 +24,17 @@ mkPnpmPackage rec {
};

src = "${monorepoSrc}/browser";
pnpmDeps = fetchPnpmDeps {
inherit src pname;
hash = "";
};

nativeBuildInputs = [ pnpmConfigHook ];

# These 2 options are needed to work with pnpm workspaces, which atomic-browser is using
# https://github.com/nzbr/pnpm2nix-nzbr/issues/29#issuecomment-1918811838
installInPlace = true;
distDir = ".";
#installInPlace = true;
#distDir = ".";

meta = {
description = "Create, share, fetch and model linked Atomic Data! There are three components: a javascript / typescript library, a react library, and a complete GUI: Atomic-Data Browser.";
Expand Down
4 changes: 3 additions & 1 deletion pkgs/by-name/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
callPackage,
dream2nix,
mkPnpmPackage,
fetchPnpmDeps,
pnpmConfigHook,
pkgs,
}: let
baseDirectory = ./.;
Expand Down Expand Up @@ -49,7 +51,7 @@
mapAttrs (
_: directory:
if pathExists (directory + "/package.nix")
then callPackage (directory + "/package.nix") {}
then callPackage (directory + "/package.nix") { }
else if pathExists (directory + "/dream2.nix")
then callModule (directory + "/dream2.nix")
else throw "No package.nix or dream2.nix found in ${directory}"
Expand Down

0 comments on commit 479ea46

Please sign in to comment.