This repository has no description
4.9 kB
174 lines
1{ ... }:
2{
3 perSystem =
4 { pkgs, ... }:
5 let
6 zshrc = ''
7 autoload -U compinit && compinit
8
9 # Setup custom interactive shell init stuff.
10 # Bind gpg-agent to this TTY if gpg commands are used.
11 export GPG_TTY=$(tty)
12
13
14 unsetopt nomatch # no matches found urls
15 setopt autocd extendedglob
16 bindkey -e
17
18 #C-x C-e open line in editor
19 autoload -z edit-command-line
20 zle -N edit-command-line
21 bindkey "^X^E" edit-command-line
22
23 #fzf inclusion
24 source ${pkgs.fzf}/share/fzf/completion.zsh
25 source ${pkgs.fzf}/share/fzf/key-bindings.zsh
26
27 # atuin distributed shell history
28 export ATUIN_CONFIG_DIR=${pkgs.writeTextDir "/config.toml" ''
29 auto_sync = true
30 update_check = false
31 sync_address = "http://green.r:8888"
32 sync_frequency = 0
33 style = "compact"
34 ''};
35 export ATUIN_NOBIND="true" # disable all keybdinings of atuin
36 eval "$(atuin init zsh)"
37 bindkey '^r' _atuin_search_widget # bind ctrl+r to atuin
38 # use zsh only session history
39 fc -p
40
41 #completion magic
42 autoload -Uz compinit
43 compinit
44 zstyle ':completion:*' menu select
45
46 #enable automatic rehashing of $PATH
47 zstyle ':completion:*' rehash true
48
49 # fancy mv which interactively gets the second argument if not given
50 function mv() {
51 if [[ "$#" -ne 1 ]] || [[ ! -e "$1" ]]; then
52 command mv -v "$@"
53 return
54 fi
55
56 newfilename="$1"
57 vared newfilename
58 command mv -v -- "$1" "$newfilename"
59 }
60
61 #emacs bindings
62 bindkey "[7~" beginning-of-line
63 bindkey "[8~" end-of-line
64 bindkey "Oc" emacs-forward-word
65 bindkey "Od" emacs-backward-word
66
67 # direnv integration
68 eval "$(${pkgs.direnv}/bin/direnv hook zsh)"
69
70 # This function is called whenever a command is not found.
71 command_not_found_handler() {
72 local p='${pkgs.nix-index}/etc/profile.d/command-not-found.sh'
73 if [ -x "$p" ] && [ -f '/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite' ]; then
74 # Run the helper program.
75 "$p" "$@"
76
77 # Retry the command if we just installed it.
78 if [ $? = 126 ]; then
79 "$@"
80 else
81 return 127
82 fi
83 else
84 # Indicate than there was an error so ZSH falls back to its default handler
85 echo "$1: command not found" >&2
86 return 127
87 fi
88 }
89
90
91 # Setup aliases.
92 alias -- grep='grep --color=auto'
93 alias -- ip='ip -color=auto'
94 alias -- l='ls -alh'
95 alias -- la='ls -la'
96 alias -- ll='ls -l'
97 alias -- ls='ls --color'
98 alias -- ns='nix-shell --command zsh'
99
100 # Setup prompt.
101 autoload -U promptinit
102 promptinit
103
104 p_error='%(?..%F{red}%?%f )'
105 t_error='%(?..%? )'
106
107 case $UID in
108 0)
109 p_username='%F{red}root%f'
110 t_username='root'
111 ;;
112 1337)
113 p_username=""
114 t_username=""
115 ;;
116 *)
117 p_username='%F{blue}%n%f'
118 t_username='%n'
119 ;;
120 esac
121
122 if test -n "$SSH_CLIENT"; then
123 p_hostname='@%F{magenta}%M%f '
124 t_hostname='@%M '
125 else
126 p_hostname=""
127 t_hostname=""
128 fi
129
130 #check if in nix shell
131 if test -n "$IN_NIX_SHELL"; then
132 p_nixshell='%F{green}[s]%f '
133 t_nixshell='[s] '
134 else
135 p_nixshell=""
136 t_nixshell=""
137 fi
138
139 PROMPT="$p_error$p_username$p_hostname$p_nixshell%~ "
140 TITLE="$t_error$t_username$t_hostname$t_nixshell%~"
141 case $TERM in
142 (*xterm* | *rxvt*)
143 function precmd {
144 PROMPT_EVALED=$(print -P "$TITLE")
145 echo -ne "\033]0;$$ $PROMPT_EVALED\007"
146 }
147 # This seems broken for some reason
148 # # This is seen while the shell waits for a command to complete.
149 # function preexec {
150 # PROMPT_EVALED=$(print -P "$TITLE")
151 # echo -ne "\033]0;$$ $PROMPT_EVALED $1\007"
152 # }
153 ;;
154 esac
155
156
157 # Disable some features to support TRAMP.
158 if [ "$TERM" = dumb ]; then
159 unsetopt zle prompt_cr prompt_subst
160 unset RPS1 RPROMPT
161 PS1='$ '
162 PROMPT='$ '
163 fi
164 '';
165 in
166 {
167 packages.shell = pkgs.writeScriptBin "shell" ''
168 #!/bin/sh
169 zdotdir=$(mktemp -d)
170 export ZDOTDIR=${pkgs.writeTextDir "/.zshrc" zshrc}
171 exec ${pkgs.zsh}/bin/zsh "$@"
172 '';
173 };
174}