Nix Shell with Micromamba and FHS

I have a shell.nix file for activating a FHS user env in order to work with micromamba as describe here

However, when I run nix-shell I’m getting this error message:

/etc/profile: line 151: complete: command not found

I think it’s caused by this line:

eval "$(micromamba shell hook -s bash)"

Is it because I don’t have a bash shell when running nix-shell?

@mausch

Thank you

1 Like

See (micro)mamba activation support · Issue #321 · cachix/devenv · GitHub

1 Like

I just ran into this issue setting up a new Python environment.
It’s weird because I’ve been using micromamba+nix for quite some time now (I wrote the micromamba package) and haven’t seen this until now.

Not sure if it’s because micromamba changed something in the generated script in some recent version or if something’s different because this new env I’m setting up uses flakes :man_shrugging:

Anyway the fix for me was using eval "$(micromamba shell hook --shell=posix)"

Let me know if that works for you.

Thank you for your answer. Unfortunately, that doesn’t work for me. I don’t know, maybe it’s because I’m using fish as my default shell. I will try it with flakes. Maybe it makes a difference.

The flake approach with posix works for me, too.

Hi I’m having the same issue. I also use fish as shell and set up dash as a replacement for bash according to the wiki. Since I’m quite new to Nix I’m having trouble setting up an environment using a flake.
My flake looks like this:

{
description = “Flake to set up python environment with micromamba”;

inputs = {
nixpkgs.url = “github:NixOS/nixpkgs/nixos-unstable”;
};

outputs = { self, nixpkgs, … }:
let
system = “x86_64-linux”;
pkgs = import nixpkgs {
inherit system;
};

            fhs = pkgs.buildFHSUserEnv {
                    name = "my-fhs-environment";

                    targetPkgs = _: [
                            pkgs.micromamba
                    ];

            profile = ''
                    set -e
                    eval "$(micromamba shell hook --shell=posix)"
                    export MAMBA_ROOT_PREFIX=${builtins.getEnv "PWD"}/.mamba
                    micromamba create -q -n my-environment
                    micromamba activate my-environment
                    micromamba install --yes -f conda-requirements.txt -c conda-forge
                    set +e
            '';
            };

    in {
            fhs.env
    };

}

But when executing it with nix-shell python-flake.nix it tells me there is an unexpected }.
I have checked the flake now multiple times and as far as I know there is no wrong {}. Am I missing some technicality here?

I have my system configured via a flake as well and it works fine. But there I did a flake init first, but executing it in my dev folder I get:

DBI connect(‘dbname=/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite’,’’,…) failed: unable to open database file at /run/current-system/sw/bin/command-not-found line 13.
cannot open database `/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite’ at /run/current-system/sw/bin/command-not-found line 13.

I hope you can help me out here. Setting up the environment is the last thing I need to do to switch over to NixOS.

Thank you and sorry for the long post.

Edit: I don’t know why only the second half of the flake is formatted correctly, sorry for the inconvenience.

You are missing an assignnent here. Though then you were still not following the flake output schema correctly.

What you probably want is this instead:

in {
  devShells.x86_64-linux.default = fhs.env;
};

Though you are still using builtins.getEnv then which is “impure” and therefore not allowed in flakes.

You are running a command that does not exist in PATH and you also have not set up the command-not-found database. What has been the command you ran? If the command was literally flake init, then you need to use nix flake init instead! Anyway. That is rarely necessary and does not do any magic setup. It just gives you an empty flake.nix.

Because it was intended by at least 4 spaces or a tab. In general you might want to prefer markdown fenced codeblocks:

```nix
{} # this is nix code
```

PS: If you have follow up questions, please open another thread. This is marked as solved already, and an ongoing discussion here might be missed by people who filter “solved” threads.

Hi, I had the same problem, and posted solution did not work for me.
Good news: I found a workaround, which was to “patch” the micromamba shell hook using sed to comment out the lines using complete builtin before evaluation. Now nix-shell seems to work for me, and shell completion seems to work too (not sure what those now-commented-out lines did).

This is probably not a super stable solution, but nothing else I could find would fix this! By the way, the root of the issue, as I understand it, has to do with the profile = ''...'' commands running with bash rather than bashInteractive (bash-interactive). I found when I added echo $BASH to the profile = ''...'' section of this shell.nix, it would point to a bash binary in the nix store, rather than bash-interactive. I tried for a few hours to figure out how to switch the shell used for the profile commands, but had no luck (adding pkgs.bashInteractive to targetPkgs did not work). If anyone has some advice for where to report this for a proper upstream fix, that would be much appreciated.

Anyway, here’s my working nix.shell:

{ pkgs ? import <nixpkgs> {}}:
let
  fhs = pkgs.buildFHSUserEnv {
    name = "example-project-environment";

    targetPkgs = _: [
      pkgs.micromamba
    ];

    profile = ''
      set -e
      export MAMBA_ROOT_PREFIX=${builtins.getEnv "PWD"}/.mamba
      eval "$(micromamba shell hook --shell=bash | sed 's/complete / # complete/g')"
      micromamba create --yes -q -n my-mamba-environment
      micromamba activate my-mamba-environment
      micromamba install --yes -f conda-requirements.txt -c conda-forge
      set +e
    '';


  };
in fhs.env