Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deeply merge sets in Nix

Tags:

nix

nixos

in Nix, you can use // to merge two sets and replace the lefts attributes with the right ones if they're double, however. In this example:

let 
  
  set_one = {
    nested_set = {
        some_value_that_will_be_lost = "some forgotten value";
        some_attribute = "some value";
    };
  };

  set_two = {
    nested_set = {
        some_attribute = "some other value";
    };
  };

in ( set_one // set_two )

will result in:

{
    nested_set = {
        some_attribute = "some other value";
    };
}

while I am expecting:

{
    nested_set = {
        some_value_that_will_be_lost = "some forgotten value";
        some_attribute = "some other value";
    };
}

I tried to see if lib had any useful functions, and I found lib.modules.mergeModules, and tried:

let 

  lib = (import <nixpkgs> { }).pkgs.lib;

  set_one = {
    nested_set = {
        some_value_that_will_be_lost = "some forgotten value";
        some_attribute = "some value";
    };
  };

  set_two = {
    nested_set = {
        some_attribute = "some other value";
    };
  };

in ( lib.modules.mergeModules set_one set_two )

But that module was depricated, and only resulted in errors.

The problem is that I want the implementation to work regardless of what the set is. Is there a way to do this that you know of to deeply/recursively merge two sets without knowing it's contents?

like image 689
St-H Avatar asked Oct 15 '25 04:10

St-H


1 Answers

See lib.attrsets.recursiveUpdate

let
  pkgs = import <nixpkgs> {};
  lib = pkgs.lib;
  set_one = {
    nested_set = {
      some_value_that_will_be_lost = "some forgotten value";
      some_attribute = "some value";
    };
  };
  set_two = {
    nested_set = {
      some_attribute = "some other value";
    };
  };
in
lib.recursiveUpdate set_one set_two

evaluates to

{ nested_set = { some_attribute = "some other value"; some_value_that_will_be_lost = "some forgotten value"; }; }
like image 188
user24653487 Avatar answered Oct 18 '25 13:10

user24653487



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!