summary refs log tree commit diff stats
path: root/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'default.nix')
-rw-r--r--default.nix42
1 files changed, 42 insertions, 0 deletions
diff --git a/default.nix b/default.nix
new file mode 100644
index 0000000..cb9f6ef
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,42 @@
+{}: 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 =
+    # Since Nix 2.23, https://github.com/NixOS/nix/pull/10592
+    builtins.warn
+    or (
+      # Do not eta reduce v, so that we have the same strictness as `builtins.warn`.
+      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 "${colorize "1;35" "evaluation warning:"} ${msg}" v
+    );
+in {
+  mkByName = import ./by-name-overlay.nix {inherit warn;};
+
+  # Warn while merging two attrsets if the RHS shares names with the LHS.
+  # # Example:
+  # ```nix
+  # let
+  # attr1 = {atuin = "first"; default = "second";};
+  # attr2 = {atuin = "hi"; other = "hi2";};
+  # in
+  # warnMerge attr1 attr2 "attr1 core value set"
+  # ```
+  # # Type:
+  # {} -> {} -> string -> {}
+  warnMerge = 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
+    if overridden != {}
+    then
+      warn "Attributes overridden while merging ${sourceSet}: ${mkWarning overridden}"
+      merged
+    else merged;
+}