Commit 304c19070321

Vincent Demeester <vincent@sbr.pm>
2020-05-23 14:16:42
Add switch script :)
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent e672e44
Changed files (1)
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