2020-10-23 17:48:17 +03:00
|
|
|
#!/usr/bin/env bash
|
2019-01-17 17:18:00 +03:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
2019-08-19 17:06:29 +03:00
|
|
|
readonly libexec_dir="${0%/*}"
|
|
|
|
readonly format_dir=$libexec_dir/formats
|
|
|
|
|
2019-02-01 01:06:29 +03:00
|
|
|
configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix}
|
2020-10-23 18:54:01 +03:00
|
|
|
flake_uri=
|
|
|
|
flake_attr=
|
2019-08-19 16:58:28 +03:00
|
|
|
format_path=
|
2019-01-17 21:41:50 +03:00
|
|
|
target_system=
|
2019-03-23 00:57:14 +03:00
|
|
|
cores=
|
2019-01-17 17:18:00 +03:00
|
|
|
run=
|
2019-08-19 17:06:29 +03:00
|
|
|
nix_args=(
|
|
|
|
"$libexec_dir/nixos-generate.nix"
|
|
|
|
)
|
2020-07-29 08:46:10 +03:00
|
|
|
has_outlink=false
|
|
|
|
nix_build_args=()
|
2019-01-17 17:18:00 +03:00
|
|
|
|
|
|
|
## 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
|
2020-10-23 18:54:01 +03:00
|
|
|
* --flake URI:
|
|
|
|
selects the nixos configuration to build, using flake uri like "~/dotfiles#my-config"
|
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-12-20 01:38:24 +03:00
|
|
|
* --show-trace: show more detailed nix evaluation location information
|
2019-01-17 21:41:50 +03:00
|
|
|
* --system: specify the target system (eg: x86_64-linux)
|
2020-07-29 08:46:10 +03:00
|
|
|
* -o, --out-link: specify the outlink location for nix-build
|
2019-03-23 00:57:14 +03:00
|
|
|
* --cores : to control the maximum amount of parallelism. (see nix-build documentation)
|
2019-08-19 17:06:29 +03:00
|
|
|
* -I KEY=VALUE: Add a key to the Nix expression search path.
|
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
|
|
|
|
;;
|
2020-10-23 18:54:01 +03:00
|
|
|
--flake)
|
|
|
|
# Note: The reason I'm using awk over cut is because cutting with an
|
|
|
|
# out-of-bounds field will return the last in-bound field instead of empty
|
|
|
|
# string.
|
|
|
|
flake="$(echo "$2" | awk -F'#' '{ print $1; }')"
|
|
|
|
|
|
|
|
flake_uri="$(nix flake info --json -- "$flake" | jq -r .url)"
|
|
|
|
flake_attr="$(echo "$2" | awk -F'#' '{ print $2; }')"
|
|
|
|
shift
|
|
|
|
;;
|
2019-03-23 00:57:14 +03:00
|
|
|
--cores)
|
|
|
|
cores=$2
|
|
|
|
shift
|
|
|
|
;;
|
2019-01-17 17:18:00 +03:00
|
|
|
-f | --format)
|
2019-08-19 16:58:28 +03:00
|
|
|
format_path=$format_dir/$2.nix
|
2019-01-17 17:18:00 +03:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--format-path)
|
2019-08-19 16:58:28 +03:00
|
|
|
format_path=$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-08-19 16:58:28 +03:00
|
|
|
if [[ -z $format_path ]]; then
|
|
|
|
format_path=$format_dir/vm.nix
|
2019-01-17 17:18:00 +03:00
|
|
|
fi
|
|
|
|
;;
|
2019-12-20 01:38:24 +03:00
|
|
|
--show-trace)
|
|
|
|
nix_args+=(--show-trace)
|
|
|
|
;;
|
2019-01-17 21:41:50 +03:00
|
|
|
--system)
|
|
|
|
target_system=$2
|
|
|
|
shift
|
|
|
|
;;
|
2019-08-19 17:06:29 +03:00
|
|
|
-I)
|
|
|
|
nix_args+=(-I "$2")
|
|
|
|
shift
|
|
|
|
;;
|
2020-07-29 08:46:10 +03:00
|
|
|
-o | --out-link)
|
|
|
|
nix_build_args+=(--out-link "$2")
|
|
|
|
has_outlink=true
|
|
|
|
shift
|
|
|
|
;;
|
2019-01-17 17:18:00 +03:00
|
|
|
*)
|
|
|
|
abort "unknown option $1"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2020-07-29 08:46:10 +03:00
|
|
|
if ! $has_outlink; then
|
|
|
|
nix_build_args+=(--no-out-link)
|
|
|
|
fi
|
|
|
|
|
2019-08-19 16:58:28 +03:00
|
|
|
if [[ -z $format_path ]]; then
|
2019-01-17 17:18:00 +03:00
|
|
|
abort "missing format. use --help for more details"
|
|
|
|
fi
|
|
|
|
|
2020-09-12 22:01:27 +03:00
|
|
|
if [[ ! -f $format_path ]]; then
|
|
|
|
abort "format file not found: $format_path"
|
|
|
|
fi
|
|
|
|
|
2020-10-23 18:54:01 +03:00
|
|
|
nix_args+=(--argstr formatConfig "$(realpath "$format_path")")
|
|
|
|
|
|
|
|
if [[ -z "$flake_uri" ]]; then
|
|
|
|
nix_args+=(
|
|
|
|
-I "nixos-config=$configuration"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
nix_args+=(
|
|
|
|
--argstr flakeUri "$flake_uri"
|
|
|
|
--argstr flakeAttr "${flake_attr:-"$(hostname)"}"
|
|
|
|
)
|
|
|
|
fi
|
2019-01-17 17:18:00 +03:00
|
|
|
|
2019-01-17 21:41:50 +03:00
|
|
|
if [[ -n $target_system ]]; then
|
2019-08-19 17:06:29 +03:00
|
|
|
nix_args+=(--argstr system "$target_system")
|
2019-01-17 21:41:50 +03:00
|
|
|
fi
|
|
|
|
|
2019-03-23 00:57:14 +03:00
|
|
|
if [[ -n $cores ]]; then
|
2019-08-19 17:06:29 +03:00
|
|
|
nix_args+=(--cores "$cores")
|
2019-03-23 00:57:14 +03:00
|
|
|
fi
|
|
|
|
|
2019-08-19 17:06:29 +03:00
|
|
|
formatAttr=$(nix-instantiate "${nix_args[@]}" --eval --json -A config.formatAttr | jq -r .)
|
2020-05-22 11:26:49 +03:00
|
|
|
filename=$(nix-instantiate "${nix_args[@]}" --eval --json -A config.filename | jq -r .)
|
2019-01-17 17:18:00 +03:00
|
|
|
|
2020-07-29 08:46:10 +03:00
|
|
|
out=$(nix-build "${nix_args[@]}" "${nix_build_args[@]}" -A "config.system.build.$formatAttr")
|
2019-01-17 17:18:00 +03:00
|
|
|
|
|
|
|
if [[ -z $run ]]; then
|
2019-10-09 02:06:45 +03:00
|
|
|
# show the first file, ignoring nix-support
|
2020-05-22 11:26:49 +03:00
|
|
|
find "$out" -wholename "$filename" -type f -print -quit
|
2019-01-17 17:18:00 +03:00
|
|
|
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
|