Skip to content

Commit 617ac92

Browse files
authored
Merge pull request #157 from mlabs-haskell/jared/flake-typescript-nix-improvements
Improved flake-typescript.nix to put packages in `npm`s cache before installing them
2 parents 8e40c0b + c67ef66 commit 617ac92

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

extras/flake-typescript.nix

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ pkgs:
88
# for the the extra dependencies (not included in the `package.json`) for
99
# `node` to execute. This will _not_ install the "transitive" dependencies.
1010
#
11-
# Loosely, this will `npm install` each of the tarballs in order so its
12-
# important that
13-
#
14-
# - The dependencies are sorted topologically i.e., each tarball should
15-
# _only_ depend on packages before it in the list.
11+
# Loosely, this will (in the order given) copy each tarball to a local
12+
# directory, call `npm cache` on the tarball, and finally call `npm install`.
1613
#
1714
# For example, if one wanted to include `typescript` as a dependency, then
1815
# one could have
@@ -57,6 +54,13 @@ let
5754
}:
5855
pkgs.runCommand (name + "-node2nix") { buildInputs = [ pkgs.node2nix nodejs ]; }
5956
''
57+
# For some reason, npm likes to call `mkdir $HOME` (which is not
58+
# writeable when building derivations on nix), so we change `HOME` to a
59+
# writable directory
60+
mkdir -p $TMPDIR/home
61+
export HOME=$TMPDIR/home
62+
63+
6064
mkdir -p "$out"
6165
6266
cd "$out"
@@ -82,9 +86,16 @@ let
8286
chmod +777 package.json
8387
chmod +777 package-lock.json
8488
85-
# `.nix-node-deps/` is the directory to save the tarball
86-
# dependencies from `nix`.
87-
mkdir .nix-node-deps/
89+
mkdir -p $TMPDIR/cache
90+
npm config set cache $TMPDIR/cache
91+
92+
# Create a directory to store the node dependencies provided by nix
93+
NIX_NODE_DEPS_PATH=.nix-node-deps/
94+
mkdir -p "$NIX_NODE_DEPS_PATH"
95+
96+
# Helper function to convert a package path to the path in
97+
# `NIX_NODE_DEPS_PATH`
98+
pkgPathToNixNodeDepsPath( ) { echo "$NIX_NODE_DEPS_PATH/$(basename "$1")"; }
8899
89100
${ if dependencies == [] then "" else
90101
''
@@ -94,18 +105,22 @@ let
94105
# Copying all `dependencies` into `.nix-nodes-deps/` i.e.,
95106
# we run:
96107
# ```
97-
# echo "Copying <dependency1>"
98-
# cp Copying <dependency1> .nix-node-deps/
99-
# echo "Copying <dependency2>"
100-
# cp Copying <dependency2> .nix-node-deps/
108+
# echo "Copying <dependency1> and adding it to npm's cache"
109+
# cp <dependency1> "$(pkgPathToNixNodeDepsPath <dependency1>)"
110+
# npm cache add <dependency1>
111+
# echo "Copying <dependency2> and adding it to npm's cache"
112+
# cp <dependency2> "$(pkgPathToNixNodeDepsPath <dependency2>)"
113+
# npm cache add <dependency2>
101114
# ...
102-
# echo "Copying <dependencyN>"
103-
# cp Copying <dependencyN> .nix-node-deps/
115+
# echo "Copying <dependencyN> and adding it to npm's cache"
116+
# cp <dependencyN> "$(pkgPathToNixNodeDepsPath <dependencyN>)"
117+
# npm cache add <dependencyN>
104118
# ```
105119
${builtins.concatStringsSep "\n" (builtins.map (pkgPath:
106120
''
107-
echo "Copying ${pkgPath}..."
108-
cp "${pkgPath}" .nix-node-deps/
121+
echo "Copying ${pkgPath} and adding it to npm's cache..."
122+
cp ${pkgPath} "$(pkgPathToNixNodeDepsPath "${pkgPath}")"
123+
npm cache add --loglevel=verbose "$(pkgPathToNixNodeDepsPath "${pkgPath}")"
109124
''
110125
) dependencies)}
111126
@@ -114,7 +129,7 @@ let
114129
# npm install --save --package-lock-only <dependency1> <dependency2> ... <dependencyN>
115130
# ```
116131
echo 'Running `npm install`...'
117-
HOME=$TMPDIR npm install --save --package-lock-only ${builtins.concatStringsSep " " (builtins.map (pkgPath: ''".nix-node-deps/$(basename "${pkgPath}")"'') dependencies) }
132+
npm install --loglevel=verbose --save --package-lock-only ${builtins.concatStringsSep " " (builtins.map (pkgPath: ''$(pkgPathToNixNodeDepsPath "${pkgPath}")'') dependencies) }
118133
''
119134
}
120135

0 commit comments

Comments
 (0)