svanderburg / node2nix

Generate Nix expressions to build NPM packages
MIT License
525 stars 100 forks source link

Development: how do I run npm scripts? #241

Open thomasheartman opened 3 years ago

thomasheartman commented 3 years ago

Hi 🙋🏾

I was pointed to this package a little while ago and it successfully solves some install issues I've had with some npm packages. However, I'm not sure I understand how to use it, especially for development.

When working with node projects, I'm used to using the various npm scripts (npm start, npm run test, ...) to control the application, but I can't figure out how to do that with node2nix. I can't find any mention of this in the readme. Is this not supported? If not: what's the intended workflow for development?

Thanks! 😄

o1lo01ol1o commented 3 years ago

Have you tried entering the nix shell for the node2nix derivation in question with nix-shell -A shell and then running the defined npm run foo? nix-shell should give you access to the (npm etc) environment defined by default.nix

thomasheartman commented 3 years ago

Hi! Thanks for the response 😄 I think you're right in what you're saying.

I think I gave up last time, but recently had this again. There were a few gotchas I had to get around:

  1. You can't have a shell.nix file in the same directory, because it will try to enter whatever environment you have there instead, which probably is not the right one. Figuring this out isn't easy unless you know what you're doing. You could probably get around this, but I can never remember what the flags are for selecting a different file.
  2. To be able to use the node modules, you have to symlink the result to node_modules. This is mentioned in the readme, but kinda hard to find.

I recently tried this again (when I found out the above), but it still didn't work. I can't remember what went wrong, but in the end it worked to just npm install them as you would on any other system, so I went with that 🤷🏾

o1lo01ol1o commented 3 years ago

nix-shell -A shell should load the "shell" attribute defined by ./default.nix. Otherwise, yes, you'll need something like

# Setting up the node environment:
ln -s ${nodePkgs}/lib/node_modules ./node_modules
export PATH="${nodePkgs}/bin:$PATH"

in the installPhase of your top-level derivation so that nix puts symlinks into the environment and adds the /bin to the PATH.

On Wed, Aug 25, 2021 at 1:36 PM Thomas Heartman @.***> wrote:

Hi! Thanks for the response 😄 I think you're right in what you're saying.

I think I gave up last time, but recently had this again. There were a few gotchas I had to get around:

  1. You can't have a shell.nix file in the same directory, because it will try to enter whatever environment you have there instead, which probably is not the right one. Figuring this out isn't easy unless you know what you're doing. You could probably get around this, but I can never remember what the flags are for selecting a different file.
  2. To be able to use the node modules, you have to symlink the result to node_modules. This is mentioned in the readme, but kinda hard to find.

I recently tried this again (when I found out the above), but it still didn't work. I can't remember what went wrong, but in the end it worked to just npm install them as you would on any other system, so I went with that 🤷🏾

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/svanderburg/node2nix/issues/241#issuecomment-905463255, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB5DC5LXKSFEWRW2URDDGJ3T6TPVVANCNFSM47PUROVQ .

-- 328 Plymouth St Brooklyn, NY 11201

617 990 7876

thomasheartman commented 3 years ago

nix-shell -A shell should load the "shell" attribute defined by `./default.nix

Yes, but if I understand correctly, if you have a shell.nix file in the same directory, then that'll always take precedence.

Otherwise, yes, you'll need something like

# Setting up the node environment:
ln -s ${nodePkgs}/lib/node_modules ./node_modules
export PATH="${nodePkgs}/bin:$PATH"

in the installPhase of your top-level derivation so that nix puts symlinks into the environment and adds the /bin to the PATH.

I'm sorry, I don't quite follow. What's the top-level derivation here? Actually, more importantly: can you auto-create the symlink when entering the shell defined in default.nix? As mentioned in the readme, you'll have to symlink the $NODE_PATH to node_modules for a lot of build tools. It would be swell if that could be done automatically. Do you know if that's possible?