Add shell completions to nix2 cli #1043

Open
opened 2025-11-20 16:43:21 +00:00 by llakala · 0 comments

I've been trying to lower my reliance on nix3 commands as of late, but one of the things I've been missing is shell completions. Eelco added completions in the original flakes PR (commit seen here). From what I can tell, the completions logic remains minimal. As Lix is planning on bringing Flakes out of core, it'd be nice if nix2 received quality-of-life improvements like this.

Describe alternatives you've considered

I was able to achieve a minimal level of completions for nix-shell -p in Fish, by going through the existing flake infrastructure.

function _nixpkgs_completions
    # Lie to nix and pretend we're generating the flakey completions
    set -l current_token (commandline --current-token)
    set -l old_args (env NIX_GET_COMPLETIONS=2 nix shell "nixpkgs#$current_token")

    for arg in $old_args
        # nix gives the completions in the form of: "nixpkgs#foo"
        # We need to get just the foo part.
        # TODO: is there a faster way to do this?
        set -a new_args (string sub --start=9 $arg)
    end

    string collect -- $new_args[2..-1]
end

complete -c nix-shell \
    -x -s p -l packages \
    -a '(_nixpkgs_completions)'

Obviously this is terrible and doesn't handle options. But if this issue was closed, I could write a bunch of completion logic for all the nix-* commands, without actually going through Nix.

## Is your feature request related to a problem? Please describe. I've been trying to lower my reliance on nix3 commands as of late, but one of the things I've been missing is shell completions. Eelco added completions in the original flakes PR (commit seen [here](https://github.com/NixOS/nix/pull/3573/commits/91ddee6bf045b1c6144d14233abdb96127186ec3)). From what I can tell, the completions logic [remains minimal](https://git.lix.systems/lix-project/lix/src/commit/24054c110787d725ac0bbdafd40c71c83bf1f9a0/lix/libutil/args.cc#L83). As Lix is planning on bringing Flakes out of core, it'd be nice if nix2 received quality-of-life improvements like this. ## Describe alternatives you've considered I was able to achieve a minimal level of completions for `nix-shell -p` in Fish, by going through the existing flake infrastructure. ```fish function _nixpkgs_completions # Lie to nix and pretend we're generating the flakey completions set -l current_token (commandline --current-token) set -l old_args (env NIX_GET_COMPLETIONS=2 nix shell "nixpkgs#$current_token") for arg in $old_args # nix gives the completions in the form of: "nixpkgs#foo" # We need to get just the foo part. # TODO: is there a faster way to do this? set -a new_args (string sub --start=9 $arg) end string collect -- $new_args[2..-1] end complete -c nix-shell \ -x -s p -l packages \ -a '(_nixpkgs_completions)' ``` Obviously this is terrible and doesn't handle options. But if this issue was closed, I _could_ write a bunch of completion logic for all the `nix-*` commands, without actually going through Nix.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lix-project/lix#1043
No description provided.