home

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 304c19070321694c399eb12d4e3ffd60e5cd2378
parent e672e44c1354b58ee0336643ae788f00ad277e21
Author: Vincent Demeester <vincent@sbr.pm>
Date:   Sat, 23 May 2020 14:16:42 +0200

Add switch script :)

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Diffstat:
Aswitch | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+), 0 deletions(-)

diff --git a/switch b/switch @@ -0,0 +1,50 @@ +#! /usr/bin/env bash +set -o pipefail -o noclobber -o nounset + +function error() { + local red + local reset + red="$(tput setaf 1)" + reset="$(tput sgr0)" + + printf "%s%s%s\n" "$red" "$*" "$reset" + exit 1 +} + +function set_work_dir() { + if [[ ! -v WORK_DIR ]]; then + WORK_DIR="$(mktemp --tmpdir -u nix-config-sync.XXXXXXXXXX)" + # shellcheck disable=2064 + trap "rm -rf '$WORK_DIR'" EXIT + fi +} + +function build() { + [ "$#" -eq 0 ] || error "build" + set_work_dir + local machine + machine="$(hostname)" + unset NIX_PATH + nix-build --out-link "$WORK_DIR" -A "$machine" || + error "Failed to build system" +} + +function switch() { + [ "$#" -eq 0 ] || error "switch" + set_work_dir + local switch_bin="$WORK_DIR/bin/switch-to-configuration" + sudo nix-env --set \ + --profile "/nix/var/nix/profiles/system" \ + "$WORK_DIR" || + error "Failed to activate profile" + sudo "$switch_bin" "switch" || + error "Failed to activate system" +} + +function main() { + build + switch + exit 0 +} + +main "$@"+ \ No newline at end of file