about summary refs log tree commit diff stats
path: root/lib/default.nix
blob: 7c86aba1a05cd64aa7fe20a8916e0004381d4cc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{}: let
  # 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 "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:
  # ```nix
  # let
  # attr1 = {atuin = "first"; default = "second";};
  # attr2 = {atuin = "hi"; other = "hi2";};
  # 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;
  in
    attr1 // checkedAttr2;
}