mirror of
https://github.com/nix-community/nixos-generators.git
synced 2024-09-11 12:15:29 +03:00
122 lines
2.2 KiB
Plaintext
Executable File
122 lines
2.2 KiB
Plaintext
Executable File
#!/usr/bin/env nix-shell
|
|
#! nix-shell -i bash -p jq
|
|
set -euo pipefail
|
|
|
|
## Configuration
|
|
|
|
libexec_dir="${0%/*}"
|
|
configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix}
|
|
format=
|
|
format_dir=$libexec_dir/formats
|
|
target_system=
|
|
cores=
|
|
run=
|
|
|
|
## Functions
|
|
|
|
showUsage() {
|
|
cat <<USAGE
|
|
Usage: $0 [options]
|
|
|
|
Options:
|
|
|
|
* --help: shows this help
|
|
* -c, --configuration PATH:
|
|
select the nixos configuration to build. Default: $configuration
|
|
* -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
|
|
* --system: specify the target system (eg: x86_64-linux)
|
|
* --cores : to control the maximum amount of parallelism. (see nix-build documentation)
|
|
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)
|
|
configuration=$2
|
|
shift
|
|
;;
|
|
--cores)
|
|
cores=$2
|
|
shift
|
|
;;
|
|
-f | --format)
|
|
format=$format_dir/$2.nix
|
|
shift
|
|
;;
|
|
--format-path)
|
|
format=$2
|
|
shift
|
|
;;
|
|
--help)
|
|
showUsage
|
|
exit
|
|
;;
|
|
--list)
|
|
listFormats
|
|
exit
|
|
;;
|
|
--run)
|
|
run=1
|
|
# default to the VM format
|
|
if [[ -z $format ]]; then
|
|
format=$format_dir/vm.nix
|
|
fi
|
|
;;
|
|
--system)
|
|
target_system=$2
|
|
shift
|
|
;;
|
|
*)
|
|
abort "unknown option $1"
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [[ -z $format ]]; then
|
|
abort "missing format. use --help for more details"
|
|
fi
|
|
|
|
args=(
|
|
"$libexec_dir/nixos-generate.nix"
|
|
-I "nixos-config=$configuration"
|
|
-I "format-config=$format"
|
|
)
|
|
|
|
if [[ -n $target_system ]]; then
|
|
args+=(--argstr system "$target_system")
|
|
fi
|
|
|
|
if [[ -n $cores ]]; then
|
|
args+=(--cores "$cores")
|
|
fi
|
|
|
|
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
|
|
runner=$(find "$out"/bin -type l -print -quit)
|
|
exec "$runner"
|
|
fi
|