summaryrefslogtreecommitdiffstats
path: root/disko
blob: deb3cbdbd23a0eb72cf00f74fa739f0353113805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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"