-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.nix
517 lines (443 loc) · 15.3 KB
/
home.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
{ config, pkgs, ... }:
let
# Found this out debugging and digging with `builtins.trace (pkgs.lib.attrNames config)`. No idea where `config.home` comes from.
home = config.home.homeDirectory;
shellStartupDir = "${home}/Code";
zshCustomCompletionsRelToHome = ".zsh_custom_completions";
zshCustomCompletions = "${home}/${zshCustomCompletionsRelToHome}";
# Alacritty default is 10k, its maximum 100k; tmux default is 2k; filling up 100k
# lines w/ tmux takes 600 MB memory, so let's not max it out
terminalScrollback = 20000;
ssh = {
agentDuration = "12h";
key = rec {
type = "ed25519";
priv = "~/.ssh/id_${type}";
pub = "${priv}.pub";
};
};
in
{
home = {
stateVersion = "23.11";
activation = {
# Creates a file, so place after `writeBoundary`; https://nix-community.github.io/home-manager/options.xhtml#opt-home.activation
createTerminalStartDir = config.lib.dag.entryAfter [ "writeBoundary" ] ''
run mkdir -p ${shellStartupDir}
'';
};
file = {
".config" = {
source = ./home/.config;
recursive = true;
};
".ipython" = {
source = ./home/.ipython;
recursive = true;
};
".ignore" = {
# General-purpose ignore file. For example, this can be picked up by `rg`:
# https://github.com/BurntSushi/ripgrep/blob/f1d23c06e30606b2428a4e32da8f0b5069e81280/GUIDE.md#L184
# and `fd`:
# https://github.com/sharkdp/fd/blob/29936f0fbae1e52984ab582b2b2c98685d6ad638/README.md#L263
# ⚠️ These have `.gitignore` semantics.
text = ''
.DS_Store
.Trash
Applications
Desktop
Documents
Downloads
Library
Movies
Music
Pictures
Public
.git/
'';
};
"${zshCustomCompletionsRelToHome}/dummy" = {
text = ''
# Placeholder for zsh completions, to ensure this directory is created.
# Managing this in Nix ensures proper lifecycle management.
# Completion scripts of the form `_some-command` can be placed in this directory.
'';
};
};
packages = import ./packages/lists/personal.nix { inherit pkgs; };
};
programs = {
alacritty = {
enable = true;
settings = {
general = {
working_directory = shellStartupDir;
import = [
"${pkgs.alacritty-theme}/material_theme.toml"
];
};
terminal = {
shell = {
program = pkgs.lib.getExe pkgs.tmux;
args = [
"new-session"
"-A"
"-D"
"-s"
"main"
];
};
};
window = {
startup_mode = "Fullscreen";
option_as_alt = "Both";
};
scrolling = {
history = terminalScrollback;
};
keyboard = {
bindings = [
{
# https://github.com/alacritty/alacritty/issues/474#issuecomment-338803299
key = "Left";
mods = "Alt";
chars = "\\u001bb";
}
{
# https://github.com/alacritty/alacritty/issues/474#issuecomment-338803299
key = "Right";
mods = "Alt";
chars = "\\u001bf";
}
{
# https://github.com/alacritty/alacritty/issues/474#issuecomment-338803299
key = "Left";
mods = "Command";
chars = "\\u001bOH";
}
{
# https://github.com/alacritty/alacritty/issues/474#issuecomment-338803299
key = "Right";
mods = "Command";
chars = "\\u001bOF";
}
];
};
selection = {
save_to_clipboard = true;
};
font = {
normal = {
family = "FiraCode Nerd Font";
style = "Regular"; # Default one is too thin
};
size = 22; # Going blind over here
};
};
};
direnv = {
enable = true;
};
fd = {
enable = true;
hidden = true;
};
fzf = {
enable = true;
fileWidgetCommand = "${pkgs.fd}/bin/fd --hidden";
fileWidgetOptions = [
"--preview '${pkgs.bat}/bin/bat --style=numbers --color=always --line-range :100 {}'"
];
tmux = {
enableShellIntegration = false; # Kinda neat but doesn't put fzf where I want it
};
};
git = {
enable = true;
userName = "Alex Povel";
userEmail = "[email protected]";
aliases = {
a = "add";
ap = "add --patch";
c = "commit";
ca = "commit --all";
cano = "commit --amend --no-edit";
cl = "!f() { git clone \"$1\" $(git-url-extract-path \"$1\"); }; f"; # Sort into owner/repo format
cli = "clean --interactive";
d = "diff";
dog = "log --decorate --oneline --graph --pretty=format:'%C(auto)%h%d %s %C(240)- %cr %C(240)- %an'";
doga = "dog --all";
dogaf = "doga --first-parent";
dogf = "dog --first-parent";
ds = "diff --staged";
p = "push";
pl = "pull --prune";
pla = "pull --all --prune";
rb = "rebase";
rbi = "rebase --interactive";
s = "status --short --branch";
subfull = "submodule update --init --recursive";
sw = "switch";
swd = "sw --detach";
# 'What does force-pushing the current branch to its upstream (overwriting it)
# change to the PR of that upstream against main?'; similar to
# https://stackoverflow.com/a/52512813/11477374 . `@{u}`: upstream branch.
check-if-force-pushing-ruins-my-life = "!f() { git range-diff main HEAD $(git rev-parse @{u}); }; f";
};
delta = {
enable = true;
options = {
hyperlinks = true;
light = false; # set to true if you're in a terminal w/ a light background color (e.g. the default macOS terminal)
navigate = true; # use n and N to move between diff sections
syntax-theme = "Coldark-Dark";
};
};
ignores = [
# General-purpose, low-false positive items.
# https://git-scm.com/docs/gitignore#_pattern_format
".DS_Store"
".dev/" # In-repo temporary experiments
];
extraConfig = {
commit = {
gpgsign = true;
};
core = {
autocrlf = false;
eol = "lf";
editor = "code --wait";
fsmonitor = true;
};
gpg = {
format = "ssh";
ssh = {
allowedSignersFile = "~/.ssh/allowed_signers";
};
};
merge = {
conflictstyle = "diff3";
};
pull = {
rebase = true;
};
push = {
autoSetupRemote = true;
};
rerere = {
enabled = true;
};
user = {
signingKey = ssh.key.pub;
};
};
};
go = {
enable = true;
};
home-manager = {
# Let Home Manager install and manage itself.
enable = true;
};
nix-index = {
enable = true;
};
ripgrep = {
enable = true;
arguments = [
"--smart-case"
"--hidden"
];
};
ssh = {
enable = true;
addKeysToAgent = ssh.agentDuration;
compression = true;
includes = [
# Place add. config files here. Allows for non-Nix managed, throw-away configs.
"dynamic.d/*"
];
};
starship = {
enable = true;
enableZshIntegration = true;
settings = {
kubernetes = {
disabled = false;
};
git_status = {
ahead = "\${count}a";
behind = "\${count}b";
conflicted = "\${count}c";
deleted = "\${count}d";
diverged = "\${ahead_count}/\${behind_count}D";
modified = "\${count}m";
renamed = "\${count}r";
staged = "\${count}s";
stashed = "\${count}S";
untracked = "\${count}u";
up_to_date = "✓";
};
custom = {
sshagent = {
# Convenience function to not be surprised by untimely password prompts.
command = ''
[[ $(ssh-add -L) =~ "no identities" ]] && echo '! 🔐 ssh agent vacant'
'';
when = true;
style = "bold yellow";
description = "Indicates whether any identites are represented by the ssh agent";
};
};
};
};
tmux = {
enable = true;
baseIndex = 1; # Default of 0 is inconvenient
historyLimit = terminalScrollback;
mouse = true; # For scrolling easily
extraConfig = ''
# https://stackoverflow.com/a/45010147/11477374
set-option -g status-interval 2
set-option -g automatic-rename on
set-option -g automatic-rename-format "#{?#{==:#{pane_current_command},zsh},#{b:pane_current_path},#{pane_current_command}}"
'';
};
vim = {
enable = true;
};
vscode = {
enable = true;
};
zoxide = {
enable = true;
};
zsh = {
# Things like starship and fzf integration only start working if home-manager manages zsh.
# While nix-darwin manages `/etc/zshrc`, home-manager manages `~/.zshrc`, and will source etc. there.
enable = true;
autosuggestion = {
enable = true;
# Make it obnoxiously different; with a non-hex value, suggestions and actual
# insertions were the same color.
highlight = "fg=#c787ee,bold";
strategy = [
"history"
"completion"
];
};
syntaxHighlighting = {
enable = true;
highlighters = [
"main"
"brackets"
];
};
plugins = [
{
# Get some more native completions (e.g. `go` command). See list at
# https://github.com/zsh-users/zsh-completions/tree/master/src
name = "zsh-completions";
src = "${pkgs.zsh-completions}/share/zsh/site-functions";
}
];
initExtraBeforeCompInit = ''
# zmodload zsh/zprof # Uncomment for `zprof`
# Expand where zsh looks for completions.
# This slows down shell startup massively (~200ms), even for simple completions (https://www.reddit.com/r/zsh/comments/wrbi1v/7x_slowdown_when_modify_fpath_and_add_completion/).
# THAT'S TRUE EVEN IF THIS DIRECTORY IS EMPTY, which some tests showed.
fpath+=(${zshCustomCompletions})
# https://docs.docker.com/config/completion/#zsh
# Do this dynamically to keep completions in sync. We're rocking Docker Desktop,
# which isn't natively installable via Nix (which usually takes care of completions).
docker completion zsh > ${zshCustomCompletions}/_docker # Not the cheapest call; ~20ms on warm cache
for cmd in 'rustup' 'cargo'; do
# `cargo` completions were actually already set up, but `rustup` weren't.
# Just do both for consistency.
rustup completions zsh "$cmd" > "${zshCustomCompletions}/_$cmd";
done
# More completions go here, in the form of `_some-command`...
# Note: Nix will install most completions natively already.
'';
initExtra = ''
test -f ${ssh.key.priv} || { ssh-keygen -t ${ssh.key.type} -f ${ssh.key.priv} && echo "Run \`ssh-keygen -c\` to set comment on new key"; }
ssh-add -L | grep "$(cat ${ssh.key.pub})" || { echo -n "(Hit Return to skip) " && ssh-add -t '${ssh.agentDuration}' ${ssh.key.priv}; }
wf() {
# "`w`here `f`ile": which files contain the given regex?
#
# Can be used as `vim $(wf -i 'foo')` to open a file containing 'foo'
# (case-insensitive).
rg --files-with-matches "$@" | fzf
}
# Clone a user's personal (== non-fork) GitHub repositories and pull *all* branches.
pullall() {
gh auth status 1>/dev/null 2>&1 || gh auth login
local USER="$1"
gh repo list "$USER" --limit 1000 --source | while read -r repo _; do
gh repo clone "$repo" "$repo" || (
cd "$repo"
for branch in 'main' 'master' 'dev' 'devel'; do
# Need to be on a branch to pull; first one found wins.
git switch "$branch" && break
done
git pull --all || echo "Failed to pull $repo"
)
done
}
setopt interactive_comments # Allow comments in interactive shell, to tag them for later search
# `HOME` happens to send '^[[1~' and `END` '^[[4~' on my machine (no idea), see also
# https://github.com/search?type=code&q=%27%5E%5B%5B1%7E%27 .
# Found out via `command cat -v` and pressing the keys.
bindkey '^[[1~' beginning-of-line
bindkey '^[[4~' end-of-line
# https://thevaluable.dev/zsh-completion-guide-examples/
zstyle ':completion:*' menu select
bindkey '^[[Z' reverse-menu-complete # Shift-Tab; https://unix.stackexchange.com/a/722487
# Launch and `d`etach new window at `t`arget index, with specific `n`ame
tmux list-windows -F '#W' | grep -q 'pi' || tmux new-window -d -n 'pi' -t 2 'ipython' || true
# zprof # Uncomment for `zprof`
'';
envExtra = ''
path+=("$(go env GOPATH)/bin") # Target for 'go install'; for syntax, see also https://stackoverflow.com/a/18077919
export WORDCHARS='-_' # Consider only these part of words (default is MUCH more); see also `man zshall | grep -C5 'WORDCHARS'`
export HISTORY_SUBSTRING_SEARCH_FUZZY=1 # Fuzzy search in history
export HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=1 # Don't show duplicates
export HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_TIMEOUT=2 # Highlight matches for 2 seconds
# These were unset or "C" before, causing wrong Unicode processing and 'Unknown local' warnings.
# From `man 7 locale`, it seems `LANG` and `LC_ALL` in combination suffice (`locale` now prints `en_US.UTF-8` for everything).
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
'';
logoutExtra = ''
echo "Running history backup"
BACKUP_SRC=$HOME/.zsh_history # This breaks if ZDOTDIR != HOME
BACKUP_DST=$HOME/Nextcloud/.backup/zsh_history/$(hostname)/.zsh_history # Excuse the hard-coding
if [ -e "$BACKUP_SRC" ]; then
mkdir -p "$(dirname $BACKUP_DST)"
cp "$BACKUP_SRC" "$BACKUP_DST"
fi
'';
shellAliases = {
c = "cargo";
d = "docker";
cat = "bat";
g = "git";
j = "just";
k = "kubectl";
l = "eza --long --git --git-repos --header --all --all"; # `all` twice gives `.` and `..`
m = "make";
pi = "ipython";
rr = "git rev-parse --show-toplevel 2>/dev/null || pwd"; # Get current git repo's root, if possible; can be used as `cd $(rr)`, `z `rr`` etc.
tf = "terraform";
};
history = {
extended = true;
ignoreSpace = true; # True is default, but be extra sure. Relying on this for secrets
size = 1000000;
};
historySubstringSearch = {
enable = true;
};
};
};
}