nix-env-selector icon indicating copy to clipboard operation
nix-env-selector copied to clipboard

[Feature]: Support Flake and nix develop

Open jmatsushita opened this issue 4 years ago • 12 comments

Is your feature request related to a problem? Please describe. Selecting a flake.nix file from the environment selector dropdown currently doesn't work (the "Applying environment..." status keeps spinning).

Describe the solution you'd like Compatibility with the newer nix develop shell.

Describe alternatives you've considered It's possible to create a shell.nix file that provides a compatibility layer, however I thin kit doesn't benefit from the performance improvements with nix develop:

(import (
  fetchTarball {
    url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
    sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
  src =  ./.;
}).shellNix

jmatsushita avatar Mar 13 '21 16:03 jmatsushita

Yes! This would be awesome! I just moved all my projects over to flakes, but this has been holding me back...

zyansheep avatar Apr 01 '21 22:04 zyansheep

What would be needed for this? I didn't look into how this extension works, but I believe a hacky solution would be using flake-compat as explained by the OP. It would basically be something like nix-build -E '(import (fetchTarball { /* flake-compat url and hash */ }) { src = ./.; }).shellNix' which I think the extension could use (again, I don't how the extension handles this).

90-008 avatar Sep 11 '21 10:09 90-008

Just to be clear, if I create a shell.nix with the content that @jmatsushita is proposing.. this should be working already? because it's not for me :-( Even if nix-shell shell.nix works.

edulix avatar Nov 03 '21 15:11 edulix

It works already if you create the shell.nix file and you select shell.nix with the Nix Env: Select Environment command.

jmatsushita avatar Nov 03 '21 16:11 jmatsushita

@arrterian I'd happily sponsor work that would result in this being implemented, do you think that could be arranged?

kubukoz avatar Dec 23 '21 00:12 kubukoz

@arrterian I'd happily sponsor work that would result in this being implemented, do you think that could be arranged?

I will pledge $20 worth of Monero if that means anything : )

zyansheep avatar Dec 24 '21 22:12 zyansheep

What would be needed for this?

Some notes:

Implementation wise, for covering nix develop I'd estimate the main work would be in src/main/ext/nix_env.cljs. https://github.com/arrterian/nix-env-selector/blob/472c4c4211f13f74ea27418e1ec30a706b809e5f/src/main/ext/nix_env.cljs, the actions.cljs then uses env/get-nix-env-async and env/set-current-env.

This file seems to roughly take the output of nix-shell --run "export", and parses the various declare -x name="nix-shell".

The nix flake equivalent nix develop at least provides nix print-dev-env --json which may be easier to make use of as a way to get environment variables from a flake devshell.

For covering nix shell nixpkgs#<pkg>, I guess it'd involve something like the equivalent of getting the path from nix build nixpkgs#gcc --no-link --json.

The setting name nixEnvSelector.nixFile doesn't make as much sense for flake devshells as it does for the nix-shell command.

rgoulter avatar Mar 02 '22 09:03 rgoulter

If anyone is looking for a workaround: https://github.com/srid/haskell-template uses flake-compat to provide a shell.nix that in turn uses flake.nix (as well as flake.lock). All of that can go away if this issue gets implemented. Using direnv is another option.

EDIT: It no longer uses flake-compat, and shell.nix simply contains: (builtins.getFlake ("git+file://" + toString ./.)).devShells.${builtins.currentSystem}.default

srid avatar May 29 '22 19:05 srid

For another workaround, consider using nix-direnv and its appropriate vscode extension too - it seems to work with use flake for me.

kubukoz avatar May 30 '22 13:05 kubukoz

@kubukoz wdym the appropriate vscode extension? The direnv one? So no need for this one?

mimoo avatar Jul 06 '22 18:07 mimoo

@mimoo yeah, I'm using this one: https://marketplace.visualstudio.com/items?itemName=Rubymaniac.vscode-direnv

kubukoz avatar Jul 06 '22 19:07 kubukoz

This one's being maintained actively: https://marketplace.visualstudio.com/items?itemName=mkhl.direnv

srid avatar Jul 06 '22 20:07 srid