Nix will refuse to use the directory set by $HOME as your home directory if it’s not owned by the same user as the one executing nix (which in this case is root because you’re using sudo), because otherwise permissions of various user-specific channel/profile files in your user’s home directory could break.
AIUI, normally the sudo policy that forces it to set the home directory to the target users’ is enabled by default on NixOS, though, which should make that feature never trigger. Ultimately there’s no harm done, nix does what sudo should be doing, but I guess you’ve played around with your sudoers file?
On an unrelated note, if you use the (very poorly named) --use-remote-sudo flag instead of manually calling sudo, nix can do all the usual build things without having to run as root, and only call sudo for the actual switch. This is generally nicer and a bit more robust (though it does mean you need to wait for the build to finish to type your password).
but I guess you’ve played around with your sudoers file?
No, I did not. Not as far as I am aware atleast
Also, yesterday, before the migration to flakes, when I was doing the ususal sudo nixos-rebuild switch --upgrade, there was no such warning. Something changed and I dont know what.
After some more googling, I still cant find an answer to how make this warning go away.
But I do have a new suspicion: I think it might be because I moved my flakes from /etc/nixos to ~/repos/dotfiles.
And to be clear, that line does exactly what I expected, it changes your sudoers file to make sudo persist $HOME, which causes nix to print that warning.
Again, it’s not harmful per-se, but it will mess with your users’ files when you call sudo and run certain commands that lack this safety feature nix has, which is why most distros - including NixOS - disable this by default.
Probably, you also should think hard about whether you really want NOPASSWD for all commands (or even any). It makes your user effectively root for security purposes, which isn’t exactly great.