#!/bin/sh -eux
if test "$(id -u)" != 0
then sudo "$0" "$@"
else
  cd "${0%/*}"
  chmod -R g-rwx,o-rwx "$PWD/private/root"
  ln -sfn "$PWD/private/root" /root/private
  trap 'git reset private/root' EXIT
  git rm -rf --cached --ignore-unmatch private/root # prevent copying to /nix/store
  nixos-rebuild switch --flake . "$@"
  nix-env --delete-generations +9 --profile /nix/var/nix/profiles/system
fi