Python: how to expose a dependency's bin?

I’ve packaged a CLI tool written in python, with a dependency on argcomplete. Enabling autocompletion requires running the following in the shell (e.g. in bashrc or similar):

eval "$(register-python-argcomplete my-cli)"

Though my package works (the CLI can be used), the “register-python-argcomplete” command (provided by argcomplete) is not installed in the user-environment. I can remedy this by installing argcomplete separately.

I’m using pypi2nix. My default.nix lists argcomplete in its propagatedBuilldInputs, but I guess propagating the argcomplete library to my CLI’s runtime environment is not the same as propagating its executables to the user’s environment.

Is there a way I could propagate this command to the user environment? Would that be bad practice?
I’m also considering adding a wrapper script in my $out/bin along the lines of:

/path/to/register-python-argcomplete my-cli

That would avoid interfering with any existing installation of argcomplete in the user’s environment. I’m not sure how I’d implement this though.

The propagatedUserEnvPkgs attribute takes a list of packages which will be added to an environment when installing the package that has it defined. In Nixpkgs we typically avoid using this but this looks like a valid case.

I find the wrapper a nice solution as well. As a postFixup you could use makeWrapper to create the wrapper.

1 Like

Thanks, that was too easy! Adding argcomplete to propagatedUserEnvPkgs did the trick, but I decided to go with the makeWrapper solution instead.

postFixup = ''
  makeWrapper \
    ${argcomplete}/bin/register-python-argcomplete \
    $out/bin/register-my-cli-argcomplete \
    --add-flags my-cli \
    --argv0 '$0'

With eval "$(register-my-cli-argcomplete)" in my bashrc, everything works.