This repository has no description
6.0 kB
199 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 # show how long a command takes
14 ZSH_COMMAND_TIME_MIN_SECONDS=3
15 ZSH_COMMAND_TIME_EXCLUDE=(vim)
16 ZSH_COMMAND_TIME_MSG="took: %s sec"
17 source ${
18 pkgs.fetchFromGitHub {
19 owner = "popstas";
20 repo = "zsh-command-time";
21 rev = "803d26eef526bff1494d1a584e46a6e08d25d918";
22 hash = "sha256-ndHVFcz+XmUW0zwFq7pBXygdRKyPLjDZNmTelhd5bv8=";
23 }
24 }/command-time.plugin.zsh
25
26 unsetopt nomatch # no matches found urls
27 setopt autocd extendedglob
28 bindkey -e
29
30 #C-x C-e open line in editor
31 autoload -z edit-command-line
32 zle -N edit-command-line
33 bindkey "^X^E" edit-command-line
34
35 #fzf inclusion
36 source ${pkgs.fzf}/share/fzf/completion.zsh
37 source ${pkgs.fzf}/share/fzf/key-bindings.zsh
38
39 # atuin distributed shell history
40 if type -p atuin >/dev/null; then
41 export ATUIN_CONFIG_DIR=${pkgs.writeTextDir "/config.toml" ''
42 auto_sync = true
43 update_check = false
44 sync_address = "http://green.r:8888"
45 sync_frequency = 0
46 style = "compact"
47 ''};
48 export ATUIN_NOBIND="true" # disable all keybdinings of atuin
49 eval "$(${pkgs.atuin}/bin/atuin init zsh)" # TODO make this optional?
50 bindkey '^r' _atuin_search_widget # bind ctrl+r to atuin
51 # use zsh only session history
52 fc -p
53 fi
54
55 #completion magic
56 autoload -Uz compinit
57 compinit
58 zstyle ':completion:*' menu select
59
60 #enable automatic rehashing of $PATH
61 zstyle ':completion:*' rehash true
62
63 # fancy mv which interactively gets the second argument if not given
64 function mv() {
65 if [[ "$#" -ne 1 ]] || [[ ! -e "$1" ]]; then
66 command mv -v "$@"
67 return
68 fi
69
70 newfilename="$1"
71 vared newfilename
72 command mv -v -- "$1" "$newfilename"
73 }
74
75 #emacs bindings
76 bindkey "[7~" beginning-of-line
77 bindkey "[8~" end-of-line
78 bindkey "Oc" emacs-forward-word
79 bindkey "Od" emacs-backward-word
80
81 # direnv integration
82 eval "$(${pkgs.direnv}/bin/direnv hook zsh)"
83
84 # This function is called whenever a command is not found.
85 command_not_found_handler() {
86 local p='${pkgs.nix-index}/etc/profile.d/command-not-found.sh'
87 if [ -x "$p" ] && [ -f '/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite' ]; then
88 # Run the helper program.
89 "$p" "$@"
90
91 # Retry the command if we just installed it.
92 if [ $? = 126 ]; then
93 "$@"
94 else
95 return 127
96 fi
97 else
98 # Indicate than there was an error so ZSH falls back to its default handler
99 echo "$1: command not found" >&2
100 return 127
101 fi
102 }
103
104 download_nixpkgs_cache_index () {
105 filename="index-$(uname -m | sed 's/^arm64$/aarch64/')-$(uname | tr A-Z a-z)"
106 mkdir -p ~/.cache/nix-index && cd ~/.cache/nix-index
107 # -N will only download a new version if there is an update.
108 wget -q -N https://github.com/nix-community/nix-index-database/releases/latest/download/$filename
109 ln -f $filename files
110 }
111
112 # Setup aliases.
113 alias -- grep='grep --color=auto'
114 alias -- ip='ip -color=auto'
115 alias -- l='ls -alh'
116 alias -- la='ls -la'
117 alias -- ll='ls -l'
118 alias -- ls='ls --color'
119 alias -- ns='nix-shell --command zsh'
120
121 # Setup prompt.
122 autoload -U promptinit
123 promptinit
124
125 p_error='%(?..%F{red}%?%f )'
126 t_error='%(?..%? )'
127
128 case $UID in
129 0)
130 p_username='%F{red}root%f'
131 t_username='root'
132 ;;
133 1337)
134 p_username=""
135 t_username=""
136 ;;
137 *)
138 p_username='%F{blue}%n%f'
139 t_username='%n'
140 ;;
141 esac
142
143 if test -n "$SSH_CLIENT"; then
144 p_hostname='@%F{magenta}%M%f '
145 t_hostname='@%M '
146 else
147 p_hostname=""
148 t_hostname=""
149 fi
150
151 #check if in nix shell
152 if test -n "$IN_NIX_SHELL"; then
153 p_nixshell='%F{green}[s]%f '
154 t_nixshell='[s] '
155 else
156 p_nixshell=""
157 t_nixshell=""
158 fi
159
160 PROMPT="$p_error$p_username$p_hostname$p_nixshell%~ "
161 TITLE="$t_error$t_username$t_hostname$t_nixshell%~"
162 case $TERM in
163 (*xterm* | *rxvt*)
164 function precmd {
165 PROMPT_EVALED=$(print -P "$TITLE")
166 echo -ne "\033]0;$$ $PROMPT_EVALED\007"
167 }
168 # This seems broken for some reason
169 # # This is seen while the shell waits for a command to complete.
170 # function preexec {
171 # PROMPT_EVALED=$(print -P "$TITLE")
172 # echo -ne "\033]0;$$ $PROMPT_EVALED $1\007"
173 # }
174 ;;
175 esac
176
177
178 # Disable some features to support TRAMP.
179 if [ "$TERM" = dumb ]; then
180 unsetopt zle prompt_cr prompt_subst
181 unset RPS1 RPROMPT
182 PS1='$ '
183 PROMPT='$ '
184 fi
185 '';
186 in
187 {
188 packages.shell = pkgs.writeScriptBin "shell" ''
189 #!/bin/sh
190 export ZDOTDIR=${pkgs.writeTextDir "/.zshrc" zshrc}
191 exec ${pkgs.zsh}/bin/zsh "$@"
192 '';
193 packages.zsh = pkgs.writeScriptBin "zsh" ''
194 #!/bin/sh
195 export ZDOTDIR=${pkgs.writeTextDir "/.zshrc" zshrc}
196 exec ${pkgs.zsh}/bin/zsh "$@"
197 '';
198 };
199}