yasu
March 1, 2019, 9:38pm
1
Hi!
I want to play with some ECMAScript programs available in NPM (I don’t know the exact terminology, let me list node.js, package.json, etc).
What is the best way to do this in NixOS? I distrust NPM so that’s why I am trying to use NixOS as a clean environment to protect myself.
Cheers,
Yasu
tinix
March 1, 2019, 10:52pm
2
1 Like
yasu
March 1, 2019, 11:03pm
3
Thank you, well, I am not quite familiar with NixOS anyway, so let me start with Haskell + NixOS. Hopefully, Haskell is “mainstream” in NixOS so I will get more insights into how to use Nix packaging systems when other people use other managers such as stack.
yasu
March 1, 2019, 11:04pm
4
Then I will get more insights into how to use Nix instead of NPM?
There is npm2nix and yarn2nix . You create package.json and then use one of these to generate nix expression.
Let me know if you’re still hung up on this. I got ECMAScript set up a few weeks back for some WebGL stuff. I’d be happy to help but don’t want to do a whole write-up if you’ve already resolved this.
1 Like
hello .
how use nix-shell for install npm packages and why need use npm2nix.what is diffrence .
i use node2nix for install packages but need edit . no easy way in nix/nixos install node,python ,… packages/libs
with nix-shell, you get to have access to the internet, so you could just do something like:
nix-shell -p nodejs --run "npm install"
and it will work fine, as you’re just using nix to bootstrap the dev environment
that’s much more invovled, and you’ll probably have to sue node2nix to solve the issues.
For python, you can take a look at nixpkgs/doc/languages-frameworks/python.section.md at 5622b6b6feb669edc227aaf000413d5b593d4051 · NixOS/nixpkgs · GitHub if your python env is static.
Or you can look at nixpkgs/doc/languages-frameworks/python.section.md at 5622b6b6feb669edc227aaf000413d5b593d4051 · NixOS/nixpkgs · GitHub if your want to use a venv like python dev env
2 Likes
mboyea
December 2, 2024, 8:02pm
9
There is another option. Instead of pkgs.node2nix, you can use pkgs.buildNpmPackage (or the yarn equivalent). I chose to implement buildNpmPackage, so I’ll drop an example here.
shell.nix :
{
pkgs ? import <nixpkgs> {}
}: pkgs.mkShell {
packages = [
pkgs.nodejs
pkgs.prefetch-npm-deps # see server.nix
];
}
server.nix :
{
pkgs ? import <nixpkgs> {},
name ? "test",
version ? "0.0.0"
} : pkgs.buildNpmPackage rec {
pname = "${name}-server";
inherit version;
src = ./.;
# Generate a new hash using:
# nix-shell
# npm i --package-lock-only
# prefetch-npm-deps package-lock.json
npmDepsHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
npmBuildScript = "build";
installPhase = ''
mkdir -p $out/bin $out/lib
cp -rv build $out/lib
cp -rv package.json $out/lib
cat > $out/bin/${pname} << EOF
#!/bin/sh
${pkgs.lib.getExe pkgs.nodejs} $out/lib/build
EOF
chmod +x $out/bin/${pname}
'';
meta.mainProgram = "${pname}";
}
This example expects npm run build to generate the directory ./build. It requires that you manually update npmDepsHash if your dependencies change.
See lacuna/modules/sveltekit at main · mboyea/lacuna · GitHub for the full example of wrapping nix around an existing NPM project.
In case you instead wish to implement this manually (without node2nix or buildNpmPackage), I found this great article: Nicolas Mattia – Lockfile trick: Package an npm project with Nix in 20 lines
2 Likes