diff options
-rw-r--r-- | cli.nix | 15 | ||||
-rw-r--r-- | default.nix | 4 | ||||
-rwxr-xr-x | disko | 68 |
3 files changed, 85 insertions, 2 deletions
@@ -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} ''; @@ -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" |