Nix-shell setup errors

To set up a shell environment, I used the configuration from the nixosWiki:
My shell.nix:

# shell.nix
{ pkgs ? import <nixpkgs> {} }:
let
  my-python = pkgs.python3;
  python-with-my-packages = my-python.withPackages (p: with p; [
     black
  ]);
in
pkgs.mkShell {
  buildInputs = [
    python-with-my-packages
  ];
  shellHook = ''
    PYTHONPATH=${python-with-my-packages}/${python-with-my-packages.sitePackages}
  '';
}

However, I got these errors:

I am still learning nix, and I have no ideas how to fix this error. Thanks for any help!

nix: nix (Nix) 2.5pre20211007_844dd90
system: mac intel OS: 12.0.1

Thanks for the help!

1 Like

If you just want to use black, you can skip the withPackages thing, and just use black in your "buildInputs`.

Beyond that, since you say you’re new, I think that wiki entry is doing a few unecessary things:

  • I don’t think you need to set $PYTHONPATH at all at least from my own usage. That’s kind of the point of withPackages, setting up the python path correctly… The site-packages end up where Python considers its root to be, so they don’t need to be added to the path.

  • The other thing is that my-python variable. It’s completely superfluous.

Finally, I think the actual issue is this:

{ pkgs ? import <nixpkgs> {} }:

You seem to be using a nix that does flakes things. You’re not allowed to use the <nixpkgs> notation, because it is impure (I.e., the version of nixpkgs that points to depends on the environment the command runs in), and therefore prohibited by a flaky nix.

--show-trace will probably give you a nicer error.

I’m unsure why this would leak into nix-shell, which principally shouldn’t be complaining about that, but that’s what the error you’re getting refers to. It’s been a while since I used non-flake shells, but I think you can simply remove the ? import <nixpkgs> {} bit and it should work.

Your nix version is very new, considering that 2.4 released a week or two ago, is there any reason you’re using a version that recent?

1 Like

Thanks for the reply!
For the version I chose, I just followed some installations guides from the internet.

I tried remove the ? import <nixpkgs> {},
it give me this error

Also, If I just simply set up a shell environment without site-packages (I tried Python and R, none of them works) or packages installed system-wide, everything just works.
example shell.nix which works when called `nix-shell:

# shell.nix
{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = with pkgs; [
    python39
    #black
    #git
    #nodejs
    wget
  ];
  shellHook = ''
    echo "Welcome"
  '';
}

I put the above shell.nix file and the output of the full errors in this repo.

Thanks for the help!

Hmm, this is very odd.

Try the following:

  • Update your channels and rebuild your full install
  • Use --impure

If you need to use --impure, this might be worth raising as a bug against nix, I don’t think nix should be complaining about that path…

Again, any reason you’re using a dev version of nix? I don’t think anyone should be using that, unless they’re testing (in which case you’d probably know how to fix this :wink: ) or have a very specific use case.

1 Like

You have to use the command nix develop in order to run the shell, since you’re using a recent Nix version (with nix command instead of nix-command)

Also, add the following in your flake.nix

devShell."${system}" = import ./shell.nix { inherit pkgs; };
2 Likes

Given the “shell.nix” in your initial post, doing nix develop -f shell.nix works as expected for me.

❯ nix --version
nix (Nix) 2.4pre-rc1
1 Like

This is the one-line command to run it, thanks

Thanks, this works for me now. I need to use it with flake, and I also followed this flake wiki.

Thanks for everyone’s help!

Flakes, as well as the nix subcommands, are not stable yet, right? Why would nix-shell no longer work under those conditions, is this down to using an unstable version of nix?

Both nix-shell and nix develop -f shell.nix work for me with the “shell.nix” in the first message.

What nix version are you using? Maybe this is a regression, and we should open an issue against nix, while @Dezw downgrades to nix stable :slight_smile:

❯ nix-info
system: "x86_64-linux", multi-user?: yes, version: nix-env (Nix) 2.4pre-rc1

I think this may be some issues with nix pre2.5 or only happens in macOS.

I also tried the same shell.nix file in my NixOS machine (x86_64-linux, nix pre2.4) with nix-shell, everything works.