Host specific configuration

Hi! I have the following problem. I have two machines: A and B. machine A i an old laptop that I use as a desktop, I want it to be always on so I added these lines to my conf.nix:

  systemd.sleep.extraConfig = ''
    AllowSuspend=no
    AllowHibernation=no
    AllowHybridSleep=no
    AllowSuspendThenHibernate=no
  '';

And it worked fine. Machine B is a laptop that I take with me to work. Both run NixOS. Now I am syncing /etc/nixos on both machines via syncthing and I don’t want that systemd.sleep.extraConfig on machine B. Is it possible to add some kind of “if clause” to conf.nix based on hostname to achieve that?

Don’t. Just create separate configs for each host and import the common config in each. Conditionals on hostname makes for unmaintainable configs.

3 Likes

Okay, thanks. I did that, but by curiosity this is possible?

Yes, sometimes it does make sense for one configuration value to be enabled by another configuration value, and in that case one would write something like this:

system.sleep.extraConfig = lib.mkIf (config.foo.bar == "some value") ''
   ...
'';

For more, see the NixOS manual.

Thank you very much!