#!/usr/bin/env nix-shell #! nix-shell -i bash -p jq set -euo pipefail ## Configuration readonly libexec_dir="${0%/*}" readonly format_dir=$libexec_dir/formats configuration=${NIXOS_CONFIG:-$libexec_dir/configuration.nix} format_path= target_system= cores= run= nix_args=( "$libexec_dir/nixos-generate.nix" ) has_outlink=false nix_build_args=() ## Functions showUsage() { cat <&2 exit 1 } ## Main ## while [[ $# -gt 0 ]]; do case "$1" in -c | --configuration) configuration=$2 shift ;; --cores) cores=$2 shift ;; -f | --format) format_path=$format_dir/$2.nix shift ;; --format-path) format_path=$2 shift ;; --help) showUsage exit ;; --list) listFormats exit ;; --run) run=1 # default to the VM format if [[ -z $format_path ]]; then format_path=$format_dir/vm.nix fi ;; --show-trace) nix_args+=(--show-trace) ;; --system) target_system=$2 shift ;; -I) nix_args+=(-I "$2") shift ;; -o | --out-link) nix_build_args+=(--out-link "$2") has_outlink=true shift ;; *) abort "unknown option $1" ;; esac shift done if ! $has_outlink; then nix_build_args+=(--no-out-link) fi if [[ -z $format_path ]]; then abort "missing format. use --help for more details" fi nix_args+=( -I "nixos-config=$configuration" -I "format-config=$format_path" ) if [[ -n $target_system ]]; then nix_args+=(--argstr system "$target_system") fi if [[ -n $cores ]]; then nix_args+=(--cores "$cores") fi formatAttr=$(nix-instantiate "${nix_args[@]}" --eval --json -A config.formatAttr | jq -r .) filename=$(nix-instantiate "${nix_args[@]}" --eval --json -A config.filename | jq -r .) out=$(nix-build "${nix_args[@]}" "${nix_build_args[@]}" -A "config.system.build.$formatAttr") if [[ -z $run ]]; then # show the first file, ignoring nix-support find "$out" -wholename "$filename" -type f -print -quit else runner=$(find "$out"/bin -type l -print -quit) exec "$runner" fi