Sudo: nix-env: command not found

When doing darwin-rebuild --switch on a system, I get the error sudo: nix-env: command not found. What’s the proper way to fix this error? Thanks.

What happens if you run nix-env directly?

That works fine - it seems only sudo has an issue. Still means that darwin-rebuild doesn’t work though.

Does it fail if you manually run sudo nix-env?

If that fails, I’d check /etc/sudoers, I guess. I haven’t used them, but it has some settings that could either be discarding your user’s PATH (env_check and env_delete, maybe more?) or hard-setting it (secure_path).

Edit: I guess you could check on the PATH with something like sudo /bin/sh -c 'echo $PATH'

1 Like

I was experiencing the same issue, after a fresh multi-user install on a Ubuntu 22.04 VM.

nix-env works fine when I’m logged in as a regular user. It also works fine when I open a root prompt with sudo su -. But sudo nix-env yields

sudo: nix-env: command not found

@abathur it seems that your hunch was correct. My /etc/sudoers contained a secure_path option, which overrides the $PATH when using sudo. Commenting it out (probably not the correct long-term solution!) solves the issue.

1 Like