Variables in system.activationScripts optiom

Good morning everyone,

This is my first post here and hopefully i will get unblocked.

So, What a need to do is theoretically simple, I need to use one variable to set one configuration for my service based on the instance name.

This is the scenario:

I have a variable called address that is used in this line on my service configuration:

server ${address} 255.255.0.0

However this variable address should be set based on the host name, to do this I’m using this block of code:

address_1="10.10.1.0";
address_2="10.10.2.0";
address="";

system.activationScripts.script.text = ''
      if [ "$(cat /etc/ec2-tags/name)" == "instance-1" ]; then
       ${address}=${address_1}
      else
       ${address}=${address_2}
      fi
  '';

The logic of this above code is, if the hostname is “instance-1” the variable address will receive the value of address_1 if not the address will receive address_2.

The if clause is working, however when i deploy the variable address is receiving this value: “=10.10.1.0”

My need is set the value to address variable that will be used in the next block of code.

Any ideas?

You can not set a Nix variable from an activation script, building the NixOS config and its activation happen at different times. If you set your networking.hostName to the same value as the EC2 tag, you can do this:

address = if config.networking.hostName == "instance-1" then address1 else address2;

I’ve tried to do in this way, but unfortunately do not work, the host-names are not set before than activation step. Before the activation the instances have the same name and it will be update after activation (configuration-switch)

I think there is no way to do it. I will need to work with some environmental variable, and template the configuration.