# deploy target: # nh os switch --file ./default.nix hosts.{{ target }} --target-host {{ target }} --build-host {{ target }} set shell := ["bash", "-c"] deploy_old target build_target=target: # nixos-rebuild switch --file ./default.nix -A hosts.{{ target }} --target-host {{ target }} --build-host {{ target }} --sudo |& nom nixos-rebuild switch --file ./default.nix -A hosts.{{ target }} --target-host {{ target }} --build-host {{ build_target }} --sudo |& nom deploy_local target: nixos-rebuild switch --file ./default.nix -A hosts.{{ target }} --target-host {{ target }} --sudo |& nom deploy target ssh_target=target: #!/usr/bin/env bash set -euo pipefail printf "\e[34m%s \e[1m%s\e[0m \e[34m%s \e[1m%s\e[0m\n\n" "Remote building and deploying system" "hosts.{{ target }}" "onto" "ssh://{{ ssh_target }}" # 1. printf "\e[1;34m%s\e[0m \e[34m%s\e[0m\n" "1." "Building derivation ..." derivation=$(nix-instantiate ./default.nix -A hosts.{{ target }}.config.system.build.toplevel) printf "\e[34m%s\e[0m \e[34m%s\e[0m \e[4;32m%s\e[0m\n\n" "Derivation build!" "Path:" "${derivation}" # 2. printf "\e[1;34m%s\e[0m \e[34m%s\e[0m \e[1;34m%s\e[0m\n" "2." "Copying derivation to remote" "\"{{ ssh_target }}\"" # nix-copy-closure --to {{ ssh_target }} "$derivation" nix copy --derivation --to ssh://{{ ssh_target }} "${derivation}" # 3. printf "\n\e[1;34m%s\e[0m \e[34m%s \e[1m%s\e[0m\n" "3." "Building derivation on" "\"{{ ssh_target }}\"" BUILD=$(ssh {{ ssh_target }} bash -s "${derivation}" <<-'REMOTE' set -euo pipefail DERIVATION=$1 BUILD=$(nix run nixpkgs#nix-output-monitor -- build "${DERIVATION}^out" --print-out-paths --no-link) # 4. printf "\n\e[1;34m%s\e[0m \e[34m%s\e[0m\n" "4." "Showing diff" >&2 nix run nixpkgs#nvd -- --color always diff /run/current-system "$BUILD" >&2 echo "$BUILD" REMOTE ) # 5. printf "\n\e[33m%s\e[0m\n" "Continue deploying? (Y/n)" read ans if [[ "$ans" = "y" || "$ans" = "" ]]; then UNIT=nix-magic-rollback # 5. 1/2 printf "\n\e[1;34m%s\e[0m \e[34m%s\e[0m\n" "5." "Deploying with magic rollback" ssh {{ ssh_target }} bash -s "${BUILD}" "${UNIT}" <<-'REMOTE' set -euo pipefail BUILD=$1; UNIT=$2; sudo systemd-run \ --unit="$UNIT" \ --description="Magic rollback" \ --on-active=40 \ -- bash -c "echo 'Rewinding back to previous generation' && /nix/var/nix/profiles/system/bin/switch-to-configuration test" sudo "$BUILD/bin/switch-to-configuration" test REMOTE # 5. 2/2 printf "\n\e[1;34m%s\e[0m \e[34m%s\e[0m\n" "6." "Finalizing deployment" ssh {{ ssh_target }} bash -s "${BUILD}" "${UNIT}" <<-'REMOTE' set -euo pipefail BUILD=$1; UNIT=$2; sudo systemctl stop ${UNIT}.timer ${UNIT}.service || true sudo nix-env -p /nix/var/nix/profiles/system --set "$BUILD" sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch REMOTE printf "\n\e[1;32m%s\e[0m\n" "Deployment successful!" else printf "\n\e[31m%s\e[0m\n" "Deployment cancelled" fi deploy-homelab: nixos-rebuild switch --file ./default.nix -A hosts.home-server --target-host root@snowlab.local --build-host root@snowlab.local |& nom deploy-server: nh os switch --file ./default.nix hosts.server --target-host server --build-host server wireguard_gen: #!/usr/bin/env nix-shell #!nix-shell -i bash -p wireguard-tools key=$(wg genkey) pub=$(echo $key | wg pubkey) psk=$(wg genpsk) echo -e "key: $key\npub: $pub\npsk: $psk" deploy_dots: nix run nixpkgs#stow -- --verbose --dir configs/ snowflake --target="$HOME"