1
1
# This file originates from node2nix
2
2
3
- { lib , stdenv , nodejs , python2 , pkgs , libtool , runCommand , writeTextFile , writeShellScript } :
3
+ { lib , stdenv , nodejs , python2 , pkgs , libtool , runCommand , writeTextFile , writeShellScript } :
4
4
5
5
let
6
6
# Workaround to cope with utillinux in Nixpkgs 20.09 and util-linux in Nixpkgs master
9
9
python = if nodejs ? python then nodejs . python else python2 ;
10
10
11
11
# Create a tar wrapper that filters all the 'Ignoring unknown extended header keyword' noise
12
- tarWrapper = runCommand "tarWrapper" { } ''
12
+ tarWrapper = runCommand "tarWrapper" { } ''
13
13
mkdir -p $out/bin
14
14
15
15
cat > $out/bin/tar <<EOF
90
90
# Bundle the dependencies of the package
91
91
#
92
92
# Only include dependencies if they don't exist. They may also be bundled in the package.
93
- includeDependencies = { dependencies } :
94
- lib . optionalString ( dependencies != [ ] ) (
93
+ includeDependencies = { dependencies } :
94
+ lib . optionalString ( dependencies != [ ] ) (
95
95
''
96
96
mkdir -p node_modules
97
97
cd node_modules
98
98
''
99
- + ( lib . concatMapStrings
100
- ( dependency :
101
- ''
102
- if [ ! -e "${ dependency . packageName } " ]; then
103
- ${ composePackage dependency }
104
- fi
105
- ''
106
- )
107
- dependencies )
99
+ + ( lib . concatMapStrings ( dependency :
100
+ ''
101
+ if [ ! -e "${ dependency . packageName } " ]; then
102
+ ${ composePackage dependency }
103
+ fi
104
+ ''
105
+ ) dependencies )
108
106
+ ''
109
107
cd ..
110
108
''
111
109
) ;
112
110
113
111
# Recursively composes the dependencies of a package
114
- composePackage = { name , packageName , src , dependencies ? [ ] , ... } @args :
112
+ composePackage = { name , packageName , src , dependencies ? [ ] , ... } @args :
115
113
builtins . addErrorContext "while evaluating node package '${ packageName } '" ''
116
114
installPackage "${ packageName } " "${ src } "
117
115
${ includeDependencies { inherit dependencies ; } }
118
116
cd ..
119
117
${ lib . optionalString ( builtins . substring 0 1 packageName == "@" ) "cd .." }
120
118
'' ;
121
119
122
- pinpointDependencies = { dependencies , production } :
120
+ pinpointDependencies = { dependencies , production } :
123
121
let
124
122
pinpointDependenciesFromPackageJSON = writeTextFile {
125
123
name = "pinpointDependencies.js" ;
196
194
# dependencies in the package.json file to the versions that are actually
197
195
# being used.
198
196
199
- pinpointDependenciesOfPackage = { packageName , dependencies ? [ ] , production ? true , ... } @args :
197
+ pinpointDependenciesOfPackage = { packageName , dependencies ? [ ] , production ? true , ... } @args :
200
198
''
201
199
if [ -d "${ packageName } " ]
202
200
then
209
207
210
208
# Extract the Node.js source code which is used to compile packages with
211
209
# native bindings
212
- nodeSources = runCommand "node-sources" { } ''
210
+ nodeSources = runCommand "node-sources" { } ''
213
211
tar --no-same-owner --no-same-permissions -xf ${ nodejs . src }
214
212
mv node-* $out
215
213
'' ;
@@ -416,73 +414,73 @@ let
416
414
'' ;
417
415
} ;
418
416
419
- prepareAndInvokeNPM = { packageName , bypassCache , reconstructLock , npmFlags , production } :
417
+ prepareAndInvokeNPM = { packageName , bypassCache , reconstructLock , npmFlags , production } :
420
418
let
421
419
forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com" ;
422
420
in
423
421
''
424
- # Pinpoint the versions of all dependencies to the ones that are actually being used
425
- echo "pinpointing versions of dependencies..."
426
- source $pinpointDependenciesScriptPath
427
-
428
- # Patch the shebangs of the bundled modules to prevent them from
429
- # calling executables outside the Nix store as much as possible
430
- patchShebangs .
431
-
432
- # Deploy the Node.js package by running npm install. Since the
433
- # dependencies have been provided already by ourselves, it should not
434
- # attempt to install them again, which is good, because we want to make
435
- # it Nix's responsibility. If it needs to install any dependencies
436
- # anyway (e.g. because the dependency parameters are
437
- # incomplete/incorrect), it fails.
438
- #
439
- # The other responsibilities of NPM are kept -- version checks, build
440
- # steps, postprocessing etc.
441
-
442
- export HOME=$TMPDIR
443
- cd "${ packageName } "
444
- runHook preRebuild
445
-
446
- ${ lib . optionalString bypassCache ''
447
- ${ lib . optionalString reconstructLock ''
448
- if [ -f package-lock.json ]
449
- then
450
- echo "WARNING: Reconstruct lock option enabled, but a lock file already exists!"
451
- echo "This will most likely result in version mismatches! We will remove the lock file and regenerate it!"
452
- rm package-lock.json
453
- else
454
- echo "No package-lock.json file found, reconstructing..."
455
- fi
422
+ # Pinpoint the versions of all dependencies to the ones that are actually being used
423
+ echo "pinpointing versions of dependencies..."
424
+ source $pinpointDependenciesScriptPath
425
+
426
+ # Patch the shebangs of the bundled modules to prevent them from
427
+ # calling executables outside the Nix store as much as possible
428
+ patchShebangs .
429
+
430
+ # Deploy the Node.js package by running npm install. Since the
431
+ # dependencies have been provided already by ourselves, it should not
432
+ # attempt to install them again, which is good, because we want to make
433
+ # it Nix's responsibility. If it needs to install any dependencies
434
+ # anyway (e.g. because the dependency parameters are
435
+ # incomplete/incorrect), it fails.
436
+ #
437
+ # The other responsibilities of NPM are kept -- version checks, build
438
+ # steps, postprocessing etc.
456
439
457
- node ${ reconstructPackageLock }
458
- '' }
440
+ export HOME=$TMPDIR
441
+ cd "${ packageName } "
442
+ runHook preRebuild
459
443
460
- node ${ addIntegrityFieldsScript }
461
- '' }
444
+ ${ lib . optionalString bypassCache ''
445
+ ${ lib . optionalString reconstructLock ''
446
+ if [ -f package-lock.json ]
447
+ then
448
+ echo "WARNING: Reconstruct lock option enabled, but a lock file already exists!"
449
+ echo "This will most likely result in version mismatches! We will remove the lock file and regenerate it!"
450
+ rm package-lock.json
451
+ else
452
+ echo "No package-lock.json file found, reconstructing..."
453
+ fi
462
454
463
- npm ${ forceOfflineFlag } --nodedir=${ nodeSources } ${ npmFlags } ${ lib . optionalString production "--production" } rebuild
455
+ node ${ reconstructPackageLock }
456
+ '' }
464
457
465
- runHook postRebuild
458
+ node ${ addIntegrityFieldsScript }
459
+ '' }
466
460
467
- if [ "'' ${dontNpmInstall-}" != "1" ]
468
- then
469
- # NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
470
- rm -f npm-shrinkwrap.json
461
+ npm ${ forceOfflineFlag } --nodedir=${ nodeSources } ${ npmFlags } ${ lib . optionalString production "--production" } rebuild
471
462
472
- npm ${ forceOfflineFlag } --nodedir=${ nodeSources } --no-bin-links --ignore-scripts ${ npmFlags } ${ lib . optionalString production "--production" } install
473
- fi
463
+ runHook postRebuild
464
+
465
+ if [ "'' ${dontNpmInstall-}" != "1" ]
466
+ then
467
+ # NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
468
+ rm -f npm-shrinkwrap.json
469
+
470
+ npm ${ forceOfflineFlag } --nodedir=${ nodeSources } --no-bin-links --ignore-scripts ${ npmFlags } ${ lib . optionalString production "--production" } install
471
+ fi
474
472
475
- # Link executables defined in package.json
476
- node ${ linkBinsScript }
473
+ # Link executables defined in package.json
474
+ node ${ linkBinsScript }
477
475
'' ;
478
476
479
477
# Builds and composes an NPM package including all its dependencies
480
478
buildNodePackage =
481
479
{ name
482
480
, packageName
483
481
, version ? null
484
- , dependencies ? [ ]
485
- , buildInputs ? [ ]
482
+ , dependencies ? [ ]
483
+ , buildInputs ? [ ]
486
484
, production ? true
487
485
, npmFlags ? ""
488
486
, dontNpmInstall ? false
492
490
, dontStrip ? true
493
491
, unpackPhase ? "true"
494
492
, buildPhase ? "true"
495
- , meta ? { }
496
- , ...
497
- } @args :
493
+ , meta ? { }
494
+ , ... } @args :
498
495
499
496
let
500
497
extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" "dontStrip" "dontNpmInstall" "preRebuild" "unpackPhase" "buildPhase" "meta" ] ;
572
569
, packageName
573
570
, version ? null
574
571
, src
575
- , dependencies ? [ ]
576
- , buildInputs ? [ ]
572
+ , dependencies ? [ ]
573
+ , buildInputs ? [ ]
577
574
, production ? true
578
575
, npmFlags ? ""
579
576
, dontNpmInstall ? false
@@ -582,70 +579,69 @@ let
582
579
, dontStrip ? true
583
580
, unpackPhase ? "true"
584
581
, buildPhase ? "true"
585
- , ...
586
- } @args :
582
+ , ... } @args :
587
583
588
584
let
589
585
extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" ] ;
590
586
in
591
- stdenv . mkDerivation ( {
592
- name = "node-dependencies-${ name } ${ if version == null then "" else "-${ version } " } " ;
587
+ stdenv . mkDerivation ( {
588
+ name = "node-dependencies-${ name } ${ if version == null then "" else "-${ version } " } " ;
593
589
594
- buildInputs = [ tarWrapper python nodejs ]
595
- ++ lib . optional ( stdenv . isLinux ) utillinux
596
- ++ lib . optional ( stdenv . isDarwin ) libtool
597
- ++ buildInputs ;
590
+ buildInputs = [ tarWrapper python nodejs ]
591
+ ++ lib . optional ( stdenv . isLinux ) utillinux
592
+ ++ lib . optional ( stdenv . isDarwin ) libtool
593
+ ++ buildInputs ;
598
594
599
- inherit dontStrip ; # Stripping may fail a build for some package deployments
600
- inherit dontNpmInstall unpackPhase buildPhase ;
595
+ inherit dontStrip ; # Stripping may fail a build for some package deployments
596
+ inherit dontNpmInstall unpackPhase buildPhase ;
601
597
602
- includeScript = includeDependencies { inherit dependencies ; } ;
603
- pinpointDependenciesScript = pinpointDependenciesOfPackage args ;
598
+ includeScript = includeDependencies { inherit dependencies ; } ;
599
+ pinpointDependenciesScript = pinpointDependenciesOfPackage args ;
604
600
605
- passAsFile = [ "includeScript" "pinpointDependenciesScript" ] ;
601
+ passAsFile = [ "includeScript" "pinpointDependenciesScript" ] ;
606
602
607
- installPhase = ''
608
- source ${ installPackage }
603
+ installPhase = ''
604
+ source ${ installPackage }
609
605
610
- mkdir -p $out/${ packageName }
611
- cd $out/${ packageName }
606
+ mkdir -p $out/${ packageName }
607
+ cd $out/${ packageName }
612
608
613
- source $includeScriptPath
609
+ source $includeScriptPath
614
610
615
- # Create fake package.json to make the npm commands work properly
616
- cp ${ src } /package.json .
617
- chmod 644 package.json
618
- ${ lib . optionalString bypassCache ''
619
- if [ -f ${ src } /package-lock.json ]
620
- then
621
- cp ${ src } /package-lock.json .
622
- chmod 644 package-lock.json
623
- fi
624
- '' }
611
+ # Create fake package.json to make the npm commands work properly
612
+ cp ${ src } /package.json .
613
+ chmod 644 package.json
614
+ ${ lib . optionalString bypassCache ''
615
+ if [ -f ${ src } /package-lock.json ]
616
+ then
617
+ cp ${ src } /package-lock.json .
618
+ chmod 644 package-lock.json
619
+ fi
620
+ '' }
625
621
626
- # Go to the parent folder to make sure that all packages are pinpointed
627
- cd ..
628
- ${ lib . optionalString ( builtins . substring 0 1 packageName == "@" ) "cd .." }
622
+ # Go to the parent folder to make sure that all packages are pinpointed
623
+ cd ..
624
+ ${ lib . optionalString ( builtins . substring 0 1 packageName == "@" ) "cd .." }
629
625
630
- ${ prepareAndInvokeNPM { inherit packageName bypassCache reconstructLock npmFlags production ; } }
626
+ ${ prepareAndInvokeNPM { inherit packageName bypassCache reconstructLock npmFlags production ; } }
631
627
632
- # Expose the executables that were installed
633
- cd ..
634
- ${ lib . optionalString ( builtins . substring 0 1 packageName == "@" ) "cd .." }
628
+ # Expose the executables that were installed
629
+ cd ..
630
+ ${ lib . optionalString ( builtins . substring 0 1 packageName == "@" ) "cd .." }
635
631
636
- mv ${ packageName } lib
637
- ln -s $out/lib/node_modules/.bin $out/bin
638
- '' ;
639
- } // extraArgs ) ;
632
+ mv ${ packageName } lib
633
+ ln -s $out/lib/node_modules/.bin $out/bin
634
+ '' ;
635
+ } // extraArgs ) ;
640
636
641
637
# Builds a development shell
642
638
buildNodeShell =
643
639
{ name
644
640
, packageName
645
641
, version ? null
646
642
, src
647
- , dependencies ? [ ]
648
- , buildInputs ? [ ]
643
+ , dependencies ? [ ]
644
+ , buildInputs ? [ ]
649
645
, production ? true
650
646
, npmFlags ? ""
651
647
, dontNpmInstall ? false
654
650
, dontStrip ? true
655
651
, unpackPhase ? "true"
656
652
, buildPhase ? "true"
657
- , ...
658
- } @args :
653
+ , ... } @args :
659
654
660
655
let
661
656
nodeDependencies = buildNodeDependencies args ;
677
672
678
673
# Provide the dependencies in a development shell through the NODE_PATH environment variable
679
674
inherit nodeDependencies ;
680
- shellHook = lib . optionalString ( dependencies != [ ] ) ''
675
+ shellHook = lib . optionalString ( dependencies != [ ] ) ''
681
676
export NODE_PATH=${ nodeDependencies } /lib/node_modules
682
677
export PATH="${ nodeDependencies } /bin:$PATH"
683
678
'' ;
0 commit comments