# modules/home/cheats.nix — the cheat cards this repo ships, in the house
# style (~/.local/bin/*-cheat, rendered with the toolbox's cheat-render).
# Every modules/home/cheats/<name>.md becomes a `<name>-cheat` command:
#
# nix-cheat rebuilding this machine: layout, nixos-rebuild, nh, add, desktop (Hyprland or Niri), secrets, repo
# nixmod-cheat finding modules, creating them, settings, templates, validation and fixes
# gpg-cheat GnuPG from the key hierarchy to signing, encryption, SSH, git, offline primary, fixes
# niri-cheat Niri + Noctalia: every bind, Noctalia ipc, niri msg, wallpaper, settings, fixes
# pentest-cheat the offensive toolkit: htb workflow, recon, AD, pivoting, transfer, cracking, web, DFIR
# htb-cheat htbbox, htbcast, the target: new box, clear, sessions, files, env
#
# <name>-cheat the whole card <name>-cheat --list the section names
# <name>-cheat SECTION one section <name>-cheat --raw the markdown itself
#
# Rendered with cheat-render when that is on PATH, else glow, else printed
# plain. These cards live here (not in the dotfiles) because they document
# this repo and this machine; xcheats only lists ~/.local/bin cards, so call
# these by name.
{ ... }:
{
flake.homeModules.cheats =
{ pkgs, lib, ... }:
let
cards = [ "nix" "nixmod" "gpg" "niri" "pentest" "htb" ];
mkCheat = name: pkgs.writeShellScriptBin "${name}-cheat" ''
set -uo pipefail
card=${./cheats + "/${name}.md"}
usage() {
echo "usage: ${name}-cheat [SECTION] [--list] [--raw] [--help]"
echo " sections: $(sections | tr '\n' ' ')"
}
sections() { # first word of each '## ' heading
${pkgs.gnused}/bin/sed -n 's/^## \([a-z-]*\).*/\1/p' "$card"
}
section() { # the heading whose first word matches $1, up to the next heading
${pkgs.gawk}/bin/awk -v want="$1" '
/^## / { on = (tolower($2) == want) }
/^# / { next }
on
' "$card"
}
render() {
if [ ! -t 1 ]; then cat
elif command -v cheat-render >/dev/null 2>&1; then cheat-render - | ''${PAGER:-less -R}
else ${pkgs.glow}/bin/glow -p -
fi
}
case "''${1:-}" in
-h|--help) usage; exit 0 ;;
--list) sections; exit 0 ;;
--raw) cat "$card"; exit 0 ;;
"") render < "$card" ;;
*)
key=$(echo "$1" | tr '[:upper:]' '[:lower:]')
out=$(section "$key")
if [ -z "$out" ]; then
echo "${name}-cheat: no section '$1'" >&2; usage >&2; exit 1
fi
{ head -1 "$card"; echo; printf '%s\n' "$out"; } | render ;;
esac
'';
in
{
home.packages = map mkCheat cards;
}
;
}