Files
LogSeqDB/pages/Git status in shell prompt with Nix Home Manager.sync-conflict-20250817-085627-UULL5XD.md
2025-12-11 06:26:12 -08:00

1.8 KiB
Raw Blame History

created, tags, source, author
created tags source author
2024-01-25T16:49:34 (UTC -05:00) nixos https://jeffkreeftmeijer.com/nix-home-manager-git-prompt/ Jeff Kreeftmeijer
  • Git status in shell prompt with Nix Home Manager

    Excerpt

    By on 2023-03-19 (originally published on 2023-03-13)


    By on 2023-03-19 (originally published on 2023-03-13)

    Git comes with a script named git-prompt.sh that exposes information about the current directorys repository to use in your shells prompt. The script is in the contrib directory, but I used to extract it to not have to download the git source to set my prompt.

    Using Nix, the prompt is available in the ~/.nix-profile directory, meaning setting up the prompt doesnt require remembering to download an extra file. Instead, its three lines in programs.zsh.initExtra to source the script and set the prompt1:

    programs.zsh.initExtra =
    ''
    source ~/.nix-profile/share/git/contrib/completion/git-prompt.sh
    setopt PROMPT_SUBST
    export PS1='%~ $(__git_ps1 "(%s) ")%# '
    '';
    

    This produces prompts like ~/repository (main) % inside a git repository, and ~/not/a/repository % elsewhere.


    1. This example is for zsh, omit the setopt and alter the PS1 for bash:
    ```
    programs.bash.initExtra =
    ''
    source ~/.nix-profile/share/git/contrib/completion/git-prompt.sh
    export PS1='\w $(__git_ps1 "(%s) ")$ '
    '';
    ```
    
    [↩︎](https://jeffkreeftmeijer.com/nix-home-manager-git-prompt/#fnr.1)