-
-
Save ar4s-eth/6bc9ea4c0ed37b2eaacb810c73bf3f9d to your computer and use it in GitHub Desktop.
Automate creating nix-shell environments with node2nix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Exit on any failure | |
set -e | |
if [ "$#" -lt 1 ]; then | |
echo "Expected at least one parameter (npm package name)" | |
exit 1 | |
fi | |
NPM_PACKAGE_NAME="$1" | |
NPM_PACKAGE_VERSION="${2:-latest}" | |
echo "creating env for $NPM_PACKAGE_NAME:$NPM_PACKAGE_VERSION" | |
function fail_if_exists () { | |
local FILE_NAME="$1" | |
if [ -f "$FILE" ]; then | |
echo "$FILE exists. Aborting to avoid overwriting it. Remove and retry if that's your intention" | |
exit 1 | |
fi | |
} | |
fail_if_exists "default.nix" | |
fail_if_exists "node-env.nix" | |
fail_if_exists "node-packages.nix" | |
fail_if_exists "generated.nix" | |
cat <<EOT >> node2nix.nix | |
with import <nixpkgs> {}; | |
stdenv.mkDerivation rec { | |
name = "node2nix"; | |
buildInputs = [ nodePackages.node2nix ]; | |
} | |
EOT | |
cat <<EOT >> package.json | |
{ | |
"name": "$NPM_PACKAGE_NAME", | |
"version": "$NPM_PACKAGE_VERSION", | |
"devDependencies": { | |
"$NPM_PACKAGE_NAME": "$NPM_PACKAGE_VERSION" | |
} | |
} | |
EOT | |
# Running it will create: default.nix, node-env.nix and node-package.nix | |
nix-shell node2nix.nix --run "node2nix --development" | |
mv default.nix generated.nix | |
cat <<EOT >> default.nix | |
let | |
gen = import ./generated.nix; | |
in (gen { }).shell | |
EOT | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment