disko: support nixos-install style flake syntax

This commit is contained in:
lassulus 2022-11-09 23:36:40 +01:00
parent 7e52500078
commit 5fd29dfeb3
2 changed files with 37 additions and 14 deletions

15
cli.nix
View file

@ -1,14 +1,17 @@
{ pkgs ? import <nixpkgs> {} { pkgs ? import <nixpkgs> {}
, mode ? "mount" , mode ? "mount"
, fromFlake ? null , flake ? null
, diskoFile , flakeAttr ? null
, diskoFile ? null
, ... }@args: , ... }@args:
let let
disko = import ./. { }; disko = import ./. { };
diskFormat =
if fromFlake != null diskFormat = if flake != null then
then (builtins.getFlake fromFlake) + "/${diskoFile}" (pkgs.lib.attrByPath [ "diskoConfigurations" flakeAttr ] (builtins.abort "${flakeAttr} does not exist") (builtins.getFlake flake)) args
else import diskoFile; else
import diskoFile args;
diskoEval = if (mode == "create") then diskoEval = if (mode == "create") then
disko.createScript diskFormat pkgs disko.createScript diskFormat pkgs
else if (mode == "mount") then else if (mode == "mount") then

34
disko
View file

@ -27,6 +27,8 @@ Options:
pass value to nix-build. can be used to set disk-names for example pass value to nix-build. can be used to set disk-names for example
* --argstr name value * --argstr name value
pass value to nix-build as string pass value to nix-build as string
* --dry-run
just show the path to the script instead of running it
USAGE USAGE
} }
@ -49,8 +51,7 @@ while [[ $# -gt 0 ]]; do
shift shift
;; ;;
-f | --flake) -f | --flake)
from_flake="$2" flake="$2"
nix_args+=("--argstr" "fromFlake" "$2")
shift shift
;; ;;
--argstr | --arg) --argstr | --arg)
@ -62,6 +63,12 @@ while [[ $# -gt 0 ]]; do
showUsage showUsage
exit 0 exit 0
;; ;;
--dry-run)
dry_run=y
;;
--show-trace)
nix_args+=("$1")
;;
*) *)
if [ -z ${disko_config+x} ]; then if [ -z ${disko_config+x} ]; then
disko_config=$1 disko_config=$1
@ -78,17 +85,30 @@ if ! ([[ $mode = "create" ]] || [[ $mode = "mount" ]]); then
abort "mode must be either create or mount" abort "mode must be either create or mount"
fi fi
if [[ -e "${disko_config+x}" ]]; then if [[ ! -z "${flake+x}" ]]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
if [[ -z "$flakeAttr" ]]; then
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri."
echo "For example, to use the output diskoConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri."
exit 1
fi
nix_args+=("--arg" "flake" "$flake")
nix_args+=("--argstr" "flakeAttr" "$flakeAttr")
elif [[ ! -z "${disko_config+x}" ]] && [[ -e "$disko_config" ]]; then
nix_args+=("--arg" "diskoFile" "$disko_config") nix_args+=("--arg" "diskoFile" "$disko_config")
elif [[ -n "${from_flake+x}" ]]; then
nix_args+=("--argstr" "diskoFile" "$disko_config")
else else
abort "disko config must be an exising file of flake must be set" abort "disko config must be an exising file or flake must be set"
fi fi
script=$(nix-build "${libexec_dir}"/cli.nix \ script=$(nix-build "${libexec_dir}"/cli.nix \
--argstr diskoFile "$disko_config" \
--argstr mode "$mode" \ --argstr mode "$mode" \
"${nix_args[@]}" "${nix_args[@]}"
) )
if [[ ! -z "${dry_run+x}" ]]; then
echo "$script"
else
exec "$script" exec "$script"
fi