Python virtualenv not working inside FHSUserEnv?

As per the Python wiki page, I’ve tried running this shell:

{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSUserEnv {
  name = "my-python-env";
  targetPkgs = pkgs: (with pkgs; [
    python3
    python3Packages.pip
    python3Packages.virtualenv
  ]);
  runScript = ''
    bash
  '';
}).env

But using virtualenv in that shell gives me this error message:

my-python-env-chrootenv:user@nixos-laptop:/tmp$ virtualenv venv
/nix/store/4s0h5aawbap3xhldxhcijvl26751qrjr-python3-3.8.9/bin/python3: 
symbol lookup error: /usr/lib/libc.so.6: 
undefined symbol: _dl_audit_symbind_alt, version GLIBC_PRIVATE

Really what I want is a FHS environment that has some python packages available. I’ve tried directly adding python3Packages.... to the targetPkgs, but those are not made available to python inside the FHS env for some reason. So this is my next attempt where I just want to make a virtualenv and use pip to install the packages.

You want (python3.withPackages (ps: with ps; [ pip virtualenv ])). You need to “make a python with those packages”, not just “install those packages”. I don’t know if virtualenv will work as desired even then, though. The nix-specific stuff might get in the way.

1 Like

That still gives the same error message for the virtualenv, but it does work for my original goal of creating a FHS env with python and the packages I need.

Hi, I’ve been using buildFHSUserEnv as well (although not anymore atm), and have run into similar things. What it’s basically telling you is that there is a glibc version mismatch between python3-3.8.9/bin/python3 and the glibc version inside your fhs. With regular nix/without the fhs, this would not be a problem, because python3 would just use its corresponding glibc version from the nix store. But inside the fhs only one glibc version can exist, so this one has to be compatible with python3. At least that is my understanding of the issue.

Now, how to solve this? Well, try taking a look at buildFHSUserEnv itself, that might already be helpful. I think glibc is added as a package to the fhs there somewhere. How I usually “solved” these cases is by making sure that all the different versions of nixpkgs in play (for example the one imported by the first line in your script and the one your system is based on) are in sync. Try also checking out which version of glibc your python3 wants, and which version gets installed by buildFHSUserEnv.

That’s all I can help with, good luck in solving the issue!

1 Like

@Noughtmare the same page you linked mentions that python 3.6+ includes virtualenv. I’ve tried using it without python3Packages.pip and python3Packages.virtualenv, and got jupyter + a bunch of libs running from the first shot.

Whole flake.nix:

see contents
{
  inputs.flake-utils.url = "github:numtide/flake-utils";
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ] (system:
      let
        pkgs = import nixpkgs { system = system; };
        fhs = pkgs.buildFHSUserEnv {
          name = "FHSforpython";
          targetPkgs = p: (with p; [
            python310
          ]);
          runScript = "${pkgs.fish}/bin/fish";
        };
      in
      {
        devShell = fhs.env;
      }
    );
}

Not clearly enough for me… I see only:

Starting from Python 3 virtual environment is natively supported.

and

Python 3.6+: python3 -m venv is the way to go.

But what is the relation between “virtual environment” and the virtualenv executable and the python -m venv command? Are they all the same thing or are they different things?

Also, right below it they show an example with Python 3.9 that uses virtualenv and not python -m venv, so are they not following their own advice? Or does the FHS have special requirements?

I agree that the docs should be updated.

But what is the relation between “virtual environment” and the virtualenv executable and the python -m venv command? Are they all the same thing or are they different things?

virtualenv would be a standalone binary and python -m venv is the one bundled with python. They are the same thing, the latter is just bundled with python itself.

1 Like