repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
setup.shShell108 lines4.3 KiBraw download
#!/usr/bin/env bash
# Setup / cutover for the modular dotfiles. Reversible: --revert removes our
# ~/.zshenv block and tmux symlink, restoring your previous config.
#
#   setup.sh --link [--with-deps]   backup + plugins + cmds + ~/.zshenv + tmux + bin
#   setup.sh --bin                  symlink bin/* into ~/.local/bin only
#   setup.sh --deps                 install dependencies only (brew/apt)
#   setup.sh --revert               undo --link
#   setup.sh --check                show cutover state
#   setup.sh --test                 boot-timing + DB self-test
set -euo pipefail
DOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ZSHENV="$HOME/.zshenv"
MARK="# >>> dotfiles ZDOTDIR >>>"
ENDMARK="# <<< dotfiles ZDOTDIR <<<"

os() { case "$(uname -s)" in Darwin) echo mac ;; Linux) echo linux ;; *) echo other ;; esac; }

deps() {
  local pkgs=(zsh tmux git sqlite3 fzf eza starship atuin zoxide fastfetch)
  if [ "$(os)" = mac ]; then
    command -v brew >/dev/null || { echo "install Homebrew first: https://brew.sh"; return 1; }
    echo "installing via brew: ${pkgs[*]}"
    brew install "${pkgs[@]}" || true
  elif command -v apt >/dev/null; then
    echo "installing via apt (sudo): ${pkgs[*]}"
    sudo apt update -y && sudo apt install -y zsh tmux git sqlite3 fzf || true
    # eza/starship/atuin/zoxide/fastfetch often need extra repos; install best-effort
    command -v cargo >/dev/null && cargo install eza zoxide 2>/dev/null || true
    command -v starship >/dev/null || curl -sS https://starship.rs/install.sh | sh -s -- -y || true
  else
    echo "unknown package manager — install manually: ${pkgs[*]}"
  fi
}

backup() {
  local ts dir; ts="$(date +%Y%m%d-%H%M%S)"; dir="$HOME/.dotfiles-backup-$ts"
  mkdir -p "$dir"
  local f
  for f in .zshrc .zshenv .zprofile .pentest.zsh .pentest-mac.zsh .tmux.conf; do
    if [ -e "$HOME/$f" ] && [ ! -L "$HOME/$f" ]; then
      cp -a "$HOME/$f" "$dir/" && echo "backed up $f -> $dir"
    fi
  done
}

# Symlink everything in bin/ into ~/.local/bin so the tools are on PATH.
# Only ever replaces links that already point back into this repo.
linkbin() {
  local target="$HOME/.local/bin" f dest
  mkdir -p "$target"
  for f in "$DOT"/bin/*; do
    [ -f "$f" ] || continue
    chmod +x "$f"
    dest="$target/$(basename "$f")"
    if [ -e "$dest" ] && [ ! -L "$dest" ]; then
      echo "skip $(basename "$f"): $dest exists and is not a symlink"
      continue
    fi
    ln -sfn "$f" "$dest" && echo "linked $dest -> $f"
  done
  case ":$PATH:" in
    *":$target:"*) ;;
    *) echo "note: $target is not on PATH" ;;
  esac
}

link() {
  backup
  "$DOT/scripts/install-plugins.sh"
  "$DOT/scripts/gen-pentest-cmds.sh"
  if ! grep -q "$MARK" "$ZSHENV" 2>/dev/null; then
    { echo "$MARK"; echo 'export ZDOTDIR="$HOME/.dotfiles"'; echo "$ENDMARK"; } >> "$ZSHENV"
    echo "wrote ZDOTDIR to $ZSHENV"
  fi
  ln -sfn "$DOT/.tmux.conf" "$HOME/.tmux.conf" && echo "linked ~/.tmux.conf -> $DOT/.tmux.conf"
  mkdir -p "$HOME/.config/cheats"; for d in PENTESTING:pentesting DATABASE:database TMUX:tmux CHEATSHEET:cheatsheet; do ln -sf "$DOT/docs/${d%%:*}.md" "$HOME/.config/cheats/${d##*:}.md"; done; echo "linked pentest cheatsheets"
  chmod +x "$DOT"/scripts/*.sh 2>/dev/null || true
  linkbin
  echo "Done. Open a new shell. Revert anytime: $0 --revert"
}

revert() {
  if [ -f "$ZSHENV" ] && grep -q "$MARK" "$ZSHENV"; then
    sed -i.bak "/$MARK/,/$ENDMARK/d" "$ZSHENV" && rm -f "$ZSHENV.bak"
    [ -s "$ZSHENV" ] || rm -f "$ZSHENV"
    echo "removed ZDOTDIR block from $ZSHENV"
  fi
  if [ -L "$HOME/.tmux.conf" ]; then rm -f "$HOME/.tmux.conf" && echo "removed ~/.tmux.conf symlink"; fi
  echo "reverted: your previous ~/.zshrc is active again (restore ~/.tmux.conf from a backup dir if needed)"
}

selftest() {
  echo "OS: $(os)"
  echo -n "boot time: "
  ZDOTDIR="$DOT" /usr/bin/time -p zsh -ic exit 2>&1 | awk '/real/{print $2"s"}'
  echo "run full suite: zsh $DOT/scripts/test.zsh"
}

case "${1:-}" in
  --link)   link; [ "${2:-}" = "--with-deps" ] && deps ;;
  --bin)    linkbin ;;
  --deps)   deps ;;
  --revert) revert ;;
  --check)  echo "DOT=$DOT  ZSHENV=$ZSHENV"; { [ -f "$ZSHENV" ] && grep -q "$MARK" "$ZSHENV" && echo "ZDOTDIR ACTIVE"; } || echo "ZDOTDIR INACTIVE"; [ -L "$HOME/.tmux.conf" ] && echo "tmux LINKED" || echo "tmux not linked" ;;
  --test)   selftest ;;
  *) echo "usage: setup.sh --link [--with-deps] | --bin | --deps | --revert | --check | --test"; exit 1 ;;
esac