Aliases defined in `nix-shell`'s `shellHook` work with `--command` but not with `--run`

Given the following shell.nix

with import <nixpkgs> {};

pkgs.mkShell {
  pname = "just-testing";

  shellHook = ''
    echo AAAAAAAAAAAAAAAAAAA
    alias doit='echo DOING IT'
    echo BBBBBBBBBBBBBBBBBBB
    doit
    echo CCCCCCCCCCCCCCCCCCC
  '';

}

nix-shell --run 'echo ZZZZZZZZZZZZZ' gives

AAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBB
/nix/store/n4qv0ad1rg8rafppb4dh59n080m94bxy-stdenv-linux/setup: line 84: doit: command not found
CCCCCCCCCCCCCCCCCCC
ZZZZZZZZZZZZZ

while nix-shell --command 'echo ZZZZZZZZZZZZZ' gives

AAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBB
DOING IT
CCCCCCCCCCCCCCCCCCC
ZZZZZZZZZZZZZ

In other words, changing from --command to --run makes the alias defined in the shellHook disappear, (even though the echos indicate that the shellHook was executed).

What is the reason for this?

1 Like

Bash does not expand aliases when in non-interactive mode. That’s why you see the difference in behaviour

1 Like