diff options
-rwxr-xr-x | nix-script-switch.sh | 153 |
1 files changed, 82 insertions, 71 deletions
diff --git a/nix-script-switch.sh b/nix-script-switch.sh index 749b929..bd26c76 100755 --- a/nix-script-switch.sh +++ b/nix-script-switch.sh @@ -9,16 +9,31 @@ COMMAND="switch" usage() { cat <<EOS - $(help_synopsis "${BASH_SOURCE[0]}" "[-h] [-q] [-c <command>] [-w <working directory>] [-- args...]") + $(help_synopsis "${BASH_SOURCE[0]}" \ + "[-h] [-q] [-c <command>] [-w <working directory>]") + + -c <command> Command for nixos-rebuild. + See 'man nixos-rebuild' (default: switch) + + -w <path> Path to your configuration git directory + (default: '$RC_CONFIG') - -c <command> Command for nixos-rebuild. See 'man nixos-rebuild' (default: switch) - -w <path> Path to your configuration git directory (default: '$RC_CONFIG') -n DON'T include hostname in tag name + -t <tagname> Custom tag name - -p [<pkgs>] Generate the switch tag in the nixpkgs at <pkgs> as well. (default: '$RC_NIXPKGS') - -f <tag-flags> Flags for git-tag (see 'git tag --help') (default: '$RC_SWITCH_DEFAULT_TAG_FLAGS') + + -p [<pkgs>] Generate the switch tag in the nixpkgs at <pkgs> + as well. (default: '$RC_NIXPKGS') + + -f <tag-flags> Flags for git-tag (see 'git tag --help') + (default: '$RC_SWITCH_DEFAULT_TAG_FLAGS') + -b Do not call nixos-rebuild at all. + -q Don't pass -Q to nixos-rebuild + + -s <pkgs> Use these nixpkgs clone instead of channels + -h Show this help and exit This command helps you rebuilding your system and keeping track @@ -35,14 +50,8 @@ usage() { # in the tag as well (helpful if your configuration is # shared between hosts). # Verbosity is on here. - nix-script -v switch -c switch -w /home/me/config -n - 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 - - can be used to use your local clone of the nixpkgs repository. + nix-script -v switch -c switch -w /home/me/config -n $(help_rcvars \ "RC_CONFIG - Path of your system configuration (git) directory"\ @@ -55,7 +64,7 @@ $(help_end "${BASH_SOURCE[0]}") EOS } -COMMAND= +COMMAND=switch ARGS= WD=$RC_CONFIG TAG_NAME= @@ -66,24 +75,29 @@ TAG_FLAGS="$RC_SWITCH_DEFAULT_TAG_FLAGS" TAG_FLAGS_NIXPKGS="$RC_SWITCH_DEFAULT_TAG_FLAGS_NIXPKGS" DONT_BUILD= QUIET=1 +USE_ALTERNATIVE_SOURCE_NIXPKGS=0 -while getopts "c:w:t:nbp:f:qh" OPTION +while getopts "c:w:t:nbp:f:qs:h" OPTION do case $OPTION in c) COMMAND=$OPTARG + dbg "COMMAND = $COMMAND" ;; w) WD=$OPTARG + dbg "WD = $WD" ;; t) TAG_NAME=$OPTARG + dbg "TAG_NAME = $TAG_NAME" ;; n) HOSTNAME="" + dbg "HOSTNAME = $HOSTNAME" ;; p) @@ -93,9 +107,11 @@ do fi TAG_NIXPKGS=1 ;; + dbg "TAG_NIXPKGS = $TAG_NIXPKGS" f) TAG_FLAGS=$OPTARG + dbg "TAG_FLAGS = $TAG_FLAGS" ;; b) @@ -108,6 +124,13 @@ do dbg "QUIET = $QUIET" ;; + s) + USE_ALTERNATIVE_SOURCE_NIXPKGS=1 + ALTERNATIVE_SOURCE_NIXPKGS="$OPTARG" + dbg "USE_ALTERNATIVE_SOURCE_NIXPKGS = $USE_ALTERNATIVE_SOURCE_NIXPKGS" + dbg "ALTERNATIVE_SOURCE_NIXPKGS = $ALTERNATIVE_SOURCE_NIXPKGS" + ;; + h) usage exit 1 @@ -115,88 +138,76 @@ do esac done -# -# Function to generate the tag at $NIXPKGS as well -# -tag_nixpkgs() { - if [[ ! -d "$1" ]] - then - stderr "'$1' is not a directory, so can't be a nixpkgs clone" - return - fi - - commit=$(nixos-version | cut -d . -f 3 | cut -d " " -f 1) - - c_txt="Trying to create tag '$TAG_NAME' at '$1' on commit '$commit'" - continue_question "$c_txt" || return - - __git "$1" tag $TAG_FLAGS_NIXPKGS "$TAG_NAME" $commit || \ - stderr "Could not create tag in nixpkgs clone" -} - - -dbg "COMMAND = $COMMAND" -dbg "WD = $WD" -dbg "TAG_NAME = $TAG_NAME" -dbg "HOSTNAME = $HOSTNAME" -dbg "NIXPKGS = $NIXPKGS" -dbg "TAG_FLAGS = $TAG_FLAGS" ARGS=$(echo $* | sed -r 's/(.*)(\-\-(.*)|$)/\2/') -dbg "ARGS = $ARGS" +dbg "ARGS = $ARGS" [[ -z "$WD" ]] && \ stderr "No configuration git directory." && \ stderr "Won't do anything" && exit 1 -[[ ! -d "$WD" ]] && stderr "No directory: $WD" && exit 1 -[[ -z "$COMMAND" ]] && COMMAND="switch" +[[ ! -d "$WD" ]] && stderr "No directory: $WD" && exit 1 + +TAG_TARGET=$(__git "$WD" rev-parse HEAD) +stdout "Tag in config will be generated at '$TAG_TARGET'" if [[ -z "$DONT_BUILD" ]] then __q="-Q" [[ $QUIET -eq 0 ]] && __q="" - explain sudo nixos-rebuild $__q $COMMAND $ARGS + + __altnixpkgs="" + [[ $USE_ALTERNATIVE_SOURCE_NIXPKGS -eq 1 ]] && \ + [[ -d "$ALTERNATIVE_SOURCE_NIXPKGS" ]] && \ + __altnixpkgs="-I nixpkgs=$ALTERNATIVE_SOURCE_NIXPKGS" + + explain sudo nixos-rebuild $__q $COMMAND $ARGS $__altnixpkgs REBUILD_EXIT=$? else stdout "Do not call nixos-rebuild" REBUILD_EXIT=0 fi -if [[ $REBUILD_EXIT -eq 0 ]] -then - LASTGEN=$(current_system_generation) - sudo -k +[[ ! $REBUILD_EXIT -eq 0 ]] && \ + stderr "Switching failed. Won't executing any further commands." && \ + exit $REBUILD_EXIT - stdout "sudo -k succeeded" - stdout "Last generation was: $LASTGEN" +LASTGEN=$(current_system_generation) +sudo -k - if [[ -z "$TAG_NAME" ]] - then - if [[ -z "$HOSTNAME" ]]; then TAG_NAME="nixos-$LASTGEN-$COMMAND" - else TAG_NAME="nixos-$HOSTNAME-$LASTGEN-$COMMAND" - fi +stdout "sudo -k succeeded" +stdout "Last generation was: $LASTGEN" + +if [[ -z "$TAG_NAME" ]] +then + if [[ -z "$HOSTNAME" ]]; then TAG_NAME="nixos-$LASTGEN-$COMMAND" + else TAG_NAME="nixos-$HOSTNAME-$LASTGEN-$COMMAND" fi +fi - __git "$WD" tag $TAG_FLAGS "$TAG_NAME" +__git "$WD" tag $TAG_FLAGS "$TAG_NAME" "$TAG_TARGET" - if [[ $TAG_NIXPKGS -eq 1 ]] +if [[ $TAG_NIXPKGS -eq 1 ]] +then + if [[ ! -z "$NIXPKGS" ]] then - if [[ ! -z "$NIXPKGS" ]] - then - stdout "Trying to generate tag in $NIXPKGS" - tag_nixpkgs "$NIXPKGS" - else - stderr "Do not generate a tag in the nixpkgs clon" - stderr "no NIXPKGS given." - usage - stderr "Continuing..." - fi + stdout "Trying to generate tag in $NIXPKGS" + [[ ! -d "$NIXPKGS" ]] && \ + stderr "'$NIXPKGS' is not a directory, so can't be a nixpkgs clone" && \ + exit 1 + + commit=$(nixos-version | cut -d . -f 3 | cut -d " " -f 1) + + continue_question "Trying to create tag '$TAG_NAME' at '$NIXPKGS' on commit '$commit'" && \ + (__git "$NIXPKGS" tag $TAG_FLAGS_NIXPKGS "$TAG_NAME" $commit || \ + stderr "Could not create tag in nixpkgs clone") else - stdout "nixpkgs tag generating disabled" + stderr "Do not generate a tag in the nixpkgs clone" + stderr "no NIXPKGS given." + usage + stderr "Continuing..." fi - else - stderr "Switching failed. Won't executing any further commands." - exit $REBUILD_EXIT + stdout "nixpkgs tag generating disabled" fi +stdout "Ready." |