diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-24 14:59:29 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-24 14:59:29 +0200 |
commit | 0cc353f49f9c5d0e0bd203a6854b488a58daaa96 (patch) | |
tree | 48d4a821a842f94e3614f7c310423d9d2c98cc21 /lib/default.nix | |
parent | refactor(modules/unison): Migrate to `by-name` and parameterize (diff) | |
download | nixos-config-0cc353f49f9c5d0e0bd203a6854b488a58daaa96.tar.gz nixos-config-0cc353f49f9c5d0e0bd203a6854b488a58daaa96.zip |
refactor({modules,pkgs}/by-name-overlay): De-duplicate in a `nixLib`
Diffstat (limited to 'lib/default.nix')
-rw-r--r-- | lib/default.nix | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 00000000..7c86aba1 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,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 "[1;35mevaluation warning:[0m ${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; +} |