arrterian / nix-env-selector

Allows switch environment for Visual Studio Code using Nix Package Manager.
MIT License
220 stars 29 forks source link

nix-env-selector does not work with flakes #87

Open samuela opened 8 months ago

samuela commented 8 months ago

Describe the bug This bug manifests with similar symptoms as in https://github.com/arrterian/nix-env-selector/issues/56... specifically stuck in the "Applying environment..." state, but AFAICT has a different root cause and I am therefore opening a separate issue.

When selecting a flake.nix from the nix-env-selector drop-down, the error

[Extension Host] Error: Command failed: nix-shell "/home/skainswo/dev/sshenanigans/flake.nix" --run export
error: nix-shell requires a single derivation
Try 'nix-shell --help' for more information.

    at ChildProcess.exithandler (node:child_process:420:12)
    at ChildProcess.emit (node:events:513:28)
    at maybeClose (node:internal/child_process:1091:16)
    at Socket.<anonymous> (node:internal/child_process:449:11)
    at Socket.emit (node:events:513:28)
    at Pipe.<anonymous> (node:net:322:12)

is shown in the developer console, and the tray icon stays in the "Applying environment..." spinner forever.

To Reproduce Steps to reproduce the behavior:

  1. Go to a project with a flake. For example, I am currently attempting to use nix-env-selector with https://github.com/samuela/sshenanigans.
  2. Run Nix-Env: Select environment... and select flake.nix from the drop down.
  3. Observe the error and infinity spinner......

Expected behavior The environment equivalent to nix develop to be applied.

Screenshots n/a

Environment:

Additional context n/a

nyabinary commented 8 months ago

Same here

MichelleGranat commented 7 months ago

Any progress?

nyabinary commented 7 months ago

Any progress?

Use direnv vscode extension instead :3 works for me

acelinkio commented 6 months ago

Would greatly appreciate if this project could be extended to support flakes as well. Flakes have done a great job at increasing Nix's accessibility!

Would like to avoid adding another tool to the mix to setup vscode.

scaxyz commented 4 months ago

What works for me: I either keep a my old shell.nix file around and use devShells.default = import ./shell.nix { inherit pkgs; }; in the flake.nix file.

Or I create a shell.nix file with (builtins.getFlake ("git+file://" + toString ./.)).devShells.${builtins.currentSystem}.default as content.

And then just use the shell.nix as normal with the vscode extention. (Credits: https://discourse.nixos.org/t/how-to-fetch-flake-in-shell-nix/39587/2 and https://github.com/SlimeVR/SlimeVR-Server/blob/d9955d10e3f055491cea4d6a7e7edef0788235f2/shell.nix#L2)

OlivierLDff commented 3 months ago

My approach has been to use flake-compat

shell.nix:

(import
  (
    let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
    fetchTarball {
      url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
      sha256 = lock.nodes.flake-compat.locked.narHash;
    }
  )
  { src = ./.; }
).shellNix
HendrikHuebner commented 3 months ago

Any progress?

Use direnv vscode extension instead :3 works for me

@nyabinary Can you explain how to use it? I've tried creating an envrc with just "nix develop" inside. This has been kind of buggy. WHen I try to hook it to my shell it evaluates the flake and drops me into the devshell, but then it throws an error saying its taking too long. Once I press Ctrl+C it continues to work. I think the problem is that it basically waits for "nix develop" to finish, meaning for me to exit the shell. Could you post your envrc?

OlivierLDff commented 1 month ago

I gave a shot at the direnv extension, works perfectly.

@HendrikHuebner here is how I managed.

Content of .envrc (taken from here

#!/bin/sh

#If already set (e.g. in github actions); use that instead
if [ -z "$GITHUB_TOKEN" ]; then
  nix run nixpkgs#gh auth status || nix run nixpkgs#gh auth login
  GITHUB_TOKEN="$(nix run nixpkgs#gh auth token)"
  export GITHUB_TOKEN
fi

NIX_CONFIG="access-tokens = github.com=$GITHUB_TOKEN"
export NIX_CONFIG
use flake

Because my flake use private repo.

If you have a basic flake, you can simply use .envrc:

use flake