summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlassulus <lassulus@lassul.us>2022-10-27 00:02:49 +0200
committerlassulus <lassulus@lassul.us>2022-11-01 20:05:10 +0100
commitf1531fb4f3f8052181964d17891fbb3ce1f918f7 (patch)
tree87c69c9af1699ff2ebb9867fa9ba874576e6c1f9
parent324a8d5347a18e0c74625903c3bb13144370fbf2 (diff)
add disko cli
-rw-r--r--cli.nix15
-rw-r--r--default.nix4
-rwxr-xr-xdisko68
3 files changed, 85 insertions, 2 deletions
diff --git a/cli.nix b/cli.nix
new file mode 100644
index 0000000..76e0971
--- /dev/null
+++ b/cli.nix
@@ -0,0 +1,15 @@
+{ pkgs ? import <nixpkgs> {}
+, mode ? "mount"
+, diskoFile
+, ... }@args:
+let
+ disko = import ./. {};
+ diskFormat = import diskoFile args;
+ diskoEval = if (mode == "create") then
+ disko.createScript diskFormat pkgs
+ else if (mode == "mount") then
+ disko.mountScript diskFormat pkgs
+ else
+ builtins.abort "invalid mode"
+ ;
+in diskoEval
diff --git a/default.nix b/default.nix
index fdbe43d..8d91e7c 100644
--- a/default.nix
+++ b/default.nix
@@ -15,12 +15,12 @@ let
in {
types = types;
create = cfg: types.diskoLib.create (eval cfg).config.devices;
- createScript = pkgs: cfg: pkgs.writeScript "disko-create" ''
+ createScript = cfg: pkgs: pkgs.writeScript "disko-create" ''
export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.devices pkgs)}
${types.diskoLib.create (eval cfg).config.devices}
'';
mount = cfg: types.diskoLib.mount (eval cfg).config.devices;
- mountScript = pkgs: cfg: pkgs.writeScript "disko-mount" ''
+ mountScript = cfg: pkgs: pkgs.writeScript "disko-mount" ''
export PATH=${lib.makeBinPath (types.diskoLib.packages (eval cfg).config.devices pkgs)}
${types.diskoLib.mount (eval cfg).config.devices}
'';
diff --git a/disko b/disko
new file mode 100755
index 0000000..c5b9d49
--- /dev/null
+++ b/disko
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+set -euo pipefail
+set -x
+
+readonly libexec_dir="${0%/*}"
+
+# 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
+* --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 ##
+
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ -m | --mode)
+ mode=$2
+ shift
+ ;;
+ --argstr | --arg)
+ nix_args+=("$1" "$2" "$3")
+ shift
+ shift
+ ;;
+ --help)
+ showUsage
+ exit 0
+ ;;
+ *)
+ if [ -z ${disko_config+x} ] && [ -e $1 ]; then
+ disko_config=$1
+ else
+ showUsage
+ fi
+ ;;
+ esac
+ shift
+done
+
+if ! ([[ $mode = "create" ]] || [[ $mode = "mount" ]]); then
+ abort "mode must be either create or mount"
+fi
+
+script=$(nix-build "${libexec_dir}"/cli.nix \
+ --arg diskoFile "$disko_config" \
+ --argstr mode "$mode" \
+ "${nix_args[@]}"
+)
+exec "$script"