From c5853dcb32ad4a7711af9513ddfc49da171d5f00 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Wed, 18 Sep 2024 18:54:37 +0700 Subject: [PATCH] module: throw if no disks are defined --- lib/default.nix | 88 ++++++++++++++++++++++++++----------------------- module.nix | 35 +++++++++++--------- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index f5ebae9..ccf169a 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -388,55 +388,59 @@ let description = '' The scripts generated by disko ''; - default = { pkgs, checked ? false }: { - destroyScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-destroy" '' - export PATH=${lib.makeBinPath (with pkgs; [ - util-linux - e2fsprogs - mdadm - zfs - lvm2 - bash - jq - gnused - gawk - coreutils-full - ])}:$PATH - ${cfg.config._destroy} - ''; + default = { pkgs, checked ? false }: + let + throwIfNoDisksDetected = _: v: if devices.disk == { } then throw "No disks defined, did you forget to import your disko config?" else v; + in + mapAttrs throwIfNoDisksDetected { + destroyScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-destroy" '' + export PATH=${lib.makeBinPath (with pkgs; [ + util-linux + e2fsprogs + mdadm + zfs + lvm2 + bash + jq + gnused + gawk + coreutils-full + ])}:$PATH + ${cfg.config._destroy} + ''; - formatScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-format" '' - export PATH=${lib.makeBinPath (cfg.config._packages pkgs)}:$PATH - ${cfg.config._create} - ''; + formatScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-format" '' + export PATH=${lib.makeBinPath (cfg.config._packages pkgs)}:$PATH + ${cfg.config._create} + ''; - mountScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-mount" '' - export PATH=${lib.makeBinPath (cfg.config._packages pkgs)}:$PATH - ${cfg.config._mount} - ''; + mountScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-mount" '' + export PATH=${lib.makeBinPath (cfg.config._packages pkgs)}:$PATH + ${cfg.config._mount} + ''; - diskoScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko" '' - export PATH=${lib.makeBinPath ((cfg.config._packages pkgs) ++ [ pkgs.bash ])}:$PATH - ${cfg.config._disko} - ''; + diskoScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko" '' + export PATH=${lib.makeBinPath ((cfg.config._packages pkgs) ++ [ pkgs.bash ])}:$PATH + ${cfg.config._disko} + ''; - # These are useful to skip copying executables uploading a script to an in-memory installer - destroyScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-destroy" '' - ${cfg.config._destroy} - ''; + # These are useful to skip copying executables uploading a script to an in-memory installer + destroyScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-destroy" '' + ${cfg.config._destroy} + ''; - formatScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-format" '' - ${cfg.config._create} - ''; + formatScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-format" '' + ${cfg.config._create} + ''; - mountScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" '' - ${cfg.config._mount} - ''; + mountScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" '' + ${cfg.config._mount} + ''; - diskoScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko" '' - ${cfg.config._disko} - ''; - }; + diskoScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko" '' + ${cfg.config._disko} + ''; + }; }; _destroy = lib.mkOption { internal = true; diff --git a/module.nix b/module.nix index 4a5e070..0079424 100644 --- a/module.nix +++ b/module.nix @@ -200,23 +200,28 @@ in eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix"); }; - system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // { - # we keep these old outputs for compatibility - disko = builtins.trace "the .disko output is deprecated, please use .diskoScript instead" (cfg.devices._scripts { inherit pkgs; }).diskoScript; - diskoNoDeps = builtins.trace "the .diskoNoDeps output is deprecated, please use .diskoScriptNoDeps instead" (cfg.devices._scripts { inherit pkgs; }).diskoScriptNoDeps; + system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // ( + let + throwIfNoDisksDetected = _: v: if cfg.devices.disk == { } then throw "No disks defined, did you forget to import your disko config?" else v; + in + lib.mapAttrs throwIfNoDisksDetected { + # we keep these old outputs for compatibility + disko = builtins.trace "the .disko output is deprecated, please use .diskoScript instead" (cfg.devices._scripts { inherit pkgs; }).diskoScript; + diskoNoDeps = builtins.trace "the .diskoNoDeps output is deprecated, please use .diskoScriptNoDeps instead" (cfg.devices._scripts { inherit pkgs; }).diskoScriptNoDeps; - installTest = diskoLib.testLib.makeDiskoTest { - inherit extendModules pkgs; - name = "${config.networking.hostName}-disko"; - disko-config = builtins.removeAttrs config [ "_module" ]; - testMode = "direct"; - efi = cfg.tests.efi; - extraSystemConfig = cfg.tests.extraConfig; - extraTestScript = cfg.tests.extraChecks; - }; + installTest = diskoLib.testLib.makeDiskoTest { + inherit extendModules pkgs; + name = "${config.networking.hostName}-disko"; + disko-config = builtins.removeAttrs config [ "_module" ]; + testMode = "direct"; + efi = cfg.tests.efi; + extraSystemConfig = cfg.tests.extraConfig; + extraTestScript = cfg.tests.extraChecks; + }; - vmWithDisko = lib.mkDefault config.virtualisation.vmVariantWithDisko.system.build.vmWithDisko; - }; + vmWithDisko = lib.mkDefault config.virtualisation.vmVariantWithDisko.system.build.vmWithDisko; + } + ); # we need to specify the keys here, so we don't get an infinite recursion error # Remember to add config keys here if they are added to types