# modules/features/pentest/_sets.nix — the category factory.
#
# Not a flake-parts module: the path contains `/_`, so import-tree skips it
# (flake.nix). Every category file imports it and calls mkCategory once.
#
# One category is declared in one place — its package list — and this derives
# the four things that list feeds:
#
# flake.lib.pentestPackages.<name>
# the list itself, as pkgs -> [package].
# devshells.nix unions these.
# flake.nixosModules.pentest-<name>
# environment.systemPackages when the category
# is on. System, not home: half this toolkit
# needs root (`sudo nmap -sS`), and
# home-manager packages are not on root's PATH.
# perSystem.checks.pentest-<name>
# the smoke test: every name in expectedBins
# resolves on PATH. Catches the common failure
# where an attribute exists but its binary is
# called something else (netexec -> nxc,
# snmpcheck -> snmp-check, bloodyad -> bloodyAD).
# perSystem.devShells.pentest-<name>
# the same list, portable: `nix develop`.
#
# The category declares its own `daemon.pentest.<name>.enable`; options.nix
# holds only the master switch and the options no category owns.
{ lib }:
{
name,
description,
packages,
expectedBins ? [ ],
# Binaries to actually RUN (with --help), not merely resolve. `command -v`
# cannot see a tool that installs and then dies on a missing python module or
# a bad interpreter — which is how `masky` shipped broken. Spec Testing #2
# asks for exactly this. Non-zero exit is tolerated (plenty of tools exit 1
# on --help); an import or loader error is not.
smokeBins ? [ ],
# Extra assertions for a category whose deliverable is not a binary (a
# wordlist tree, a payload tree). Takes { pkgs, lib }, returns shell appended
# to the check; fail with a non-zero exit and a message naming what is wrong.
checkScript ? (_: ""),
# Merged into the gated config, so a category that is off cannot turn on a
# NixOS service. Called with { config, pkgs, lib, user }; take what you need
# and end the pattern with `...`.
extraConfig ? (_: { }),
default ? true,
}:
{
flake.lib.pentestPackages.${name} = packages;
flake.nixosModules."pentest-${name}" =
# `user` comes from the nixosSystem's specialArgs (modules/hosts/laptop/
# default.nix), the same way vpn.nix and htb.nix take it. A category whose
# extraConfig grants a group membership needs the username, and hardcoding
# it in the category would make the file host-specific.
{ config, pkgs, lib, user, ... }:
let
cfg = config.daemon.pentest;
on = cfg.enable && cfg.${name}.enable;
in
{
# The default follows the MASTER switch: with daemon.pentest.enable off,
# every category defaults off too. Otherwise turning the toolkit off
# leaves 12 categories defaulted on and the assertion in options.nix
# fires, which made `enable = false` an eval error rather than simply
# "no toolkit" — and the spec gives the master switch default false.
# An explicit `daemon.pentest.<cat>.enable = true` still wins, and the
# assertion still catches that genuine contradiction.
options.daemon.pentest.${name}.enable = lib.mkEnableOption description // {
default = cfg.enable && default;
};
config = lib.mkIf on (
lib.mkMerge [
# Only what exists for this machine's platform: on aarch64 a few
# tools are x86-only (and fail evaluation outright). On x86_64 every
# package passes, so this filters nothing there.
{
environment.systemPackages = builtins.filter
(lib.meta.availableOn pkgs.stdenv.hostPlatform)
(packages pkgs);
}
(extraConfig { inherit config pkgs lib user; })
]
);
};
perSystem =
{ pkgs, ... }:
{
checks."pentest-${name}" = pkgs.runCommand "pentest-${name}-check"
{
nativeBuildInputs = packages pkgs;
passthru.expectedBins = expectedBins;
}
''
# The category's OWN binaries first. nativeBuildInputs also puts
# every propagated dependency's bin/ on PATH, which is how a
# python3.12 pywerview from another package's closure shadowed the
# python3.14 one this category actually installs — and made a working
# tool look broken. environment.systemPackages installs exactly this
# list, so this is the faithful PATH.
export PATH=${lib.makeBinPath (packages pkgs)}:$PATH
missing=""
for b in ${lib.escapeShellArgs expectedBins}; do
command -v "$b" >/dev/null 2>&1 || missing="$missing $b"
done
if [ -n "$missing" ]; then
echo "pentest-${name}: expected binaries not on PATH:$missing" >&2
echo " (the attribute built, but its binary is named something else)" >&2
exit 1
fi
# A writable HOME: several of these create a config directory on
# first run, and the build sandbox's HOME is /homeless-shelter.
# Without this, nxc fails with FileNotFoundError on ~/.nxc and the
# smoke test reports a defect that does not exist on a real machine.
export HOME=$(mktemp -d)
# NIX_PYTHONPATH as well as PYTHONPATH: nixpkgs' python setup hook
# uses the NIX_ one, and clearing only PYTHONPATH left the leak in
# place. Every python package in this check's
# nativeBuildInputs puts its modules on PYTHONPATH, so one tool's
# python3.12 impacket shadows another's python3.14 one and the tool
# dies on an import. That is an artifact of the check, not a defect —
# it produced false positives for pywerview and donpapi, and I
# briefly removed a working tool because of it. A user's shell has no
# PYTHONPATH, so clearing it is also the faithful test.
for b in ${lib.escapeShellArgs smokeBins}; do
# NOT a variable called `out`: that is the derivation's own
# output path, and clobbering it makes the final redirect
# ambiguous. (Second time I have made this mistake in this repo.)
smoke_out=$(env -u PYTHONPATH -u PYTHONHOME -u NIX_PYTHONPATH \
-u NIX_PYTHONPREFIX -u NIX_PYTHONEXECUTABLE \
"$b" --help 2>&1 </dev/null | head -40 || true)
case "$smoke_out" in
*ModuleNotFoundError*|*"ImportError"*|*"No such file or directory"*|\
*"command not found"*|*"cannot open shared object"*)
echo "pentest-${name}: '$b' is installed but cannot run:" >&2
printf '%s\n' "$smoke_out" | sed 's/^/ /' >&2
exit 1 ;;
esac
done
${checkScript { inherit pkgs lib; }}
echo "pentest-${name}: ${toString (builtins.length expectedBins)} binaries ok" > $out
'';
devShells."pentest-${name}" = pkgs.mkShell {
name = "pentest-${name}";
packages = builtins.filter (lib.meta.availableOn pkgs.stdenv.hostPlatform) (packages pkgs);
};
};
}