diff options
author | Lassulus <github@lassul.us> | 2022-11-09 13:26:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-09 13:26:43 +0100 |
commit | 45ef21831ee493de5efa97f48f1c31ca9dd54764 (patch) | |
tree | d4efbdf4713aa4428a4922c0389ca4c61c808b9e /disko | |
parent | c96ccd7d9fb48b8283e84811c2355a3c39bb2a52 (diff) | |
parent | 0af2a7c206bd69ecdc01361e12c7cb0ec9820911 (diff) |
Merge pull request #48 from nix-community/module
Diffstat (limited to 'disko')
-rwxr-xr-x | disko | 94 |
1 files changed, 94 insertions, 0 deletions
@@ -0,0 +1,94 @@ +#!/usr/bin/env bash +set -euo pipefail + +readonly libexec_dir="${0%/*}" + +# a file with the disko config +declare disko_config + +# a flake uri, if present disko config is relative to the flake root +declare from_flake + +# 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 + +Options: + +* -m, --mode mode + set the mode, either create or mount +* -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 +USAGE +} + +abort() { + echo "aborted: $*" >&2 + exit 1 +} + +## Main ## + +[[ $# -eq 0 ]] && { + showUsage + exit 1 +} + +while [[ $# -gt 0 ]]; do + case "$1" in + -m | --mode) + mode=$2 + shift + ;; + -f | --flake) + from_flake="$2" + nix_args+=("--argstr" "fromFlake" "$2") + shift + ;; + --argstr | --arg) + nix_args+=("$1" "$2" "$3") + shift + shift + ;; + --help) + showUsage + exit 0 + ;; + *) + if [ -z ${disko_config+x} ]; then + disko_config=$1 + else + showUsage + exit 1 + fi + ;; + esac + shift +done + +if ! ([[ $mode = "create" ]] || [[ $mode = "mount" ]]); then + abort "mode must be either create or mount" +fi + +if [[ -e "${disko_config}" ]]; then + nix_args+=("--arg" "diskoFile" "$disko_config") +elif [[ -n "${from_flake+x}" ]]; then + nix_args+=("--argstr" "diskoFile" "$disko_config") +else + abort "disko config must be an exising file of flake must be set" +fi + +script=$(nix-build "${libexec_dir}"/cli.nix \ + --argstr diskoFile "$disko_config" \ + --argstr mode "$mode" \ + "${nix_args[@]}" +) +exec "$script" |