disko/disko

142 lines
3.4 KiB
Plaintext
Raw Normal View History

2022-10-27 01:02:49 +03:00
#!/usr/bin/env bash
set -euo pipefail
readonly libexec_dir="${0%/*}"
2022-11-05 22:25:08 +03:00
# a file with the disko config
declare disko_config
2022-10-27 01:02:49 +03:00
# 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
2023-09-14 18:20:03 +03:00
or $0 [options] --flake github:somebody/somewhere#disk-config
With flakes, disk-config is discovered first under the .diskoConfigurations top level attribute
or else from the disko module of a NixOS configuration of that name under .nixosConfigurations.
2022-10-27 01:02:49 +03:00
Options:
* -m, --mode mode
set the mode, either format, mount or disko
2023-07-17 19:56:49 +03:00
format: create partition tables, zpools, lvms, raids and filesystems
mount: mount the partition at the specified root-mountpoint
2023-07-17 19:56:49 +03:00
disko: first unmount and destroy all filesystems on the disks we want to format, then run the create and mount mode
* -f, --flake uri
fetch the disko config relative to this flake's root
2022-10-27 01:02:49 +03:00
* --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 /some/other/mnt
where to mount the device tree (default: /mnt)
* --dry-run
just show the path to the script instead of running it
* --no-deps
avoid adding another dependency closure to an in-memory installer
requires all necessary dependencies to be available in the environment
2022-11-10 15:04:50 +03:00
* --debug
run with set -x
2023-09-20 12:02:15 +03:00
* --help
show this help
2022-10-27 01:02:49 +03:00
USAGE
}
abort() {
echo "aborted: $*" >&2
exit 1
}
## Main ##
2022-11-05 22:25:08 +03:00
[[ $# -eq 0 ]] && {
showUsage
exit 1
}
2022-10-27 01:02:49 +03:00
while [[ $# -gt 0 ]]; do
case "$1" in
2022-11-10 15:04:50 +03:00
--debug)
set -x
;;
2022-10-27 01:02:49 +03:00
-m | --mode)
mode=$2
shift
;;
-f | --flake)
flake=$2
shift
;;
2022-10-27 01:02:49 +03:00
--argstr | --arg)
nix_args+=("$1" "$2" "$3")
shift
shift
;;
2023-09-20 12:02:15 +03:00
-h | --help)
2022-10-27 01:02:49 +03:00
showUsage
exit 0
;;
--dry-run)
dry_run=y
;;
--root-mountpoint)
nix_args+=(--argstr rootMountPoint "$2")
shift
;;
2022-12-01 22:33:03 +03:00
--no-deps)
nix_args+=(--arg noDeps true)
;;
--show-trace)
nix_args+=("$1")
;;
2022-10-27 01:02:49 +03:00
*)
if [ -z ${disko_config+x} ]; then
2022-10-27 01:02:49 +03:00
disko_config=$1
else
showUsage
2022-11-05 22:25:08 +03:00
exit 1
2022-10-27 01:02:49 +03:00
fi
;;
esac
shift
done
if ! { [[ $mode = "format" ]] || [[ $mode = "mount" ]] || [[ $mode = "disko" ]] || [[ $mode = "create" ]] || [[ $mode = "zap_create_mount" ]] ; }; then
abort "mode must be either format, mount or disko"
2022-10-27 01:02:49 +03:00
fi
if [[ -n "${flake+x}" ]]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
2023-09-14 18:16:34 +03:00
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
2023-09-22 05:50:14 +03:00
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
2022-11-21 14:00:45 +03:00
abort "disko config must be an existing file or flake must be set"
fi
2023-07-30 20:12:24 +03:00
# The "--impure" is still pure, as the path is within the nix store.
2022-10-27 01:02:49 +03:00
script=$(nix-build "${libexec_dir}"/cli.nix \
2022-11-10 01:44:10 +03:00
--no-out-link \
--impure \
2022-10-27 01:02:49 +03:00
--argstr mode "$mode" \
"${nix_args[@]}"
)
if [[ -n "${dry_run+x}" ]]; then
echo "$script"
else
exec "$script"
fi