2019-01-20 22:57:14 +03:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#! nix-shell -i bash -p jq
|
2019-01-17 17:18:00 +03:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
libexec_dir="${0%/*}"
|
2019-02-01 01:06:29 +03:00
|
|
|
configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix}
|
|
|
|
format=
|
2019-01-17 17:18:00 +03:00
|
|
|
format_dir=$libexec_dir/formats
|
2019-01-17 21:41:50 +03:00
|
|
|
target_system=
|
2019-01-17 17:18:00 +03:00
|
|
|
run=
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
showUsage() {
|
|
|
|
cat <<USAGE
|
|
|
|
Usage: $0 [options]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
* --help: shows this help
|
|
|
|
* -c, --configuration PATH:
|
2019-02-01 01:06:29 +03:00
|
|
|
select the nixos configuration to build. Default: $configuration
|
2019-01-17 17:18:00 +03:00
|
|
|
* -f, --format NAME: select one of the pre-determined formats
|
|
|
|
* --format-path PATH: pass a custom format
|
|
|
|
* --list: list the available built-in formats
|
|
|
|
* --run: runs the configuration in a VM
|
|
|
|
only works for the "vm" and "vm-no-gui" formats
|
2019-01-17 21:41:50 +03:00
|
|
|
* --system: specify the target system (eg: x86_64-linux)
|
2019-01-17 17:18:00 +03:00
|
|
|
USAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
listFormats() {
|
|
|
|
for format in "$format_dir"/*.nix; do
|
|
|
|
basename "$format" ".nix"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
echo "aborted: $*" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
## Main ##
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-c | --configuration)
|
2019-02-01 01:06:29 +03:00
|
|
|
configuration=$2
|
2019-01-17 17:18:00 +03:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-f | --format)
|
2019-02-01 01:06:29 +03:00
|
|
|
format=$format_dir/$2.nix
|
2019-01-17 17:18:00 +03:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--format-path)
|
2019-02-01 01:06:29 +03:00
|
|
|
format=$2
|
2019-01-17 17:18:00 +03:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--help)
|
|
|
|
showUsage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--list)
|
|
|
|
listFormats
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--run)
|
|
|
|
run=1
|
|
|
|
# default to the VM format
|
2019-02-01 01:06:29 +03:00
|
|
|
if [[ -z $format ]]; then
|
|
|
|
format=$format_dir/vm.nix
|
2019-01-17 17:18:00 +03:00
|
|
|
fi
|
|
|
|
;;
|
2019-01-17 21:41:50 +03:00
|
|
|
--system)
|
|
|
|
target_system=$2
|
|
|
|
shift
|
|
|
|
;;
|
2019-01-17 17:18:00 +03:00
|
|
|
*)
|
|
|
|
abort "unknown option $1"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2019-02-01 01:06:29 +03:00
|
|
|
if [[ -z $format ]]; then
|
2019-01-17 17:18:00 +03:00
|
|
|
abort "missing format. use --help for more details"
|
|
|
|
fi
|
|
|
|
|
|
|
|
args=(
|
2019-02-01 01:06:29 +03:00
|
|
|
"$libexec_dir/nixos-generate.nix"
|
|
|
|
-I "nixos-config=$configuration"
|
|
|
|
-I "format-config=$format"
|
2019-01-17 17:18:00 +03:00
|
|
|
)
|
|
|
|
|
2019-01-17 21:41:50 +03:00
|
|
|
if [[ -n $target_system ]]; then
|
|
|
|
args+=(--argstr system "$target_system")
|
|
|
|
fi
|
|
|
|
|
2019-01-17 17:18:00 +03:00
|
|
|
formatAttr=$(nix-instantiate "${args[@]}" --eval --json -A config.formatAttr | jq -r .)
|
|
|
|
|
|
|
|
out=$(nix-build "${args[@]}" --no-out-link -A "config.system.build.$formatAttr")
|
|
|
|
|
|
|
|
if [[ -z $run ]]; then
|
|
|
|
# show the first file
|
|
|
|
find "$out" -type f -print -quit
|
|
|
|
else
|
2019-01-22 09:38:54 +03:00
|
|
|
runner=$(find "$out"/bin -type l -print -quit)
|
|
|
|
exec "$runner"
|
2019-01-17 17:18:00 +03:00
|
|
|
fi
|