summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2015-06-06 11:09:38 +0200
committerMatthias Beyer <mail@beyermatthias.de>2015-06-06 11:09:38 +0200
commit22d8cd9c2899959406ebbe8d033e0274608f58a4 (patch)
tree05922a8103a29dced8507120f405af0aadcb6282
parentc16b3350c7ea4920e44567d4b67bc8614a956f39 (diff)
parent85b639e9a2cb04e8bdcad0b9d34934ada4a8c2e9 (diff)
Merge pull request #15 from matthiasbeyer/add-switch
Add switch
-rwxr-xr-xnix-script-switch.sh108
-rw-r--r--nix-utils.sh5
2 files changed, 113 insertions, 0 deletions
diff --git a/nix-script-switch.sh b/nix-script-switch.sh
new file mode 100755
index 0000000..ae1bd93
--- /dev/null
+++ b/nix-script-switch.sh
@@ -0,0 +1,108 @@
+#!/usr/bin/env bash
+
+source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh
+
+CONFIG_DIR=
+
+COMMAND="switch"
+
+usage() {
+ cat <<EOS
+
+ $(help_synopsis "${BASH_SOURCE[0]}" "[-h] [-c <command>] [-g <git command>] -w <working directory> [-- args...]")
+
+ -c <command> Command for nixos-rebuild. See 'man nixos-rebuild'
+ -g <git cmd> Alternative git commit, defaults to 'tag -a'
+ -w <path> Path to your configuration git directory
+ -h Show this help and exit
+
+ Everything after a double dash (--) will be passed to nixos-rebuild as
+ additional parameters. For example:
+
+ nix-script switch -c switch -- -I nixpkgs=/home/user/pkgs
+
+$(help_end)
+EOS
+}
+
+COMMAND=
+ARGS=
+WD=
+TAG_NAME=
+GIT_COMMAND=
+
+while getopts "c:w:t:h" OPTION
+do
+ case $OPTION in
+ c)
+ COMMAND=$OPTARG
+ stdout "COMMAND = $COMMAND"
+ ;;
+ w)
+ WD=$OPTARG
+ stdout "WD = $WD"
+ ;;
+ t)
+ TAG_NAME=$OPTARG
+ stdout "TAG_NAME = $TAG_NAME"
+ ;;
+
+ g)
+ GIT_COMMAND=$OPTARG
+ stdout "GIT_COMMAND = $GIT_COMMAND"
+ ;;
+
+ h)
+ usage
+ exit 1
+ ;;
+ esac
+done
+
+ARGS=$(echo $* | sed -r 's/(.*)\-\-(.*)/\2/')
+stdout "ARGS = $ARGS"
+
+if [[ -z "$WD" ]]
+then
+ stderr "No configuration git directory."
+ stderr "Won't do anything"
+ exit 1
+fi
+
+if [[ ! -d "$WD" ]]
+then
+ stderr "No directory: $WD"
+ exit 1
+fi
+
+if [[ -z "$COMMAND" ]]
+then
+ COMMAND="switch"
+fi
+
+if [[ -z "$GIT_COMMAND" ]]
+then
+ GIT_COMMAND="tag -a"
+fi
+
+explain sudo nixos-rebuild $COMMAND $ARGS
+REBUILD_EXIT=$?
+
+if [[ $REBUILD_EXIT -eq 0 ]]
+then
+ LASTGEN=$(sudo nix-env -p /nix/var/nix/profiles/system --list-generations |\
+ grep current | cut -d " " -f 2)
+ sudo -k
+
+ if [[ -z "$TAG_NAME" ]]
+ then
+ TAG_NAME="nixos-$LASTGEN-$COMMAND"
+ fi
+
+ explain git --git-dir="$WD/.git" --work-tree="$WD" $GIT_COMMAND "'$TAG_NAME'"
+
+else
+ stderr "Switching failed. Won't executing any further commands."
+ exit $REBUILD_EXIT
+fi
+
diff --git a/nix-utils.sh b/nix-utils.sh
index 49b7b29..3ff20df 100644
--- a/nix-utils.sh
+++ b/nix-utils.sh
@@ -29,6 +29,11 @@ help_end() {
echo ""
}
+explain() {
+ stdout "$*"
+ $*
+}
+
grep_generation() {
$* | grep current | cut -d " " -f 2
}