Generate configuration from json

I eventually figure it out! Below is for any potential future readers :slight_smile:

I had to change my vlanNetdev function:

  vlanNetdev = ({ name, vid, enabled ? false, ... }: {
    name = name;
    value = {
      enable = enabled;
      netdevConfig = {
        Name = name;
        Kind = "vlan";
      };
      vlanConfig.Id = vid;
    };
  });

Then map this to the list of interfaces:

gen = builtins.fromJSON (builtins.readFile ./interfaces.json);
netdevs = builtins.map vlanNetdev gen.interfaces;

This transforms the data from interfaces.json into a name/value pair, which I can use to merge this with my manually defined configuration:

  systemd.network = {
    netdevs = builtins.listToAttrs netdevs // {
      "example" = {
        netdevConfig = {
          Name = "example";
          Kind = "vlan";
          Description = "example netdev";
        };
        vlanConfig.Id = 200;
      };

Understanding basic nix unlocked a lot of nifty things with my NixOS configuration! I can now avoid manually typing out anything I keep in interfaces.json. I have probably spent more time writing these nix functions than I will ever save on using them, but that is not the point!

1 Like