Added build script
parent
1b2733c02a
commit
1ad92b7d32
|
|
@ -1,2 +1,7 @@
|
||||||
hardware-configuration.nix
|
hardware-configuration.nix
|
||||||
|
|
||||||
|
# VM files
|
||||||
|
result
|
||||||
|
*.qcow2
|
||||||
|
|
||||||
*.swp
|
*.swp
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,114 @@
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
echo "Usage: $(basename "$0") [-n <flake name>] [-fqwx] <nixos-rebuild args...>
|
||||||
|
This script is a wrapper around nixos-rebuild, designed to simplify applying configurations and testing in vm. Requires super-user privileges.
|
||||||
|
|
||||||
|
examples:
|
||||||
|
$(basename "$0") -n nixos switch
|
||||||
|
- switch configuration to flake named nixos
|
||||||
|
|
||||||
|
$(basename "$0") -f switch
|
||||||
|
- switch configuration and don't ask confirmation
|
||||||
|
|
||||||
|
$(basename "$0") -fwx
|
||||||
|
- build a fresh vm image and run it
|
||||||
|
|
||||||
|
-f
|
||||||
|
Don't ask for confirmation about deleting the /etc/nixos directory.
|
||||||
|
-n name
|
||||||
|
Flake name. Defaults to your hostname.
|
||||||
|
-q
|
||||||
|
Don't output messages from this script.
|
||||||
|
-w
|
||||||
|
Wipes the vm disk file before running qemu, has no effect if not using the -x option.
|
||||||
|
Building the vm after making changes to configs lead to errors without this.
|
||||||
|
-x
|
||||||
|
Run the vm automatically after build. This implies 'build-vm' argument.
|
||||||
|
Also forwards host port 2222 to quest port 22." 1>&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
log() {
|
||||||
|
if [ "$quiet" != true ]; then
|
||||||
|
echo "[$(basename "$0")] $@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
die() {
|
||||||
|
echo "$@"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "$#" -eq 0 ] && usage
|
||||||
|
|
||||||
|
original_dir="$(realpath ./)"
|
||||||
|
source_dir="$(realpath "$(dirname "$0")")"
|
||||||
|
|
||||||
|
name="$(hostname)"
|
||||||
|
wipe_vm=""
|
||||||
|
execute_vm=""
|
||||||
|
force=""
|
||||||
|
quiet=""
|
||||||
|
|
||||||
|
while getopts ":n::fqwx" o; do
|
||||||
|
case "${o}" in
|
||||||
|
n)
|
||||||
|
name="$OPTARG"
|
||||||
|
;;
|
||||||
|
f)
|
||||||
|
force=true
|
||||||
|
;;
|
||||||
|
q)
|
||||||
|
quiet=true
|
||||||
|
;;
|
||||||
|
w)
|
||||||
|
wipe_vm=true
|
||||||
|
;;
|
||||||
|
x)
|
||||||
|
execute_vm=true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND-1))
|
||||||
|
|
||||||
|
if [ ! -d "$source_dir" ]; then
|
||||||
|
die "$source_dir is not a directory"
|
||||||
|
fi
|
||||||
|
|
||||||
|
dest_dir="/etc/nixos"
|
||||||
|
if [ "$(realpath "$dest_dir")" != "$(realpath "$source_dir")" ]; then
|
||||||
|
if [ "$force" != true ]; then
|
||||||
|
confirm="n"
|
||||||
|
read -p "This will remove your /etc/nixos directory! Do you want to proceed? (y/n) " confirm
|
||||||
|
case "$confirm" in
|
||||||
|
y) ;;
|
||||||
|
*) die "aborting...";;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
log "Clearing /etc/nixos"
|
||||||
|
rm -rf "$dest_dir"
|
||||||
|
log "Copying $source_dir to $dest_dir"
|
||||||
|
cp -r "$source_dir" "$dest_dir"
|
||||||
|
cd "$dest_dir" || exit 1
|
||||||
|
else
|
||||||
|
log "WARNING: It's recommended to clone this repo somewhere else than $dest_dir"
|
||||||
|
fi
|
||||||
|
|
||||||
|
sed -Ei 's;^(hardware-configuration.nix)$;#\1;' .gitignore
|
||||||
|
[ ! -f 'hardware-configuration.nix' ] && log "Adding hardware-configuration.nix" && nixos-generate-config --show-hardware-config > hardware-configuration.nix
|
||||||
|
git add hardware-configuration.nix
|
||||||
|
|
||||||
|
cd "$original_dir" || exit 1
|
||||||
|
if [ "$execute_vm" = true ]; then
|
||||||
|
# Run the vm and forward ports
|
||||||
|
[ "$wipe_vm" = true ] && rm -f *.qcow2
|
||||||
|
log "Running: nixos-rebuild --flake \"$dest_dir/#${name}\" $@ build-vm && QEMU_NET_OPTS=\"hostfwd=tcp::2222-:22\" ./result/bin/run-${name}-vm"
|
||||||
|
nixos-rebuild --flake "$dest_dir/#${name}" "$@" build-vm && QEMU_NET_OPTS="hostfwd=tcp::2222-:22" ./result/bin/run-${name}-vm
|
||||||
|
else
|
||||||
|
log "Running: nixos-rebuild --flake \"$dest_dir/#${name}\" $@"
|
||||||
|
nixos-rebuild --flake "$dest_dir/#${name}" "$@"
|
||||||
|
fi
|
||||||
Loading…
Reference in New Issue