From 6f72b636019031de5bca89c63fc1a00a8b3d0768 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sat, 7 Jul 2018 20:26:28 +0200 Subject: nixos/nixos-option: don't abort in case of evaluation errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running e.g. `nixos-option boot.kernelPackages` I get an output like this on the current unstable channel (18.09pre144959.be1461fc0ab): ``` $ nixos-option boot.kernelPackages Value: *exit 1* ``` This is fairly counter-intuitive as I have no clue what might went wrong. `strace` delivers an output like this: ``` read(3, "error: Package \342\200\230cryptodev-linu"..., 128) = 128 read(3, "ux/cryptodev/default.nix:22 is m"..., 128) = 128 read(3, "lowBroken = true; }\nin configura"..., 128) = 128 read(3, "you can add\n { allowBroken = tr"..., 128) = 128 read(3, "n)\n", 128) = 3 read(3, "", 128) = 0 ``` `nixos-option` evaluates the system config using `nix-instantiate` which might break when the evaluation fails (e.g. due to broken or unfree packages that are prohibited to evaluate by default). The script aborts due to the shebang `@shell@ -e`. In order to ensure that no unexpected behavior occurs due to removing `-e` from the interpreter the easiest way to work around this was to wrap `nix-instantiate` in `evalNix()` with a `set +e`. The function checks the success of the evaluation with `$?` in the end. Additionally `evalNix` shouldn't break, if one evaluation (e.g. the values that contain a package set by default) to return additional information like a description. With the change `nixos-option boot.kernelPackages` delivers the following output for me: ``` Value: error: Package ‘cryptodev-linux-1.9-4.14.52’ in /nix/store/47z2s8cwppymmgzw6n7pbcashikyk5jk-nixos/nixos/pkgs/os-specific/linux/cryptodev/default.nix:22 is marked as broken, refusing to evaluate. Default: { __unfix__ = ; acpi_call = ; amdgpu-pro = ; ati_drivers_x11 = ; batman_adv = ; bbswitch = ; bcc = ; beegfs-module = ; blcr = ; broadcom_sta = ; callPackage = ; cpupower = ; cryptodev = ; dpdk = ; e1000e = ; ena = ; evdi = ; exfat-nofuse = ; extend = ; facetimehd = ; fusionio-vsl = ; hyperv-daemons = ; ixgbevf = ; jool = ; kernel = ; lttng-modules = ; mba6x_bl = ; mwprocapture = ; mxu11x0 = ; ndiswrapper = ; netatop = ; nvidiaPackages = ; nvidia_x11 = ; nvidia_x11_beta = ; nvidia_x11_legacy304 = ; nvidia_x11_legacy340 = ; nvidiabl = ; odp-dpdk = ; openafs = ; openafs_1_8 = ; perf = ; phc-intel = ; pktgen = ; ply = ; prl-tools = ; recurseForDerivations = true; rtl8192eu = ; rtl8723bs = ; rtl8812au = ; rtl8814au = ; rtlwifi_new = ; sch_cake = ; spl = ; splLegacyCrypto = ; splStable = ; splUnstable = ; stdenv = ; sysdig = ; systemtap = ; tbs = ; tmon = ; tp_smapi = ; usbip = ; v4l2loopback = ; v86d = ; vhba = ; virtualbox = ; virtualboxGuestAdditions = ; wireguard = ; x86_energy_perf_policy = ; zfs = ; zfsLegacyCrypto = ; zfsStable = ; zfsUnstable = ; } Example: { _type = "literalExample"; text = "pkgs.linuxPackages_2_6_25"; } Description: "This option allows you to override the Linux kernel used by\nNixOS. Since things like external kernel module packages are\ntied to the kernel you're using, it also overrides those.\nThis option is a function that takes Nixpkgs as an argument\n(as a convenience), and returns an attribute set containing at\nthe very least an attribute kernel.\nAdditional attributes may be needed depending on your\nconfiguration. For instance, if you use the NVIDIA X driver,\nthen it also needs to contain an attribute\nnvidia_x11.\n" Declared by: "/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/nixos/modules/system/boot/kernel.nix" Defined by: "/home/ma27/Projects/nixos-config/system/boot.nix" ``` --- nixos/modules/installer/tools/nixos-option.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'nixos/modules') diff --git a/nixos/modules/installer/tools/nixos-option.sh b/nixos/modules/installer/tools/nixos-option.sh index 5141f3cd51cf..3f1e591b97b0 100644 --- a/nixos/modules/installer/tools/nixos-option.sh +++ b/nixos/modules/installer/tools/nixos-option.sh @@ -16,6 +16,7 @@ verbose=false nixPath="" option="" +exit_code=0 argfun="" for arg; do @@ -74,8 +75,13 @@ fi ############################# evalNix(){ + # disable `-e` flag, it's possible that the evaluation of `nix-instantiate` fails (e.g. due to broken pkgs) + set +e result=$(nix-instantiate ${nixPath:+$nixPath} - --eval-only "$@" 2>&1) - if test $? -eq 0; then + exit_code=$? + set -e + + if test $exit_code -eq 0; then cat <