Skip to content

Instantly share code, notes, and snippets.

@SCOTT-HAMILTON
Last active July 12, 2022 18:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SCOTT-HAMILTON/63070397f9369c44fa47c26750531803 to your computer and use it in GitHub Desktop.
Save SCOTT-HAMILTON/63070397f9369c44fa47c26750531803 to your computer and use it in GitHub Desktop.
Get nix executable dependencies required to run a shell command for a certain a time
#! /usr/bin/env nix-shell
#! nix-shell -i sh -p coreutils findutils gnused ripgrep strace
# Usage ./get-command-nix-deps.sh ./script.sh 10s
timeout "$2" strace -qqfe execve sh -c "$1" 2>&1 >/dev/null | \
rg '(/nix/store|/run/current-system/sw/bin)' | \
sed -E 's=.*execve\("(\/.*)", \[.*=\1=g' | \
xargs readlink -f | sort -u | \
sed -E 's=/nix/store/[0-9a-z]*-(.*)-(.*)/.*/.*=\1 (\2)=g' | sort -u
# Example:
# Get the dependencies for command ls, let it run for 2s before killing it if it's not finished (using timeout).
# You'll unfortunatly get bash-interactive even for commands that don't use it.
# $ ./get-command-nix-deps.sh 'ls -lh' 2s
# bash-interactive-5.1 (p16)
# coreutils (9.0)
# $ ./get-command-nix-deps.sh 'curl --version' 2s
# bash-interactive-5.1 (p16)
# curl-7.83.1 (bin)
# $ ./get-command-nix-deps.sh 'systemctl status' 2s
# bash-interactive-5.1 (p16)
# systemd (250.4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment