disko/disko
Soispha 90f24aef89
disko cli: fix error about impurity with nix
The script is generated by calling `nix-build` on the `cli.nix` file.
This fails on systems, which have the nix option pure_eval set, as nix
refuses to evaluate an untracked file. This would normally be impure,
but in this case the file is supplied in the same nix store path, i.e.
it's not referencing foreign files. Thus adding the `--impure` argument
should leave the nix call in fact still pure.
2023-06-28 17:35:27 +02:00

130 lines
2.8 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
readonly libexec_dir="${0%/*}"
# a file with the disko config
declare disko_config
# mount was chosen as the default mode because it's less destructive
mode=mount
nix_args=()
showUsage() {
cat <<USAGE
Usage: $0 [options] disk-config.nix
or $0 [options] --flake github:somebody/somewhere
Options:
* -m, --mode mode
set the mode, either create, mount, zap_create_mount or disko
* -f, --flake uri
fetch the disko config relative to this flake's root
* --arg name value
pass value to nix-build. can be used to set disk-names for example
* --argstr name value
pass value to nix-build as string
* --root-mountpoint /mnt
where to mount the device tree
* --dry-run
just show the path to the script instead of running it
* --debug
run with set -x
USAGE
}
abort() {
echo "aborted: $*" >&2
exit 1
}
## Main ##
[[ $# -eq 0 ]] && {
showUsage
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
--debug)
set -x
;;
-m | --mode)
mode=$2
shift
;;
-f | --flake)
flake=$2
shift
;;
--argstr | --arg)
nix_args+=("$1" "$2" "$3")
shift
shift
;;
--help)
showUsage
exit 0
;;
--dry-run)
dry_run=y
;;
--root-mountpoint)
nix_args+=(--argstr rootMountPoint "$2")
shift
;;
--no-deps)
nix_args+=(--arg noDeps true)
;;
--show-trace)
nix_args+=("$1")
;;
*)
if [ -z ${disko_config+x} ]; then
disko_config=$1
else
showUsage
exit 1
fi
;;
esac
shift
done
if ! { [[ $mode = "create" ]] || [[ $mode = "mount" ]] || [[ $mode = "zap_create_mount" ]] || [[ $mode = "disko" ]]; }; then
abort "mode must be either create, mount, zap_create_mount or disko"
fi
if [[ -n "${flake+x}" ]]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
if [[ -z "$flakeAttr" ]]; then
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri."
echo "For example, to use the output diskoConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri."
exit 1
fi
nix_args+=("--arg" "flake" "$flake")
nix_args+=("--argstr" "flakeAttr" "$flakeAttr")
nix_args+=(--extra-experimental-features flakes)
elif [[ -n "${disko_config+x}" ]] && [[ -e "$disko_config" ]]; then
nix_args+=("--arg" "diskoFile" "$disko_config")
else
abort "disko config must be an existing file or flake must be set"
fi
# The "--impure" is still pure, as the path is withing the nix store.
script=$(nix-build "${libexec_dir}"/cli.nix \
--no-out-link \
--impure \
--argstr mode "$mode" \
"${nix_args[@]}"
)
if [[ -n "${dry_run+x}" ]]; then
echo "$script"
else
exec "$script"
fi