Configuration.nix - activate an option if variable exists

nixos 22.05

wifi="wpl123" ;
if "${wifi}" != ""  then interfaces."${wifi}".useDHCP = true ;

I would like to only add the line (the interface) to the config if "${wifi}" != "" (if it exists)

Wouldn’t like to do it fixed via a import script per device

How to do this in nix?

the current inputs:

(the aim is to provide a config for all devices in for every human readable and adjustable manner ~> json)
all parameter/variables not in in a specific device (like "P3") will be checked in "default".


    "default" : {
            "userNix"        : "usere",
            "userAdm"        : "AdminE",
            "usertest"       : "TestE",
            "national"       : "de",
            "aeBackupSSDext" : "backupSSDExternal",

            "versionNixos"   : "22.05",

            "dev_luk"        : "/dev/nvme0n1p5" ,
            "partitionName"  : "nixos" ,

            "linux_kernel"   : "linuxPackages_latest" ,

            "comment"        : "  "  
    "L13" : {
        "versionNixos"  : "22.05" , 
        "hostnamePC"    : "L13y" ,

        "lan"           : "eno9" ,
        "wifi"          : "wlp0s1234" ,

        "gpu"           : "intel-egpu" ,
        "pcType"        : "laptop" ,

        "comment"        : ""  
    "P3" : {
        "versionNixos"  : "21.11" , 
        "hostnamePC"    : "nix" ,

        "lan"           : "eno3" ,
        "wifi"          : "" ,

        "gpu"           : "nvidia" ,
        "pcType"        : "tower" ,

        "linux_kernel"  : "linuxPackages_5_15" ,

        "comment"       : "ToDo - .." 


You’re probably going to want to start learning about creating custom options/modules: NixOS - NixOS 22.05 manual

In this case you’d make a custom option named something like myoptions.wifi that defaults to null or something, and then you’d set:

interfaces = lib.mkIf (config.myoptions.wifi != null) {
  "${config.myoptions.wifi}".useDHCP = true;

Thanks for the idea
interfaces is an already defined
configuration.nix", "message": "attribute 'interfaces' at /home/ae/ae_share_VM/z_DS_setup/vm_setup/NixOS/configuration/configuration.nix:685:7 already defined",

I only want to add a wifi device (with its name and the value true for useDHCP) if the device has an internal/external wifi chip - interfaces."${wifi}"
(at the moment I define if a device has a wifi chip via a string in a config file [where all devices are listed/configured])

before configuring the value for interfaces."${wifi}".useDHCP, the device "${wifi}" should only be created if the device has an wifi chip
(It is possible to create "${wifi}" as empty sting but that leads to a longer start up of the OS because is trying to load/configure the “”-wifi device )

As I said, you’re going to want to learn about the module system. If one module already has an interfaces definition (e.g. for the other interfaces you care about), you’ll need a separate module to do the mkIf thing. You can either include that module via the imports line, or by having multiple module configs in a mkMerge call. I suggest you read the section.

imports can not solve the issue, right?

  • as I wrote, I don’t want to create a import script for each device

merge could be needed (done automatically by nix) BUT only if interfaces."${wifi}" exists (and wifi="" should be treated as not existing)

The focus here probably shouldn’t be at the interfaces but interfaces."${wifi}"
The question is how to create interfaces."${wifi}" depending on other variables (/if the device has an wifi chip)

Ah, I misread. No I wasn’t suggesting a different import for each device. I was suggesting that you make a custom option that provides conditional configuration depending on how each device has configured its wifi definition.

I’m not sure if I understand you correct. (so I updated the description with the input json)

“conditional configuration depending on how each device”

  • which means of the nix language can be used for this purpose?

There is no need of new options (for the nixos configuration)

  • it enough to use the standard of 22.05

To merge a new/specified device (via interfaces."${wifi}") like interfaces."wlp0s1234" and interfaces.""

  • “works”
  • but device "" will prolong boot/start up time of the OS significantly

interfaces."${wifi}" should only be added to the configuration (“as whole line”) if "${wifi}" exists for a specific device

  • interfaces shouldn’t be overwritten