Skip to content

Commit

Permalink
trivial-builders: init addTmateBreakpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewCroughan committed Nov 20, 2024
1 parent a44b532 commit b3e3bf9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
41 changes: 40 additions & 1 deletion pkgs/build-support/trivial-builders/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, config, stdenv, stdenvNoCC, jq, lndir, runtimeShell, shellcheck-minimal }:
{ lib, config, stdenv, stdenvNoCC, jq, lndir, unixtools, tmate, runtimeShell, shellcheck-minimal }:

let
inherit (lib)
Expand Down Expand Up @@ -602,6 +602,45 @@ rec {
ln -s ${lib.getBin drv}/bin $out/bin
'';

/*
Adds a hook to the nativeBuildInputs of a derivation which will provide an
SSH session via tmate to the build environment, for interactive
debugging
Example:
# Spawn a tmate session after the `buildPhase` of the `hello` derivation
addTmateBreakpoint (pkgs.hello.overrideAttrs { buildPhase = "exit 1;"; })
*/
addTmateBreakpoint = let
scriptFlags = if stdenv.hostPlatform.isDarwin then "-qeF ./tmpfile" else "-qefc";
warningMessage = "addTmateBreakpoint in use, ignore the sha256 warning and do not leak these build logs, otherwise unauthorized ssh access to the build sandbox may occur";
hook = lib.warn warningMessage (makeSetupHook {} (writeTextFile {
name = "hook.sh";
text = ''
breakpointHookTmate() {
local red='\033[0;31m'
local no_color='\033[0m'
echo -e "''${red}build failed in ''${curPhase} with exit code ''${exitCode}''${no_color}"
echo -e "''${red}### WARNING ###''${no_color}"
echo -e "''${red}${warningMessage}''${no_color}"
printf "To attach using tmate:\n\n"
${unixtools.script}/bin/script ${scriptFlags} "${tmate}/bin/tmate -F"
}
failureHooks+=(breakpointHookTmate)
'';
}));
in drv: drv.overrideAttrs (old: {
name = "${drv.name}-addTmateBreakpoint-${builtins.unsafeDiscardStringContext (lib.substring 0 12 (baseNameOf drv.drvPath))}";
nativeBuildInputs = (old.nativeBuildInputs or []) ++ [
hook
];
outputHash = "";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
});

# Docs in doc/build-helpers/special/makesetuphook.section.md
# See https://nixos.org/manual/nixpkgs/unstable/#sec-pkgs.makeSetupHook
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ addTmateBreakpoint, hello }: (addTmateBreakpoint hello).overrideAttrs { postPatch = "exit 1"; }
3 changes: 3 additions & 0 deletions pkgs/build-support/trivial-builders/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ recurseIntoAttrs {
inherit (references) samples;
};
writeTextFile = callPackage ./write-text-file.nix {};
manualTesting = lib.dontRecurseIntoAttrs {
addTmateBreakpoint = callPackage ./addTmateBreakpoint.nix {};
};
}
2 changes: 1 addition & 1 deletion pkgs/top-level/stage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ let
inherit lib;
inherit (self) config;
inherit (self) runtimeShell stdenv stdenvNoCC;
inherit (self.pkgsBuildHost) jq shellcheck-minimal;
inherit (self.pkgsBuildHost) jq shellcheck-minimal unixtools tmate;
inherit (self.pkgsBuildHost.xorg) lndir;
};

Expand Down

0 comments on commit b3e3bf9

Please sign in to comment.