cli/default: refactor to use more code from lib

This commit is contained in:
lassulus 2023-07-13 17:02:23 +02:00 committed by mergify[bot]
parent cdaff66b57
commit f6649d69d9
4 changed files with 36 additions and 39 deletions

View file

@ -85,7 +85,7 @@ A simple disko configuration may look like this:
If you'd saved this configuration in /tmp/disko-config.nix, and wanted to create a disk named /dev/nvme0n1, you would run the following command to partition, format and mount the disk. If you'd saved this configuration in /tmp/disko-config.nix, and wanted to create a disk named /dev/nvme0n1, you would run the following command to partition, format and mount the disk.
``` ```
$ sudo nix run github:nix-community/disko -- --mode zap_create_mount /tmp/disko-config.nix --arg disks '[ "/dev/nvme0n1" ]' $ sudo nix run github:nix-community/disko -- --mode disko /tmp/disko-config.nix --arg disks '[ "/dev/nvme0n1" ]'
``` ```
## Related Tools ## Related Tools

14
cli.nix
View file

@ -17,17 +17,23 @@ let
diskoAttr = diskoAttr =
if noDeps then if noDeps then
{ {
create = "createScriptNoDeps"; format = "formatScriptNoDeps";
mount = "mountScriptNoDeps"; mount = "mountScriptNoDeps";
zap_create_mount = "diskoScriptNoDeps";
disko = "diskoScriptNoDeps"; disko = "diskoScriptNoDeps";
# legacy aliases
create = "createScriptNoDeps";
zap_create_mount = "diskoScriptNoDeps";
}.${mode} }.${mode}
else else
{ {
create = "createScript"; format = "formatScript";
mount = "mountScript"; mount = "mountScript";
zap_create_mount = "diskoScript";
disko = "diskoScript"; disko = "diskoScript";
# legacy aliases
create = "createScript";
zap_create_mount = "diskoScript";
}.${mode}; }.${mode};
hasDiskoConfigFlake = hasDiskoConfigFlake =

View file

@ -18,36 +18,27 @@ let
in in
{ {
lib = diskoLib; lib = diskoLib;
create = cfg: diskoLib.create (eval cfg).config.disko.devices;
createScript = cfg: pkgs: (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-create" '' # legacy alias
export PATH=${lib.makeBinPath (diskoLib.packages (eval cfg).config.disko.devices pkgs)}:$PATH create = cfg: builtins.trace "the create output is deprecated, use format instead" (eval cfg).config.disko.devices._create;
${diskoLib.create (eval cfg).config.disko.devices} createScript = cfg: pkgs: builtins.trace "the create output is deprecated, use format instead" ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).formatScript;
''; createScriptNoDeps = cfg: pkgs: builtins.trace "the create output is deprecated, use format instead" ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).formatScriptNoDeps;
createScriptNoDeps = cfg: pkgs: (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-create" ''
${diskoLib.create (eval cfg).config.disko.devices} format = cfg: (eval cfg).config.disko.devices._create;
''; formatScript = cfg: pkgs: ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).formatScript;
mount = cfg: diskoLib.mount (eval cfg).config.disko.devices; formatScriptNoDeps = cfg: pkgs: ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).formatScriptNoDeps;
mountScript = cfg: pkgs: (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-mount" ''
export PATH=${lib.makeBinPath (diskoLib.packages (eval cfg).config.disko.devices pkgs)}:$PATH mount = cfg: (eval cfg).config.disko.devices._mount;
${diskoLib.mount (eval cfg).config.disko.devices} mountScript = cfg: pkgs: ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).mountScript;
''; mountScriptNoDeps = cfg: pkgs: ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).mountScriptNoDeps;
mountScriptNoDeps = cfg: pkgs: (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" ''
${diskoLib.mount (eval cfg).config.disko.devices} disko = cfg: (eval cfg).config.disko.devices._disko;
''; diskoScript = cfg: pkgs: ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).diskoScript;
disko = cfg: diskoLib.zapCreateMount (eval cfg).config.disko.devices; diskoScriptNoDeps = cfg: pkgs: ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).diskoScriptNoDeps;
diskoScript = cfg: pkgs: (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-zap-create-mount" ''
export PATH=${lib.makeBinPath ((diskoLib.packages (eval cfg).config.disko.devices pkgs) ++ [ pkgs.bash ])}:$PATH
${diskoLib.zapCreateMount (eval cfg).config.disko.devices}
'';
# we keep this old output for backwards compatibility # we keep this old output for backwards compatibility
diskoNoDeps = cfg: pkgs: builtins.trace "the .diskoNoDeps output is deprecated, plase use .diskoScriptNoDeps instead" ( diskoNoDeps = cfg: pkgs: builtins.trace "the diskoNoDeps output is deprecated, please use disko instead" ((eval cfg).config.disko.devices._scripts { inherit pkgs checked; }).diskoScriptNoDeps;
(diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-zap-create-mount" ''
${diskoLib.zapCreateMount (eval cfg).config.disko.devices} config = cfg: (eval cfg).config.disko.devices._config;
'' packages = cfg: (eval cfg).config.disko.devices._packages;
);
diskoScriptNoDeps = cfg: pkgs: (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-zap-create-mount" ''
${diskoLib.zapCreateMount (eval cfg).config.disko.devices}
'';
config = cfg: { imports = diskoLib.config (eval cfg).config.disko.devices; };
packages = cfg: diskoLib.packages (eval cfg).config.disko.devices;
} }

6
disko
View file

@ -18,7 +18,7 @@ or $0 [options] --flake github:somebody/somewhere
Options: Options:
* -m, --mode mode * -m, --mode mode
set the mode, either create, mount, zap_create_mount or disko set the mode, either format, mount or disko
* -f, --flake uri * -f, --flake uri
fetch the disko config relative to this flake's root fetch the disko config relative to this flake's root
* --arg name value * --arg name value
@ -93,8 +93,8 @@ while [[ $# -gt 0 ]]; do
shift shift
done done
if ! { [[ $mode = "create" ]] || [[ $mode = "mount" ]] || [[ $mode = "zap_create_mount" ]] || [[ $mode = "disko" ]]; }; then if ! { [[ $mode = "format" ]] || [[ $mode = "mount" ]] || [[ $mode = "disko" ]] || [[ $mode = "create" ]] || [[ $mode = "zap_create_mount" ]] ; }; then
abort "mode must be either create, mount, zap_create_mount or disko" abort "mode must be either format, mount or disko"
fi fi
if [[ -n "${flake+x}" ]]; then if [[ -n "${flake+x}" ]]; then