Why tools installed with nix-shell in Dockerfile do not appear in $PATH

For the following Dockerfile

FROM nixos/nix

RUN nix-shell --packages bash && bash

docker build ends in exception The command '/bin/sh -c nix-shell --packages bash && bash' returned a non-zero code: 127. The only way to run bash I could figured out is to use absolte path:

FROM nixos/nix

RUN nix-shell --packages bash && `nix-build --no-out-link '<nixpkgs>' -A bash.out`/bin/bash

I’ll be grateful if anyone could explain why nix-shell -p bash in Docker doesn’t add bash as usual to $PATH.

nix-shell -p bash will create an environment with bash in PATH, and then exit it, such that && bash can be executed.

To actually run a command within the environment, you need to use --run or --cmd depending on the required semantics. One allows for interactive input, the other doesn’t.

1 Like

Thank you, @NobbZ!

I guess by --cmd you meant --command.

Maybe, I’m used to new style commands with short form flags, where it is simply -c

1 Like

In order to have Bash on the path, you can use nix-env.

FROM nixos/nix

RUN nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs
RUN nix-channel --update

RUN nix-env -f '<nixpkgs>' -i bash && bash -c 'echo "hello"'

Thank you, @austin. I don’t think nix-env is appropriate in my case because the only demand for bash and some other tools is to build gearmand on NixOS docker image. Not sure the image I proposed is the best way to do that but it works.

The best way would probably to have a proper derivation set up in nixpkgs or an overlay or even in the repo directly.


@palik why are you building gearmand within a docker container? Is this a requirement of the gearman tooling/testing system?

This will leave you with a pretty fat docker image that contains all the build tools for gearman which will never be used again but (due to the way docker layers work) will be transferred along with gearmand everywhere this docker file is deployed

I second @NobbZ it would be better to create a derivation to build gearmand within Nix and create a docker image that contains only gearmand. If you contribute your derivation to nixpkgs, you can even have nixery.dev build and serve you an optimised docker image.