feat(nix): migrate fish shell and direnv to Nix

This commit is contained in:
Stefan Imhoff
2024-07-30 20:23:36 +02:00
parent 5c3da861b9
commit 03d01920c8
46 changed files with 245 additions and 2791 deletions

View File

@@ -18,9 +18,16 @@
};
environment.systemPackages = [ pkgs.pinentry_mac ];
# Create /etc/zshrc that loads the nix-darwin environment.
programs.zsh.enable = true; # default shell on catalina
# programs.fish.enable = true;
# Shells
environment.shells = with pkgs; [
bash
zsh
fish
];
programs.zsh.enable = true;
programs.fish.enable = true;
programs.bash.enable = true;
# Used for backwards compatibility, please read the changelog before changing.
# $ darwin-rebuild changelog

View File

@@ -7,22 +7,16 @@
"homebrew/services" # Manage background services using the daemon manager launchctl on macOS or systemctl on Linux.
];
brews = [
"bash" # TODO: Migrate to nix
"cava" # Console-based Audio Visualizer for ALSA
"fileicon" # macOS CLI for managing custom icons for files and folders
"fish" # TODO: Migrate to nix
"gettext" # INFO: Dependency of Neovim
"libiconv" # INFO: Dependency of nixpkgs_fmt
"luajit" # INFO: Dependency of Neovim
"neovim" # TODO: Migrate to nix
"oatmeal" # Terminal UI to chat with large language models (LLM) using backends such as Ollama, and direct integrations with your favourite editor like Neovim!
"prettier" # Code formatter for JavaScript, CSS, JSON, GraphQL, Markdown, YAML
"starship" # TODO: Migrate to nix
"tmux" # TODO: Migrate to nix
"tree-sitter" # INFO: Dependency of Neovim
"urlview" # URL extractor/launcher
"zsh" # TODO: Migrate to nix
"zsh-syntax-highlighting" # TODO: Migration to nix
];
casks = [
"affinity-designer" # Professional graphic design software
@@ -133,4 +127,3 @@
"Yoink" = 457622435; # Drag and drop
};
}

View File

@@ -13,7 +13,9 @@ in
./bat
./ctags
./curl
./direnv
./editorconfig
./fish
./fzf
./gh
./gh-dash
@@ -70,12 +72,14 @@ in
# Volta
VOLTA_HOME = "$HOME/.volta";
# Stable Diffusion
# VIRTUAL_ENV = "$HOME/Code/AI/stable-diffusion-webui/venv";
};
# Session Paths
home.sessionPath = [
# Personal scripts
"$HOME/.dotfiles/bin"
"$HOME/.dotfiles/private/bin"
# Homebrew
@@ -96,6 +100,9 @@ in
# Volta
"$VOLTA_HOME/bin"
# ASDF
"$HOME/.asdf/shims"
];
# Let Home Manager install and manage itself.
@@ -137,6 +144,7 @@ in
jq # Lightweight and flexible command-line JSON processor
lynx # Text-mode web browser
monolith # Bundle any web page into a single HTML file
nix-prefetch-git # Script used to obtain source hashes for fetchgit
nixd # Nix language server
nixpacks # App source + Nix packages + Docker = Image Resources
nixpkgs-fmt # Nix code formatter
@@ -159,6 +167,7 @@ in
woff2 # Webfont compression reference code
yarn # Fast, reliable, and secure dependency management for javascript
yt-dlp # Command-line tool to download videos from YouTube.com and other sites (youtube-dl fork)
zoxide # A faster way to navigate your filesystem
# Programming Languages
lua # Powerful, fast, lightweight, embeddable scripting language

View File

@@ -0,0 +1,8 @@
{ ... }:
{
programs.direnv = {
enable = true;
nix-direnv.enable = true;
};
}

109
nix/home/fish/default.nix Normal file
View File

@@ -0,0 +1,109 @@
{ lib, pkgs, ... }:
let
sharedAliases = import ../shared/shared-aliases.nix { inherit lib; };
# Function to read a file and return its contents as a string
readFile = file: builtins.readFile (./. + "/functions/${file}");
# List of function files
functionFiles = [
"dataUrl.fish"
"deleteNodeModules.fish"
"encodeBase64.fish"
"fcd.fish"
"fe.fish"
"fhcd.fish"
"fs.fish"
"fwt.fish"
"ghpr.fish"
"server.fish"
"unquarantine.fish"
"update.fish"
];
# Create a set of functions, where each key is the function name (without .fish extension)
# and the value is the contents of the file
fishFunctions = builtins.listToAttrs (map
(file: {
name = lib.removeSuffix ".fish" file;
value = readFile file;
})
functionFiles
);
in
{
programs.fish = {
enable = true;
# Shell options
interactiveShellInit = ''
# Enable vi-mode key bindings
fish_vi_key_bindings
# Set environment variables
set -gx TERM wezterm
'';
shellAliases = sharedAliases.shellAliases;
functions = fishFunctions;
plugins = [
{
name = "autopair.fish";
src = pkgs.fetchFromGitHub {
owner = "jorgebucaran";
repo = "autopair.fish";
rev = "4d1752ff5b39819ab58d7337c69220342e9de0e2";
sha256 = "qt3t1iKRRNuiLWiVoiAYOu+9E7jsyECyIqZJ/oRIT1A=";
};
}
{
name = "replay.fish";
src = pkgs.fetchFromGitHub {
owner = "jorgebucaran";
repo = "replay.fish";
rev = "d2ecacd3fe7126e822ce8918389f3ad93b14c86c";
sha256 = "TzQ97h9tBRUg+A7DSKeTBWLQuThicbu19DHMwkmUXdg=";
};
}
{
name = "bass";
src = pkgs.fetchFromGitHub {
owner = "edc";
repo = "bass";
rev = "79b62958ecf4e87334f24d6743e5766475bcf4d0";
sha256 = "3d/qL+hovNA4VMWZ0n1L+dSM1lcz7P5CQJyy+/8exTc=";
};
}
{
name = "z";
src = pkgs.fetchFromGitHub {
owner = "jethrokuan";
repo = "z";
rev = "85f863f20f24faf675827fb00f3a4e15c7838d76";
sha256 = "+FUBM7CodtZrYKqU542fQD+ZDGrd2438trKM0tIESs0=";
};
}
{
name = "fish-lf-icons";
src = pkgs.fetchFromGitHub {
owner = "joshmedeski";
repo = "fish-lf-icons";
rev = "d1c47b2088e0ffd95766b61d2455514274865b4f";
sha256 = "6po/PYvq4t0K8Jq5/t5hXPLn80iyl3Ymx2Whme/20kc=";
};
}
{
name = "nix-env.fish";
src = pkgs.fetchFromGitHub {
owner = "lilyball";
repo = "nix-env.fish";
rev = "7b65bd228429e852c8fdfa07601159130a818cfa";
sha256 = "RG/0rfhgq6aEKNZ0XwIqOaZ6K5S4+/Y5EEMnIdtfPhk=";
};
}
];
};
}

View File

@@ -0,0 +1,7 @@
function dataUrl --description "Create a data URL from a file"
set mimeType (file -b --mime-type $argv)
if string match -r '^text/' $mimeType
set mimeType "$mimeType;charset=utf-8"
end
echo "data:$mimeType;base64,(openssl base64 -in $argv | tr -d '\n')"
end

View File

@@ -0,0 +1,3 @@
function deleteNodeModules --description "Delete all node_modules folders in a folder and subfolders"
find . -name "node_modules" -type d -exec rm -rf '{}' +
end

View File

@@ -0,0 +1,3 @@
function encodeBase64 --description "Encodes images in Base64"
uuencode -m $argv[1] /dev/stdout | sed 1d | sed '$d'
end

View File

@@ -0,0 +1,3 @@
function fcd --description "cd into directory"
cd (find * -type d | fzf --preview 'tree -C {} | head -50')
end

View File

@@ -0,0 +1,10 @@
# fe [FUZZY PATTERN] - Open the selected file with the default editor
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
function fe --description "Open the selected file with the default editor"
set files (fzf-tmux --query=$argv --multi --select-1 --exit-0 | string split \n)
if test -n "$files"
$EDITOR $files; and true # This line is added to prevent failure when using "set -e" in shell.
end
end

View File

@@ -0,0 +1,4 @@
function fhcd --description "Jump to home directory and search for directories"
cd $HOME
cd (find * -type d | fzf --preview 'tree -C {} | head -50')
end

View File

@@ -0,0 +1,13 @@
function fs --description "Determine size of a file or total size of a directory"
if du -b /dev/null >/dev/null 2>&1
set arg -sbh
else
set arg -sh
end
if test -n "$argv"
du $arg -- $argv
else
du $arg .[^.]* *
end
end

View File

@@ -0,0 +1,3 @@
function fwt --description "Jump to Git worktree directory"
cd (git worktree list | awk '{print $1}' | fzf)
end

View File

@@ -0,0 +1,4 @@
function ghpr --description "Search and preview GitHub pull requests"
set -l GH_FORCE_TTY 100%
gh pr list | fzf --ansi --preview 'GH_FORCE_TTY=100% gh pr view {1}' --preview-window down --header-lines 3 | awk '{print $1}' | xargs gh pr checkout
end

View File

@@ -0,0 +1,3 @@
function server --description "Run a server with browser-sync"
browser-sync start --server --files "**"
end

View File

@@ -0,0 +1,5 @@
function unquarantine --description "Manually remove a downloaded app or file from the quarantine"
for attribute in com.apple.metadata:kMDItemDownloadedDate com.apple.metadata:kMDItemWhereFroms com.apple.quarantine
xattr -r -d "$attribute" $argv
end
end

View File

@@ -0,0 +1,13 @@
function update --description "Updating Homebrew, Ruby, Python, Node.js, Neovim, and MacOS"
sudo -v
brew update && brew outdated && brew upgrade && brew cleanup
sudo gem update --system && sudo gem update && gem cleanup all
pip install --upgrade pip
pip list -o --format columns | cut -d' ' -f1 | xargs -n1 pip install -U
pnpm update -g
~/.tmux/plugins/tpm/bin/update_plugins all
gh extension upgrade --all
fisher update
nvim --headless "+Lazy! sync" +qa
# sudo softwareupdate -i -a
end

View File

@@ -0,0 +1,102 @@
{ lib, ... }:
let
mkAliases = lib.mapAttrs (name: value: lib.mkForce value);
in
{
shellAliases = mkAliases {
# ZSH
zshconfig = "vim $HOME/.zshrc";
reload = "source $HOME/.zshrc";
# Folders/Lists
"..." = "cd ../..";
".." = "cd ..";
"cd.." = "cd ..";
ls = "eza --git --group-directories-first --icons";
ll = "eza -l --git --group-directories-first --icons";
lt = "eza --git --group-directories-first --icons --tree";
lla = "ll -a";
mkdir = "mkdir -p";
pn = "pnpm";
px = "pnpx";
# Git
ga = "git add";
gb = "git branch";
gba = "git branch -a";
gc = "git commit -v";
gca = "git commit -v -a";
gcam = "git commit --amend";
gcan = "git commit --amend --no-edit";
gd = "git diff -- . ':(exclude)yarn.lock'";
gdc = "git diff --cached";
gdh = "git diff head";
gdt = "git difftool";
gfa = "git fetch --all";
gg = "git log";
ghi = "git hist";
gl = "git pull";
glr = "git pull --rebase";
glu = "git config user.name 'Stefan Imhoff' && git config user.email 'gpg@kogakure.8shield.net' && git config user.signingkey '7A7253E8!'";
glx = "git config user.name 'Stefan Imhoff' && git config user.email 'stefan.imhoff@xing.com' && git config user.signingkey '73C3E2E3!'";
gmb = "git merge-base master HEAD";
gp = "git push";
gpf = "git push --force-with-lease";
gpp = "PATCHNAME=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`-`date '+%Y-%m-%d-%H%M.patch'`; git diff --full-index master > ../patches/$PATCHNAME";
gpu = "git push -u origin HEAD";
gpv = "git push --no-verify";
grb = "git rebase master";
grbc = "git rebase --continue";
grbi = "git rebase -i ";
grbs = "git rebase --skip";
gru = "git remote update";
gsb = "git show-branch";
gsl = "git submodule foreach git pull";
gst = "git status -sb";
gsu = "git submodule update";
gu = "git up";
gw = "git whatchanged";
gwp = "git whatchanged -p";
gwa = "git worktree add"; # <folder> <branch/hash>
gwl = "git worktree list";
gwr = "git worktree remove"; # <path/name>
lg = "lazygit";
# Vim
v = "vim";
vim = "nvim";
# Tmux
t = "tmux";
ta = "tmux attach";
# Nix
nxs = "darwin-rebuild switch --flake ~/.dotfiles/nix";
# Bat
cat = "bat";
# TLDR
tldrf = "tldr --list --single-column | fzf --preview \"tldr --color=always {1}\" --preview-window=right,70% | xargs tldr";
# Can't remember the fork name
youtube-dl = "yt-dlp";
# Dotfiles folder
dotfiles = "cd ~/.dotfiles";
# iCloud
icloud = "cd ~/Library/Mobile\\ Documents/com~apple~CloudDocs";
# Recursively delete `.DS_Store` files
cleanup = "find . -type f -name '*.DS_Store' -ls -delete";
# Clear the screen
c = "clear";
# Empty the Trash on all mounted volumes and the main HDD
# Also, clear Apple's System Logs to improve shell startup speed
emptytrash = "sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl";
};
}

View File

@@ -6,6 +6,7 @@ let
text = builtins.readFile ./_cht;
destination = "/share/zsh/site-functions/_cht";
};
sharedAliases = import ../shared/shared-aliases.nix { inherit lib; };
in
{
programs.zsh = {
@@ -13,107 +14,16 @@ in
enableCompletion = true;
autosuggestion.enable = true;
syntaxHighlighting.enable = true;
shellAliases = {
# ZSH
zshconfig = "vim $HOME/.zshrc";
reload = "source $HOME/.zshrc";
# Folders
"..." = "cd ../..";
".." = "cd ..";
"cd.." = "cd ..";
ls = "eza --git --group-directories-first --icons";
ll = "eza -l --git --group-directories-first --icons";
lla = "ll -a";
mkdir = "mkdir -p";
# Git aliases
ga = "git add";
gb = "git branch";
gba = "git branch -a";
gc = "git commit -v";
gca = "git commit -v -a";
gcam = "git commit --amend";
gcan = "git commit --amend --no-edit";
gd = "git diff -- . ':(exclude)yarn.lock'";
gdc = "git diff --cached";
gdh = "git diff head";
gdt = "git difftool";
gfa = "git fetch --all";
gg = "git log";
ghi = "git hist";
gl = "git pull";
glr = "git pull --rebase";
gp = "git push";
gpf = "git push --force-with-lease";
gpu = "git push -u origin HEAD";
gpv = "git push --no-verify";
grb = "git rebase master";
grbc = "git rebase --continue";
grbi = "git rebase -i ";
grbs = "git rebase --skip";
gru = "git remote update";
gsb = "git show-branch";
gsl = "git submodule foreach git pull";
gst = "git status -sb";
gsu = "git submodule update";
gu = "git up";
gw = "git whatchanged";
gwp = "git whatchanged -p";
lg = "lazygit";
# Vim
v = "vim";
vim = "nvim";
# Tmux
t = "tmux";
tn = "tmux new -s $(pwd | sed 's/.*\///g')";
# Bat
cat = "bat";
# TLDR
tldrf = "tldr --list --single-column | fzf --preview \"tldr --color=always {1}\" --preview-window=right,70% | xargs tldr";
# iA Writer
ia = "open $1 -a /Applications/iA\\ Writer.app";
# Dotfiles folder
dotfiles = "cd $HOME/.dotfiles";
# iCloud
icloud = "cd $HOME/Library/Mobile\\ Documents/com~apple~CloudDocs";
# Get week number
week = "date +%V";
# Stopwatch
timer = "echo \"Timer started. Stop with Ctrl-D.\" && date && time cat && date";
# IP addresses
ip = "dig +short myip.opendns.com @resolver1.opendns.com";
# Flush Directory Service cache
flush = "dscacheutil -flushcache && killall -HUP mDNSResponder";
# Recursively delete `.DS_Store` files
cleanup = "find . -type f -name '*.DS_Store' -ls -delete";
# Clear the screen
c = "clear";
# Empty the Trash on all mounted volumes and the main HDD
# Also, clear Apples System Logs to improve shell startup speed
emptytrash = "sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl";
};
shellAliases = sharedAliases.shellAliases;
initExtra = builtins.readFile ./zshrc.sh;
plugins = [
{
name = "cht-completion";
src = chtCompletionScript;
}
];
antidote = {
enable = true;
plugins = [