add disko cli

This commit is contained in:
lassulus 2022-10-27 00:02:49 +02:00
parent 324a8d5347
commit f1531fb4f3
3 changed files with 85 additions and 2 deletions

15
cli.nix Normal file
View File

@ -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

View File

@ -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}
'';

68
disko Executable file
View File

@ -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"