Files
LogSeqDB/logseq/.recycle/pages_Tech_NixOS_Git status in shell prompt with Nix Home Manager.md.bak
2025-12-11 06:26:12 -08:00

45 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
created: 2024-01-25T16:49:34 (UTC -05:00)
tags:
- nixos
source: https://jeffkreeftmeijer.com/nix-home-manager-git-prompt/
author: 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`](https://github.com/git/git/blob/master/contrib/completion/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](https://github.com/jeffkreeftmeijer/git-prompt.sh) 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 prompt<sup><a id="fnr.1" href="https://jeffkreeftmeijer.com/nix-home-manager-git-prompt/#fn.1" role="doc-backlink">1</a></sup>:
```
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)