Git comes with a script named git-prompt.sh that exposes information about the current directory’s repository to use in your shell’s 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 doesn’t require remembering to download an extra file.
Instead, it’s 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.
This example is for zsh, omit the
setoptand alter thePS1for bash:↩︎programs.bash.initExtra = '' source ~/.nix-profile/share/git/contrib/completion/git-prompt.sh export PS1='\w $(__git_ps1 "(%s) ")$ ' '';