Referencing a Key-Value Pair in nix

I am not very well versed in functional programming and it’s catching up to me now as I am attempting use a conditional in my configuration.nix. my goal is to let my config know which machine it is on by using the contents of /sys/devices/virtual/dmi/id/product_family as a key and then importing that key’s value in my nix config.

  computers = {
    "HP OMEN" = "/path/to/nix/config/devices/HPOmen.nix";
    "Lenovo T470" = "/path/to/nix/config/devices/T470.nix";
  };

I would then add result of the comparison to the imported files in the configuration.nix.

imports = 
[
      "/path/to/nix/config/hardware-configuration.nix"
      computer_configuration
];

Any help would be appreciated!

I think this could be possible by using builtins.readFile to inspect the contents of that file and base your evaluation on that. This would not be possible if you were in pure mode.
That being said, I would highly discourage such practice, because it would make your evaluation dependent on external information. This creates a number of problems. You can no longer build your configuration on any other computer (this can be useful for recovery or remote deployment), your configuration is much less reproducible. Also if the path to that information ever changes or the format of it changes then the evaluation will fail.

1 Like