From 923f58794aac6c54559b4415d81cb8a06dbd1170 Mon Sep 17 00:00:00 2001 From: Nicolas Pierron Date: Wed, 19 Aug 2009 15:04:19 +0000 Subject: [PATCH] Add --rollback option to nixos-rebuild. svn path=/nixos/trunk/; revision=16771 --- modules/installer/tools/nixos-rebuild.sh | 48 ++++++++++++++++++------ 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/modules/installer/tools/nixos-rebuild.sh b/modules/installer/tools/nixos-rebuild.sh index 86b7337beac..d3b86fa6ab4 100644 --- a/modules/installer/tools/nixos-rebuild.sh +++ b/modules/installer/tools/nixos-rebuild.sh @@ -31,6 +31,8 @@ Options: channel manifest --no-build-nix don't build the latest Nix from Nixpkgs before building NixOS + --rollback restore the previous NixOS configuration (only + with switch, boot, test, build) Various nix-build options are also accepted, in particular: @@ -51,6 +53,7 @@ extraBuildFlags= action= pullManifest=1 buildNix=1 +rollback= while test "$#" -gt 0; do i="$1"; shift 1 @@ -65,6 +68,8 @@ while test "$#" -gt 0; do pullManifest= elif test "$i" = --no-build-nix; then buildNix= + elif test "$i" = --rollback; then + rollback=1 elif test "$i" = --show-trace -o "$i" = --no-build-hook -o "$i" = --keep-failed -o "$i" = -K \ -o "$i" = --keep-going -o "$i" = -k -o "$i" = --verbose -o "$i" = -v; then extraBuildFlags="$extraBuildFlags $i" @@ -83,6 +88,11 @@ if test "$action" = dry-run; then extraBuildFlags="$extraBuildFlags --dry-run" fi +if test -n "$rollback"; then + pullManifest= + buildNix= +fi + tmpDir=$(mktemp -t -d nixos-rebuild.XXXXXX) trap 'rm -rf "$tmpDir"' EXIT @@ -126,17 +136,33 @@ fi # Either upgrade the configuration in the system profile (for "switch" # or "boot"), or just build it and create a symlink "result" in the # current directory (for "build" and "test"). -if test "$action" = switch -o "$action" = boot; then - nix-env -p /nix/var/nix/profiles/system -f $NIXOS --set -A system $extraBuildFlags - pathToConfig=/nix/var/nix/profiles/system -elif test "$action" = test -o "$action" = build -o "$action" = dry-run; then - nix-build $NIXOS -A system -K -k $extraBuildFlags - pathToConfig=./result -elif test "$action" = build-vm; then - nix-build $NIXOS -A vm -K -k $extraBuildFlags - pathToConfig=./result -else - showSyntax +if test -z "$rollback"; then + if test "$action" = switch -o "$action" = boot; then + nix-env -p /nix/var/nix/profiles/system -f $NIXOS --set -A system $extraBuildFlags + pathToConfig=/nix/var/nix/profiles/system + elif test "$action" = test -o "$action" = build -o "$action" = dry-run; then + nix-build $NIXOS -A system -K -k $extraBuildFlags + pathToConfig=./result + elif test "$action" = build-vm; then + nix-build $NIXOS -A vm -K -k $extraBuildFlags + pathToConfig=./result + else + showSyntax + fi +else # test -n "$rollback" + if test "$action" = switch -o "$action" = boot; then + nix-env --rollback -p /nix/var/nix/profiles/system + pathToConfig=/nix/var/nix/profiles/system + elif test "$action" = test -o "$action" = build; then + systemNumber=$( + nix-env -p /nix/var/nix/profiles/system --list-generations | + sed -n '/current/ {g; p;}; s/ *\([0-9]*\).*/\1/; h' + ) + ln -sT /nix/var/nix/profiles/system-${systemNumber}-link ./result + pathToConfig=./result + else + showSyntax + fi fi