Litenet-Nix-Infra/build.sh

34 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p git nixos-install-tools nix-output-monitor
if [ $# -ne 2 ]; then
echo "Usage: $0 <root> <config>"
echo "NOTE: hardware config will be generated from root!"
echo "Defined configs:"
cat flake.nix | grep '.lib.nixosSystem' | sed 's/ =.*//' | sed 's/^[ \t]*//;s/[ \t]*$//' | while read cfg; do echo " - $cfg"; done
exit 1
fi
ROOT=$1
CONFIG=$2
DERIVATION=".#nixosConfigurations.${CONFIG}.config.system.build.toplevel"
EXTRA_NIX_FLAGS="-L --accept-flake-config"
if [ "${ROOT}" = "/" ]; then
[ -f "host/${CONFIG}/hooks/pre-rebuild.sh" ] && host/${CONFIG}/hooks/pre-rebuild.sh
[ ! -f "hardware-configuration.nix" ] && nixos-generate-config --show-hardware-config > hardware-configuration.nix
git add -f hardware-configuration.nix
nom build $DERIVATION $EXTRA_NIX_FLAGS && nixos-rebuild switch --flake .#${CONFIG} --upgrade-all $EXTRA_NIX_FLAGS --use-remote-sudo || exit 1
#nixos-rebuild switch --flake ".#${CONFIG}" -j`nproc` --upgrade-all -L || exit 1
[ -f "host/${CONFIG}/hooks/post-rebuild.sh" ] && host/${CONFIG}/hooks/post-rebuild.sh
git rm --cached hardware-configuration.nix
exit
else
nixos-generate-config --show-hardware-config --root "${ROOT}" > hardware-configuration.nix
git add -f hardware-configuration.nix
nom build $DERIVATION $EXTRA_NIX_FLAGS || exit 1
nixos-install --root "${ROOT}" --flake ".#${CONFIG}" --no-channel-copy
git rm --cached hardware-configuration.nix
cp . "${ROOT}/Litenet-Nix-Infra" -r
exit
fi