43 lines
1.7 KiB
Markdown
43 lines
1.7 KiB
Markdown
---
|
||
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 directory’s repository to use in your shell’s 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 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 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) |