#!/usr/bin/env bash
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
flake_root=/etc/nixos
echo "flake_root=${flake_root}"
echo "backing up ${flake_root} to .old_system"
cp -r ${flake_root} ${flake_root}/.old_system/
echo "Downloading flake.nix"
wget https://fs.louiscloud.duckdns.org/juni-flake.nix -O ${flake_root}/flake.nix
hostdir=${flake_root}/hosts/$(hostname)/
echo "making ${hostdir}"
mkdir -p ${hostdir}
echo "moving configuration.nix to ${hostdir}"
mv ${flake_root}/configuration.nix ${hostdir}
echo "moving hardware-configuration.nix to ${hostdir}"
mv ${flake_root}/hardware-configuration.nix ${hostdir}
nixos-rebuild switch --flake $flake_root
