Created
November 26, 2018 06:56
-
-
Save lionello/da7564f6a7ddf95c9bc48cf34cd75e66 to your computer and use it in GitHub Desktop.
Little bash script to generate shell.nix default.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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