Files
home/modules/home/git.nix
2025-12-29 21:14:44 +01:00

231 lines
8.1 KiB
Nix

# Git module
#
# This module configures Git with Home Manager's programs.git options.
# It supports both personal and work profiles with different signing keys
# and includeIf paths for project-specific configurations.
#
# Based on the chezmoi configuration from dot_gitconfig.tmpl
{
config,
pkgs,
lib,
...
}:
with lib;
let
cfg = config.modules.git;
in
{
options.modules.git = {
enable = mkEnableOption "Git configuration";
userName = mkOption {
type = types.str;
default = "Morten Olsen";
description = "Git user name";
};
userEmail = mkOption {
type = types.str;
description = "Git user email address";
example = "alice@personal.example.com";
};
signingKey = mkOption {
type = types.str;
description = "SSH signing key (public key content)";
example = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...";
};
includes = mkOption {
type = types.listOf (
types.submodule {
options = {
condition = mkOption {
type = types.str;
description = "The includeIf condition (e.g., gitdir:~/Projects/)";
example = "gitdir:~/Projects/";
};
path = mkOption {
type = types.str;
description = "Path to the included gitconfig file";
example = "~/Projects/.gitconfig";
};
};
}
);
default = [ ];
description = "List of conditional includes for project-specific git configurations";
};
};
config = mkIf cfg.enable {
# Global gitignore file
home.file.".gitignore_global".text = ''
.envrc
*._local.*
.DS_Store
*.tsbuildinfo
.env
'';
# Delta pager for better diffs (separate program in newer Home Manager)
programs.delta = {
enable = true;
enableGitIntegration = true;
options = {
navigate = true;
light = false;
side-by-side = true;
line-numbers = true;
};
};
programs.git = {
enable = true;
# Signing configuration with 1Password
signing = {
key = cfg.signingKey;
signByDefault = true;
};
# Conditional includes for project-specific configurations
includes = map (inc: {
inherit (inc) condition;
inherit (inc) path;
}) cfg.includes;
# All git settings using the new unified settings option
settings = {
# User configuration
user = {
name = cfg.userName;
email = cfg.userEmail;
};
# Core settings (pager is set by programs.delta)
core = {
hooksPath = "/dev/null";
excludesfile = "~/.gitignore_global";
};
# Pull settings
pull = {
ff = "only";
};
# Init settings
init = {
defaultBranch = "main";
};
# Push settings
push = {
autoSetupRemote = true;
};
# GPG/SSH signing settings
gpg = {
format = "ssh";
};
"gpg \"ssh\"" = {
program = "/Applications/1Password.app/Contents/MacOS/op-ssh-sign";
};
# Commit settings (gpgsign is handled by signing.signByDefault)
commit = {
gpgsign = true;
};
# URL rewrites
"url \"https://\"" = {
insteadOf = "git://";
};
# Git LFS
filter.lfs = {
clean = "git-lfs clean -- %f";
smudge = "git-lfs smudge -- %f";
process = "git-lfs filter-process";
required = true;
};
# Difftool configuration
"difftool \"nvimdiff\"" = {
cmd = "nvim -d \"$LOCAL\" \"$REMOTE\"";
};
# Aliases
alias = {
# Log and history aliases
# =====
# Pretty graph log with GPG info
graph = "log --graph --color --pretty=format:\"%C(yellow)%H%C(green)%d%C(reset)%n%x20%cd%n%x20%cn%C(blue)%x20(%ce)%x20%C(cyan)[gpg:%GK%x20%G?]%C(reset)%n%x20%s%n\"";
# One-line log
ll = "log --oneline";
# Last commit with stats
last = "log -1 --stat";
# Status and branch aliases
# =====
# Short branch status
st = "status -sb";
# Formatted local branches
br = "branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate";
# Formatted remote branches
brr = "branch --remote --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate";
# Commit aliases
# =====
# Commit with message
cm = "commit -m";
# Amend last commit without editing message
append = "commit --amend --no-edit";
# Stage all and amend to last commit
aa = "!git add . && git commit --amend --no-edit";
# Stage all, amend, rebase, and force push with lease
aap = "!f() { git add . && git commit --amend --no-edit && git rebase origin/$(git rev-parse --abbrev-ref HEAD) && git push --force-with-lease; }; f";
# Fixup workflow aliases
# =====
# Interactive fixup: pick commit with fzf, stage all, create fixup
fixup = "!f() { commit=$(git log --oneline --color=always | fzf --ansi --height 40% | awk '{print $1}'); [ -n \"$commit\" ] && git add . && git commit --fixup=\"$commit\"; }; f";
# Quick fixup for previous commit
fixup1 = "!git add . && git commit --fixup=HEAD~1";
# Quick fixup for 2 commits ago
fixup2 = "!git add . && git commit --fixup=HEAD~2";
# Quick fixup for 3 commits ago
fixup3 = "!git add . && git commit --fixup=HEAD~3";
# Rebase with autosquash (auto-detects upstream/main/master)
squash-fixups = "!f() { upstream=$(git rev-parse --abbrev-ref --symbolic-full-name @{upstream} 2>/dev/null); if [ -n \"$upstream\" ]; then git rebase -i --autosquash \"$upstream\"; elif git show-ref --verify --quiet refs/remotes/origin/main; then git rebase -i --autosquash origin/main; elif git show-ref --verify --quiet refs/remotes/origin/master; then git rebase -i --autosquash origin/master; else echo \"No upstream branch found. Usage: git squash-fixups <base-branch>\"; fi; }; f";
# Squash all commits unique to current branch (auto-detects default branch)
squash-all = "!f() { default=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo \"main\"); if ! git show-ref --verify --quiet \"refs/remotes/origin/$default\"; then default=\"master\"; fi; if git show-ref --verify --quiet \"refs/remotes/origin/$default\"; then base=$(git merge-base HEAD \"origin/$default\"); git rebase -i \"$base\"; else echo \"Could not find default branch (main/master). Usage: git squash-all <base-branch>\"; fi; }; f";
# Undo and reset aliases
# =====
# Interactive undo: pick state from reflog, show diff, confirm before hard reset
undo = "!f() { target=$(git reflog --color=always --date=relative --format=\"%C(yellow)%h%C(reset) %C(cyan)%gd%C(reset) %C(green)%gs%C(reset)\" | fzf --ansi --height 40% --header=\"Select a state to reset to\" | awk '{print $1}'); if [ -n \"$target\" ]; then echo \"\n=== Diff between HEAD and selected state ===\n\"; git diff --color=always HEAD \"$target\"; echo \"\n=== This will discard all changes above ===\n\"; read -p \"Reset to $target? (y/N): \" confirm; if [ \"$confirm\" = \"y\" ] || [ \"$confirm\" = \"Y\" ]; then git reset --hard \"$target\"; else echo \"Reset cancelled.\"; fi; fi; }; f";
# Unstage all staged files
unstage = "reset HEAD --";
# Other aliases
# =====
# Initialize and update submodules
sobmodules = "submodule update --init --recursive";
# Diff tool using nvimdiff
df = "difftool -t nvimdiff -y";
};
};
};
};
}