Skip to content

Instantly share code, notes, and snippets.

@KiaraGrouwstra
Last active April 22, 2024 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KiaraGrouwstra/70bf11002032b3c265512f4e17607631 to your computer and use it in GitHub Desktop.
Save KiaraGrouwstra/70bf11002032b3c265512f4e17607631 to your computer and use it in GitHub Desktop.
debugging nix builds using git bisect

System flake

git bisect start $BAD $GOOD && git bisect run $CMD

nixpkgs package

either:

  • nix-bisect: smartly pick bisect bad/skip in automated bisects and give nicer outputs
  • hydrasect: prioritize cached commits in nixpkgs bisects - unfortunately still lacks nix package/flake
    • after each checkout run git checkout $(hydrasect-search | head -1)
  • nixpkgs-staging-bisecter: like hydrasect but minimize how many derivations you will build even in staging
  • run in nixpkgs repo, e.g. for signal-desktop:
cat >> .test<< EOF
#! /usr/bin/env bash
$(nix-build -A signal-desktop)/bin/signal-desktop --use-tray-icon --no-sandbox
EOF
chmod +x ./test
git bisect start -- pkgs/applications/networking/instant-messengers/signal-desktop/
git bisect bad
git bisect run sh -c './test; [ $? -eq 0 ]'

Flake input

From the dependency's repo, run:

nix --extra-experimental-features 'nix-command flakes' shell nixpkgs#git
git bisect start $BAD $GOOD

export DEPENDENCY_INPUT=nixpkgs
export DEPENDENCY_URL=https://github.com/NixOS/nixpkgs
export SYSTEM_REPO=<MY_SYSTEM_FLAKE_REPO_PATH>
export DEPENDENCY_PATH=<DEPENDENCY_REPO_PATH>

# if judgement needs manual intervention, cycles of:
$SYSTEM_REPO/bisect.sh $DEPENDENCY_PATH $DEPENDENCY_URL $SYSTEM_REPO
# nix --extra-experimental-features 'nix-command flakes' shell nixpkgs#git -- checkout $(hydrasect-search | head -1)
# ^ if the dep is nixpkgs
# <TEST>
git bisect good
git bisect bad

# if judgement can be automated:
git bisect run "$SYSTEM_REPO/bisect.sh $DEPENDENCY_PATH $DEPENDENCY_URL $SYSTEM_REPO && <COMMAND>"
#!/usr/bin/env -S nix --extra-experimental-features 'nix-command flakes' shell nixpkgs#git nixpkgs#jaq --command sh
# # bisecting dep's perspective
# # after: git bisect $BAD $GOOD
# export DEPENDENCY_INPUT=niri-src
# export DEPENDENCY_URL=https://github.com/YaLTeR/niri
# export SYSTEM_REPO=~/Downloads/nix/nixos
export DEPENDENCY_INPUT=$0
export DEPENDENCY_URL=$1
export SYSTEM_REPO=$2
LOCK_FILE=$SYSTEM_REPO/flake.lock
CURRENT_COMMIT=$(cat $LOCK_FILE | jaq -r '.nodes["${DEPENDENCY_INPUT}"].locked.rev')
TARGET_COMMIT=$(git rev-parse HEAD)
sed -E -i "s/$CURRENT_COMMIT/$TARGET_COMMIT/g" $LOCK_FILE
CURRENT_HASH=$(cat $LOCK_FILE | jaq -r '.nodes["${DEPENDENCY_INPUT}"].locked.narHash')
## the hash i get seems off
# TARGET_HASH=$(nix-prefetch-url "${DEPENDENCY_URL}/archive/${TARGET_COMMIT}.zip" --type sha256)
# sed -E -i "s%$(echo $CURRENT_HASH | sed -E "s/\+/\\\\+/g")%$TARGET_HASH%g" $LOCK_FILE
sed -E -i "s%$(echo $CURRENT_HASH | sed -E "s/\+/\\\\+/g")%%g" $LOCK_FILE
export DEPENDENCY_PATH=$PWD
cd $SYSTEM_REPO
sudo nixos-rebuild switch --flake .#default
cd $DEPENDENCY_PATH
@KiaraGrouwstra
Copy link
Author

KiaraGrouwstra commented Apr 21, 2024

cd Downloads/nixpkgs/
# git bisect bad/good
../cfg/bisect.sh
vim ../cfg/flake.lock
cd ../cfg; sudo nixos-rebuild boot --flake .#default; cd ../nixpkgs
reboot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment