From cf73196411928e1dfc6784a8a1c67a467533af4c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 22 Dec 2021 12:52:06 +0100 Subject: [PATCH 1/2] nixpkgs: Check overlays types This seems to be a common mistake and is worth checking so we can provide a better error message. --- pkgs/top-level/default.nix | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkgs/top-level/default.nix b/pkgs/top-level/default.nix index 10cf36d4d13e..10d0c79b2129 100644 --- a/pkgs/top-level/default.nix +++ b/pkgs/top-level/default.nix @@ -49,6 +49,15 @@ let # Rename the function arguments in let lib = import ../../lib; + throwIfNot = b: msg: if b then x: x else throw msg; + + checked = + throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list." + lib.foldr (x: throwIfNot (lib.isFunction x) "All overlays passed to nixpkgs must be functions.") (r: r) overlays + throwIfNot (lib.isList crossOverlays) "The crossOverlays argument to nixpkgs must be a list." + lib.foldr (x: throwIfNot (lib.isFunction x) "All crossOverlays passed to nixpkgs must be functions.") (r: r) crossOverlays + ; + localSystem = lib.systems.elaborate args.localSystem; # Condition preserves sharing which in turn affects equality. @@ -121,4 +130,4 @@ in let pkgs = boot stages; -in pkgs +in checked pkgs From f2c5c706f47024fd477655fc34e883ce15172484 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 22 Dec 2021 13:05:55 +0100 Subject: [PATCH 2/2] lib.throwIfNot: init --- lib/default.nix | 3 ++- lib/trivial.nix | 22 ++++++++++++++++++++++ pkgs/top-level/default.nix | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 626a751cb10a..fe5d2db0db8f 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -66,7 +66,8 @@ let stringLength sub substring tail trace; inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max - importJSON importTOML warn warnIf info showWarnings nixpkgsVersion version + importJSON importTOML warn warnIf throwIfNot + info showWarnings nixpkgsVersion version mod compare splitByAndCompare functionArgs setFunctionArgs isFunction toHexString toBaseDigits; inherit (self.fixedPoints) fix fix' converge extends composeExtensions diff --git a/lib/trivial.nix b/lib/trivial.nix index 33b553ac4191..c961d3aa7301 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -325,6 +325,28 @@ rec { */ warnIf = cond: msg: if cond then warn msg else id; + /* + Like the `assert b; e` expression, but with a custom error message and + without the semicolon. + + If true, return the identity function, `r: r`. + + If false, throw the error message. + + Calls can be juxtaposed using function application, as `(r: r) a = a`, so + `(r: r) (r: r) a = a`, and so forth. + + Type: bool -> string -> a -> a + + Example: + + throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list." + lib.foldr (x: throwIfNot (lib.isFunction x) "All overlays passed to nixpkgs must be functions.") (r: r) overlays + pkgs + + */ + throwIfNot = cond: msg: if cond then x: x else throw msg; + info = msg: builtins.trace "INFO: ${msg}"; showWarnings = warnings: res: lib.foldr (w: x: warn w x) res warnings; diff --git a/pkgs/top-level/default.nix b/pkgs/top-level/default.nix index 10d0c79b2129..e95a0db78c3c 100644 --- a/pkgs/top-level/default.nix +++ b/pkgs/top-level/default.nix @@ -49,7 +49,7 @@ let # Rename the function arguments in let lib = import ../../lib; - throwIfNot = b: msg: if b then x: x else throw msg; + inherit (lib) throwIfNot; checked = throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list."