This repository has no description
6.2 kB
202 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 alias -- nb='nix build --no-link --print-out-paths -L'
121 alias -- ne='nix eval --strict --json'
122 alias -- nloc='nix-locate --top-level --whole-name'
123
124 # Setup prompt.
125 autoload -U promptinit
126 promptinit
127
128 p_error='%(?..%F{red}%?%f )'
129 t_error='%(?..%? )'
130
131 case $UID in
132 0)
133 p_username='%F{red}root%f'
134 t_username='root'
135 ;;
136 1337)
137 p_username=""
138 t_username=""
139 ;;
140 *)
141 p_username='%F{blue}%n%f'
142 t_username='%n'
143 ;;
144 esac
145
146 if test -n "$SSH_CLIENT"; then
147 p_hostname='@%F{magenta}%M%f '
148 t_hostname='@%M '
149 else
150 p_hostname=""
151 t_hostname=""
152 fi
153
154 #check if in nix shell
155 if test -n "$IN_NIX_SHELL"; then
156 p_nixshell='%F{green}[s]%f '
157 t_nixshell='[s] '
158 else
159 p_nixshell=""
160 t_nixshell=""
161 fi
162
163 PROMPT="$p_error$p_username$p_hostname$p_nixshell%~ "
164 TITLE="$t_error$t_username$t_hostname$t_nixshell%~"
165 case $TERM in
166 (*xterm* | *rxvt*)
167 function precmd {
168 PROMPT_EVALED=$(print -P "$TITLE")
169 echo -ne "\033]0;$$ $PROMPT_EVALED\007"
170 }
171 # This seems broken for some reason
172 # # This is seen while the shell waits for a command to complete.
173 # function preexec {
174 # PROMPT_EVALED=$(print -P "$TITLE")
175 # echo -ne "\033]0;$$ $PROMPT_EVALED $1\007"
176 # }
177 ;;
178 esac
179
180
181 # Disable some features to support TRAMP.
182 if [ "$TERM" = dumb ]; then
183 unsetopt zle prompt_cr prompt_subst
184 unset RPS1 RPROMPT
185 PS1='$ '
186 PROMPT='$ '
187 fi
188 '';
189 in
190 {
191 packages.shell = pkgs.writeScriptBin "shell" ''
192 #!/bin/sh
193 export ZDOTDIR=${pkgs.writeTextDir "/.zshrc" zshrc}
194 exec ${pkgs.zsh}/bin/zsh "$@"
195 '';
196 packages.zsh = pkgs.writeScriptBin "zsh" ''
197 #!/bin/sh
198 export ZDOTDIR=${pkgs.writeTextDir "/.zshrc" zshrc}
199 exec ${pkgs.zsh}/bin/zsh "$@"
200 '';
201 };
202}