nodejs-devshell-v3: init

This commit is contained in:
DavHau 2023-11-17 14:30:09 +07:00 committed by mergify[bot]
parent db35339b1e
commit c25e88974f
4 changed files with 130 additions and 12 deletions

View File

@ -5,19 +5,9 @@
...
}: {
imports = [
dream2nix.modules.dream2nix.nodejs-devshell
dream2nix.modules.dream2nix.nodejs-package-lock
dream2nix.modules.dream2nix.nodejs-devshell-v3
];
nodejs-package-lock = {
source = config.deps.fetchFromGitHub {
owner = "piuccio";
repo = "cowsay";
rev = "v1.5.0";
sha256 = "sha256-TZ3EQGzVptNqK3cNrkLnyP1FzBd81XaszVucEnmBy4Y=";
};
};
deps = {nixpkgs, ...}: {
inherit
(nixpkgs)
@ -32,7 +22,12 @@
version = "1.5.0";
mkDerivation = {
src = config.nodejs-package-lock.source;
src = config.deps.fetchFromGitHub {
owner = "DavHau";
repo = "cowsay";
rev = "package-lock-v3";
sha256 = "sha256-KuZkGWl5An78IFR5uT/2jVTXdm71oWB+p143svYVkqQ=";
};
# allow devshell to be built -> CI pipeline happy
buildPhase = "mkdir $out";
};

View File

@ -0,0 +1,49 @@
{
config,
lib,
dream2nix,
...
}: let
l = lib // builtins;
cfg = config.nodejs-devshell-v3;
nodeModulesDir = "${cfg.nodeModules.public}/lib/node_modules/${config.name}/node_modules";
in {
imports = [
./interface.nix
dream2nix.modules.dream2nix.mkDerivation
dream2nix.modules.dream2nix.nodejs-package-lock-v3
];
nodejs-devshell-v3.nodeModules = {
imports = [
{inherit (config) nodejs-package-lock-v3 name version;}
{mkDerivation.src = l.mkForce null;}
];
};
nodejs-package-lock-v3.packageLockFile = "${config.mkDerivation.src}/package-lock.json";
# rsync the node_modules folder
# - tracks node-modules store path via .dream2nix/.node_modules_id
# - omits copying if store path did not change
# - if store path changes, only replaces updated files
# - rsync can be restarted from any point, if failed or aborted mid execution.
# Options:
# -a -> all files recursive, preserve symlinks, etc.
# --delete -> removes deleted files
# --chmod=+ug+w -> make folder writeable by user+group
mkDerivation.shellHook = ''
ID=${nodeModulesDir}
currID=$(cat .dream2nix/.node_modules_id 2> /dev/null)
mkdir -p .dream2nix
if [[ "$ID" != "$currID" || ! -d "node_modules" ]];
then
${config.deps.rsync}/bin/rsync -a --chmod=ug+w --delete ${nodeModulesDir}/ ./node_modules/
echo -n $ID > .dream2nix/.node_modules_id
echo "Ok: node_modules updated"
fi
'';
}

View File

@ -0,0 +1,23 @@
{
config,
lib,
dream2nix,
packageSets,
...
}: let
l = lib // builtins;
t = l.types;
in {
options.nodejs-devshell-v3 = {
nodeModules = l.mkOption {
description = "drv-parts module for the node_modules derivation";
type = t.submoduleWith {
specialArgs = {inherit packageSets dream2nix;};
modules = [
dream2nix.modules.dream2nix.core
dream2nix.modules.dream2nix.nodejs-node-modules-v3
];
};
};
};
}

View File

@ -0,0 +1,51 @@
{
config,
lib,
dream2nix,
...
}: let
l = lib // builtins;
in {
imports = [
dream2nix.modules.dream2nix.mkDerivation
dream2nix.modules.dream2nix.nodejs-package-lock-v3
dream2nix.modules.dream2nix.nodejs-granular-v3
];
mkDerivation = {
dontUnpack = true;
dontPatch = true;
dontBuild = true;
dontInstall = true;
dontFixup = true;
preBuildPhases = l.mkForce [];
preInstallPhases = l.mkForce ["installPhaseNodejsNodeModules"];
};
env = {
# Prepare node_modules installation to $out/lib/node_modules
patchPhaseNodejs = l.mkForce ''
nodeModules=$out/lib/node_modules
mkdir -p $nodeModules/$packageName
cd $nodeModules/$packageName
'';
# copy .bin entries
# from $out/lib/node_modules/.bin
# to $out/lib/node_modules/<package-name>/node_modules/.bin
installPhaseNodejsNodeModules = ''
mkdir -p ./node_modules/.bin
localNodeModules=$nodeModules/$packageName/node_modules
for executablePath in $out/lib/node_modules/.bin/*; do
binaryName=$(basename $executablePath)
target=$(realpath --relative-to=$localNodeModules/.bin $executablePath)
echo linking binary $binaryName to nix store: $target
ln -s $target $localNodeModules/.bin/$binaryName
done
'';
};
nodejs-granular-v3 = {
installMethod = "copy";
};
}