summaryrefslogtreecommitdiffstats
path: root/nix-script-switch.sh
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2015-06-05 14:13:50 +0200
committerMatthias Beyer <mail@beyermatthias.de>2015-06-06 11:09:08 +0200
commit85b639e9a2cb04e8bdcad0b9d34934ada4a8c2e9 (patch)
tree05922a8103a29dced8507120f405af0aadcb6282 /nix-script-switch.sh
parent41f8794886d8685d21e3fdc0cb62320d30e9b742 (diff)
Add tool for switching configuration and tagging config after successfull build
Diffstat (limited to 'nix-script-switch.sh')
-rwxr-xr-xnix-script-switch.sh108
1 files changed, 108 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
+