home

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

system (1705B)


      1 #! /usr/bin/env bash
      2 set -o pipefail -o noclobber -o nounset
      3 
      4 WORK_DIR=${WORK_DIR-}
      5 if [[ -z "${WORK_DIR}" ]]; then
      6     WORK_DIR="$(mktemp --tmpdir -u nix-config-sync.XXXXXXXXXX)"
      7     # shellcheck disable=2064
      8     trap "rm -rf '$WORK_DIR'" EXIT
      9 fi
     10 function error() {
     11     local red
     12     local reset
     13     red="$(tput -Txterm setaf 1)"
     14     reset="$(tput -Txterm sgr0)"
     15 
     16     printf "%s%s%s\n" "$red" "$*" "$reset"
     17     exit 1
     18 }
     19 
     20 function dry-build() {
     21     [ "$#" -eq 0 ] || error "build"
     22     local machine
     23     machine="$(hostname)"
     24     unset NIX_PATH
     25     nix build -L systems.nix --show-trace --dry-run --out-link "$WORK_DIR" "$machine" ||
     26         error "Failed to build system"
     27 }
     28 
     29 function build() {
     30     [ "$#" -eq 0 ] || error "build"
     31     local machine
     32     machine="$(hostname)"
     33     unset NIX_PATH
     34     nix build -L -f systems.nix --show-trace --out-link "$WORK_DIR" "$machine" ||
     35         error "Failed to build system"
     36 }
     37 
     38 function switch() {
     39     [ "$#" -eq 0 ] || error "switch"
     40     build
     41     local switch_bin="$WORK_DIR/bin/switch-to-configuration"
     42     sudo nix-env --set \
     43         --profile "/nix/var/nix/profiles/system" \
     44         "$WORK_DIR" ||
     45         error "Failed to activate profile"
     46     sudo "$switch_bin" "switch" ||
     47         error "Failed to activate system"
     48 }
     49 
     50 function boot() {
     51     [ "$#" -eq 0 ] || error "switch"
     52     build
     53     local switch_bin="$WORK_DIR/bin/switch-to-configuration"
     54     sudo nix-env --set \
     55         --profile "/nix/var/nix/profiles/system" \
     56         "$WORK_DIR" ||
     57         error "Failed to activate profile"
     58     sudo "$switch_bin" "boot" ||
     59         error "Failed to activate system"
     60 }
     61 
     62 function main() {
     63     for target in $@; do
     64         $target
     65     done
     66     exit 0
     67 }
     68 
     69 main "$@"