about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-11-02 18:25:48 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-11-02 18:25:48 +0100
commit444a5231810e2575bb8955bca6492ef73e4388d5 (patch)
tree7c6a946d6be9282221cb8ee480c40b12156d8173
parentfix(flake/nixosConfigurations/common): Actually enable the new `less` module (diff)
downloadnixos-config-444a5231810e2575bb8955bca6492ef73e4388d5.tar.gz
nixos-config-444a5231810e2575bb8955bca6492ef73e4388d5.zip
fix(lib/maybeMerge): Actually output a warning if things will be overridden
-rw-r--r--lib/default.nix29
-rw-r--r--pkgs/default.nix5
2 files changed, 18 insertions, 16 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 7c86aba1..dfa9fb17 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,4 +1,7 @@
 {}: let
+  # string -> string -> string
+  colorize = color: string: "[${color}m${string}";
+
   # Taken from `nixpkgs/lib`.
   # Is here to avoid a dependency on `lib` (making this file easy to test with `nix eval`)
   warn =
@@ -9,13 +12,13 @@
       msg: v:
       # `builtins.warn` requires a string message, so we enforce that in our implementation, so that callers aren't accidentally incompatible with newer Nix versions.
         assert builtins.isString msg;
-          builtins.trace "evaluation warning: ${msg}" v
+          builtins.trace "${colorize "1;35" "evaluation warning:"} ${msg}" v
     );
 in {
   mkByName = import ./by-name-overlay.nix {inherit warn;};
 
   # Only merge two attrsets if the RHS does not share names with the LHS.
-  # Example:
+  # # Example:
   # ```nix
   # let
   # attr1 = {atuin = "first"; default = "second";};
@@ -23,15 +26,17 @@ in {
   # in
   # maybeMerge attr1 attr2 "Failed to merge"
   # ```
-  maybeMerge = attr1: attr2: message: let
-    list1 = builtins.attrNames attr1;
-
-    check = name2: value:
-      if builtins.elem name2 list1
-      then warn "Overriding ${name2} while merging two attrs sets: ${message}" value
-      else value;
-
-    checkedAttr2 = builtins.mapAttrs check attr2;
+  # # Type:
+  # {} -> {} -> string -> {}
+  maybeMerge = lhs: rhs: sourceSet: let
+    overridden = builtins.intersectAttrs lhs rhs;
+    merged = lhs // rhs;
+    mkWarning = overriddenAttrs:
+      builtins.concatStringsSep " " (builtins.map (colorize "1;97") (builtins.attrNames overriddenAttrs));
   in
-    attr1 // checkedAttr2;
+    if overridden != {}
+    then
+      warn "Attributes overridden while merging ${sourceSet}: ${mkWarning overridden}"
+      merged
+    else merged;
 }
diff --git a/pkgs/default.nix b/pkgs/default.nix
index 39d225a9..294d5b91 100644
--- a/pkgs/default.nix
+++ b/pkgs/default.nix
@@ -5,10 +5,7 @@
 }: let
   inherit (pkgs) lib;
 
-  # FIXME: Make this override check actually work.
-  # I think that some parts of the lazy eval are causing that to not actually evaluate the
-  # error message. <2024-10-23>
-  maybeMergeMessage = "While merging the pkgs in ./pkgs/by-name to the nixpkgs set.";
+  maybeMergeMessage = "the ./pkgs/by-name set";
   mMM = maybeMergeMessage;
   callPackage = lib.callPackageWith (nixLib.maybeMerge (nixLib.maybeMerge pkgs myPkgs mMM) {inherit sysLib;} mMM);