Setting up PATH for fish, bash, etc

If I define these environment variables to be picked up by bash and fish:

{
  environment.variables = {
    EDITOR = "hx";
    VISUAL = "hx";
    FZF_DEFAULT_COMMAND = "fd --type f --hidden --follow --exclude .git";
    NPM_CONFIG_PREFIX = "$HOME/.local/share/npm";
    PNPM_HOME = "$HOME/.local/share/pnpm";
    CARGO_HOME = "$HOME/.cargo";
  };
}

How can I then append/prepend the following elements to my PATH env var:

  • ${NPM_CONFIG_PREFIX}/bin
  • ${$PNPM_HOME}/bin
  • ${CARGO_HOME}/bin

I am currently using environment.shellInit and programs.fish.shellInit and duplicate the PATH construction logic, but I was wondering if there is a way to avoid doing that.

Thanks in advance,
Behrang

You simply don’t. Use the tools you have there from nixpkgs instead, package them if needed.

2 Likes

Sorry, I don’t know what you mean. Could you please elaborate?

Isn’t something like this possible:

{
  environment.variables = {
    EDITOR = "hx";
    VISUAL = "hx";
    FZF_DEFAULT_COMMAND = "fd --type f --hidden --follow --exclude .git";
    NPM_CONFIG_PREFIX = "$HOME/.local/share/npm";
    PNPM_HOME = "$HOME/.local/share/pnpm";
    CARGO_HOME = "$HOME/.cargo";

    PATH = [
      "$NPM_CONFIG_PREFIX/bin"
      "$PNPM_HOME/bin"
      "$CARGO_HOME/bin"
    ];
  };
}

Use shells per project like Declarative shell environments with shell.nix — nix.dev documentation. Especially for npm where a global env makes no sense, and unpatched binaries won’t work.

2 Likes

You probably want to add $CARGO_HOME/bin to your PATH because you use(d) cargo install to install software. This software will eventually break (dynamic linker) and you need to reinstall it.

To solve that, you instead package this software and add it to your environment.systemPackages/users.users.<name>.packages/home.packages, depending on your preferences.

3 Likes

What the others are telling you is correct… however, if you aren’t going to take their advice, path-manipulating logic should go in environment.extraInit, not shellInit.

4 Likes