From 5cc4edc985b05919f81cda6006328869ba0ae95f Mon Sep 17 00:00:00 2001
From: stuebinm <stuebinm@disroot.org>
Date: Mon, 22 Apr 2024 16:52:31 +0200
Subject: [PATCH 1/3] lib.trivial: rename NIX_ABORT_ON_WARN to
 NIXPKGS_ABORT_ON_WARN

this makes it consistent with other environment variables read by
nixpkgs to configure its behaviour. The previous naming appeared to
suggest that the variable is read by Nix itself, which it is not. This
is especially a source of confusion when used together with Nix's pure
evaluation mode, in which case setting the variable has no effect.

The old NIX_ABORT_ON_WARN is still honoured, but produces a deprecation
warning. This warning's check is added as a wrapper to the entire
lib.trivial attrset, as it should be checked (and, if necessary,
printed) regardless of whether or not a use of lib.warn is encountered,
but whenever the lib.warn function is available (i.e. whenever lib
itself is evaluated), so that users of NIX_ABORT_ON_WARN will see it
even if their code does not currently produce any warnings. Likewise,
this means that the warning does not actually abort evaluation, as
otherwise downstream users of NIX_ABORT_ON_WARN would be immediately
presented with an unrecoverable error.
---
 lib/trivial.nix | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/trivial.nix b/lib/trivial.nix
index 5b7a1ee30f7ad..e5c6963f1df7b 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -12,7 +12,20 @@ let
     version
     versionSuffix
     warn;
-in {
+
+  # We have to use builtins here in place of lib.warn, as otherwise its
+  # definition would depend on itself. Further, we do not want this to
+  # actually abort even if NIX_ABORT_ON_WARN is set, as otherwise legacy
+  # uses of this variable would immediately result in an unrecoverable
+  # error where there should be a deprecation warning.
+  printDeprecationWarning =
+    if builtins.getEnv "NIX_ABORT_ON_WARN" != "" then
+      builtins.trace
+        "warning: NIX_ABORT_ON_WARN has been renamed to NIXPKGS_ABORT_ON_WARN. NIX_ABORT_ON_WARN is still honored, but deprecated and will be removed from nixpkgs after the 24.11 release."
+    else
+      a: a;
+
+in printDeprecationWarning {
 
   ## Simple (higher order) functions
 
@@ -698,8 +711,10 @@ in {
     ```
   */
   warn =
-    if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"]
-    then msg: builtins.trace "warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.")
+    if lib.elem (builtins.getEnv "NIXPKGS_ABORT_ON_WARN") [ "1" "true" "yes" ]
+    # this latter clause is deprecated. When removing, also remove printDeprecationWarning above.
+    || lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") [ "1" "true" "yes" ]
+    then msg: builtins.trace "warning: ${msg}" (abort "NIXPKGS_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.")
     else msg: builtins.trace "warning: ${msg}";
 
   /**

From a87b0a8305ee83e310c3d5fb70647ea84c53ae28 Mon Sep 17 00:00:00 2001
From: stuebinm <stuebinm@disroot.org>
Date: Mon, 22 Apr 2024 16:55:27 +0200
Subject: [PATCH 2/3] {nixos,nixpkgs-basic-release-check.nix}: use
 NIXPKGS_ABORT_ON_WARN

as NIX_ABORT_ON_WARN has been deprecated.
---
 nixos/modules/misc/documentation.nix            | 2 +-
 pkgs/top-level/nixpkgs-basic-release-checks.nix | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/nixos/modules/misc/documentation.nix b/nixos/modules/misc/documentation.nix
index 26323e14b9017..c18abaf7b3f48 100644
--- a/nixos/modules/misc/documentation.nix
+++ b/nixos/modules/misc/documentation.nix
@@ -101,7 +101,7 @@ let
           libPath = filter (pkgs.path + "/lib");
           pkgsLibPath = filter (pkgs.path + "/pkgs/pkgs-lib");
           nixosPath = filter (pkgs.path + "/nixos");
-          NIX_ABORT_ON_WARN = warningsAreErrors;
+          NIXPKGS_ABORT_ON_WARN = warningsAreErrors;
           modules =
             "[ "
             + concatMapStringsSep " " (p: ''"${removePrefix "${modulesPath}/" (toString p)}"'') docModules.lazy
diff --git a/pkgs/top-level/nixpkgs-basic-release-checks.nix b/pkgs/top-level/nixpkgs-basic-release-checks.nix
index ea18890903281..1661dbaf132b6 100644
--- a/pkgs/top-level/nixpkgs-basic-release-checks.nix
+++ b/pkgs/top-level/nixpkgs-basic-release-checks.nix
@@ -42,7 +42,7 @@ pkgs.runCommand "nixpkgs-release-checks"
 
         # To get a call trace; see https://nixos.org/manual/nixpkgs/stable/#function-library-lib.trivial.warn
         # Relies on impure eval
-        export NIX_ABORT_ON_WARN=true
+        export NIXPKGS_ABORT_ON_WARN=true
 
         set +e
         (
@@ -79,7 +79,7 @@ pkgs.runCommand "nixpkgs-release-checks"
             exit 1
         fi
 
-        # Catch any trace calls not caught by NIX_ABORT_ON_WARN (lib.warn)
+        # Catch any trace calls not caught by NIXPKGS_ABORT_ON_WARN (lib.warn)
         if [ -s eval-warnings.log ]; then
             echo "Nixpkgs on $platform evaluated with warnings, aborting"
             exit 1

From 7edb2fc1d90c740974654e991f102b8ca0b4333f Mon Sep 17 00:00:00 2001
From: stuebinm <stuebinm@disroot.org>
Date: Mon, 22 Apr 2024 23:20:10 +0200
Subject: [PATCH 3/3] lib.trivial: guard NIX_ABORT_ON_WARN deprecation behind
 24.05 release

As by https://github.com/NixOS/nixpkgs/pull/306026#discussion_r1575142368

This has to temporarily move oldestSupportedRelease up outside the main
attribute set so that it can be used before lib.trivial itself is defined.
---
 lib/trivial.nix | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/trivial.nix b/lib/trivial.nix
index e5c6963f1df7b..dc0f4ed4c84f6 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -19,12 +19,16 @@ let
   # uses of this variable would immediately result in an unrecoverable
   # error where there should be a deprecation warning.
   printDeprecationWarning =
-    if builtins.getEnv "NIX_ABORT_ON_WARN" != "" then
+    if 2405 <= oldestSupportedRelease && builtins.getEnv "NIX_ABORT_ON_WARN" != "" then
       builtins.trace
         "warning: NIX_ABORT_ON_WARN has been renamed to NIXPKGS_ABORT_ON_WARN. NIX_ABORT_ON_WARN is still honored, but deprecated and will be removed from nixpkgs after the 24.11 release."
     else
       a: a;
 
+  # Temporarily moved here to prevent lib.trivial from referencing itself.
+  # See the `inherit oldestSupportedRelease' further down.
+  oldestSupportedRelease = 2311;
+
 in printDeprecationWarning {
 
   ## Simple (higher order) functions
@@ -390,9 +394,8 @@ in printDeprecationWarning {
     This release number allows deprecation warnings to be implemented such that
     they take effect as soon as the oldest release reaches end of life.
   */
-  oldestSupportedRelease =
     # Update on master only. Do not backport.
-    2311;
+  inherit oldestSupportedRelease;
 
   /**
     Whether a feature is supported in all supported releases (at the time of