Skip to content

Instantly share code, notes, and snippets.

@lionello
Created November 26, 2018 06:56
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 lionello/da7564f6a7ddf95c9bc48cf34cd75e66 to your computer and use it in GitHub Desktop.
Save lionello/da7564f6a7ddf95c9bc48cf34cd75e66 to your computer and use it in GitHub Desktop.
Little bash script to generate shell.nix default.nix
#!/usr/bin/env bash
usage () {
echo "Usage: $(basename $0) [--direnv] [--shell] [--default] -p|--packages packages... [--test]"
exit 0
}
error () {
echo "Error: $1"
exit 1
}
DIRENV=0
SHELL=0
DEFAULT=0
TEST=0
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
usage
;;
--test)
TEST=1
shift
;;
--test)
TEST=1
shift
;;
-p|--packages)
shift
;;
--default)
DEFAULT=1
shift
;;
--direnv)
DIRENV=1
shift
;;
--shell)
SHELL=1
shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
[ ${#POSITIONAL[@]} = 0 ] && error "Provide one or more package names as arguments."
TMP="$(mktemp)"
cat >"$TMP" <<EOF
with (import <nixpkgs> {});
mkShell {
buildInputs = [
${POSITIONAL[@]}
];
}
EOF
[ $DEFAULT = 1 ] && cp -i "$TMP" ./default.nix
[ $SHELL = 1 ] && cp -i "$TMP" ./shell.nix
[ $DEFAULT = 0 ] && [ $SHELL = 0 ] && cat "$TMP"
[ $DIRENV = 1 ] && echo use nix >> ./.envrc
rm "$TMP"
[ $TEST = 1 ] && exec nix-shell -p ${POSITIONAL[@]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment