cabal2nix icon indicating copy to clipboard operation
cabal2nix copied to clipboard

Automatically generate bash completion files for executables using optparse-applicative

Open peti opened this issue 8 years ago • 5 comments

All executables that use optparse-applicative for their command-line parsing can dump a bash completion file for themselves. We use that for stack and cabal2nix and others already, but we configure the appropriate postInstall hook manually, i.e. in https://github.com/NixOS/cabal2nix/commit/d281a6023f1e0fdfd98796bd0ba3f7764c03a457. This should be something that

  • the generic build does automatically (when an appropriate flag is enabled) or
  • cabal2nix should generate an appropriate hook automatically when the dependency is detected.

peti avatar Sep 02 '15 11:09 peti

:+1: In the meanwhile could you describe what need to be added in the default.nix or shell.nix to enable completion when using nix-shell ? That would be very handy !

PierreR avatar Mar 21 '16 22:03 PierreR

The hackage2nix expression at https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/tools/haskell/cabal2nix/hackage2nix.nix#L27-L31 uses the following code:

  postInstall = ''
    exe=$out/bin/${pname}
    mkdir -p $out/share/bash-completion/completions
    $exe --bash-completion-script $exe >$out/share/bash-completion/completions/${pname}
  '';

peti avatar Mar 22 '16 08:03 peti

Thanks for the input.

It does not work and my guest is that the culprit might be the $out param. echo $out in the shell does print some path but the path does not seem to match any directory. Am I supposed to use another variable ?

PierreR avatar Mar 22 '16 21:03 PierreR

Well, nix-shell does neither compile nor install anything -- it just provides an environment in which you can compile and/or install things --, thus the postInstall hook is never executed by nix-shell. If you want to get bash completions for projects you develop in nix-shell, then you'll have to generate them manually.

peti avatar Mar 23 '16 09:03 peti

Thanks for helping. I will use shellHook for now to source the completion file.

PierreR avatar Mar 23 '16 09:03 PierreR