How to properly turn off wifi on boot or before shutdown or reboot in NixOS?

My goal is making my laptop not to turn on wifi automatically after booting it up.
Now I am using this in configuration.nix to turn off wifi on boot, which is translated from the systemd unit file that I used in my previous distro:

	systemd.services."disable-wifi-on-boot" = {
		description = "Disable wifi on boot via nmcli";
		after = [ "NetworkManager.service" ];
		wantedBy = [ "multi-user.target" ];
		serviceConfig = {
			ExecStart = "${pkgs.networkmanager}/bin/nmcli radio wifi off";
			Type = "oneshot";
		};
	};
}

It works, however it turn off the wifi every time when I run sudo nixos-rebuild switch.
How to properly do it in NixOS? Thanks!

You can try systemd.services.disable-wifi-on-boot.restartIfChanged = false;

2 Likes

I finally have a chance to test it
Sadly it doesn’t have any effect.
I’ve also tried reloadIfChange, restartTriggers and reloadTriggers and stopIfChanged, none of them have any effect.
Actually the above service starts everytime I run nixos-rebulid switch even if the systemd unit is unchanged.

Ah it’s a oneshot. Could you try like that:

systemd.services."disable-wifi-on-boot" = {
        restartIfChanged = false;
		description = "Disable wifi on boot via nmcli";
		after = [ "NetworkManager.service" ];
		wantedBy = [ "multi-user.target" ];
		serviceConfig = {
			ExecStart = "${pkgs.networkmanager}/bin/nmcli radio wifi off";
			Type = "oneshot";
            RemainAfterExit = "true";
		};
	};
1 Like

It’s hard to know exactly the end desired behavior, but this smells like a use-case for rfkill.

You could set rfkill to always block wlan on shutdown/ (re-do on bootup to protect against unexpected power-off). rfkill will hide the radio from the system entirely.

Then, whenever you want, you can rfkill unblock the wlan radio, do whatever, then reset and block on shutdown/boot.

1 Like

Yes, the RemainAfterExit trick works! Thank you very much!

Nice advice, I will give it a try! thanks!