A step-by-step guide to not deleting your entire home directory.
I do not know what I am doing. I am not some secret Nix expert. Most importantly, I am not immune to things changing unexpectedly and rendering this guide worthless or actively detrimental. By following this guide, you assume the risk of things not working out properly, including, but not limited to:
- Deleting your home directory
- Leaving you unable to log in due to no user password being set
- Things going wrong in new and unexpected ways that neither of us could have reasonably expected
I have tried my best to ensure that this guide is accurate and as low-risk as possible, but I can't promise anything.
In short, I highly recommend you make some form of backup before attempting this.
Backing up your home folder (/home/<username>) is the bare mimimum, as that is the most likely casualty if things go wrong.
That said, any kind of backup works. Borg, Timeshift, a BTRFS snapshot if you're running that, I don't care, just back things up somehow.
Oh, and this should go without saying, but please for the love of all that is good in this world, follow every step carefully and exactly. Every one of these steps is critical, and so is the order in which you do them. Don't skip a step or you will end up with problems.
Alright, ready?
Throughout this guide, I will be referring to the old username as <old_name> and the new username as <new_name>.
Replace these with whatever username you're changing to/from, don't actually run a command with the placeholders still there or something might break.
First, log out of the user you're renaming. We don't want to get interrupted by our user session collapsing around us.
You could probably do this while logged into a graphical session as another user, but I've only tested this in a terminal-only session, so that's what I'd recommend you do, too.
On the login screen, press Ctrl+Alt+F1 to drop out of the GUI to a terminal session and log in as a user other than the one you're renaming.
All the commands we're going to be running require admin privleges, so either log in as root or prefix these commands with sudo.
Now, once you're logged in, run this command (with sudo if you are not root):
# usermod -l <new_name> -d /home/<new_name> -m <old_name>This will imperitavely rename the user and their home directory.
Next, we need to update the user's groups. For some reason every guide for changing usernames on Linux tells you this step is nessecary, but when I tested it in a VM it did literally nothing on NixOS. Regardless, I'd recommend running it anyway just in case. Absolute worst case scenario, it doesn't do anything and you move on to the next step.
Run the following command with admin permissions:
# groupmod -n <new_name> <old_name>On a normal linux distro we'd be done, but if you stop here, NixOS will likely attempt to reconstruct the user under their original name, as we haven't updated configuration.nix.
That's the next step.
Fire up your command line text editor of choice and edit the file /etc/nixos/configuration.nix.
If you've modularized your config, the lines we're looking for might be in a different file, but I'm sure you can figure it out if you're using a modular config.
Scroll through the file until you find a block resembling the following:
users.users.<old_name> = {
isNormalUser = true;
description = "Dead Nameson";
extraGroups = [ "networkmanager" "wheel" ];
...
};Rewrite the first line so that it now defines <new_name> as opposed to <old_name>.
Change the description if you need/want your DE to show something different for the user's full name.
If you're running a stock config or something close to one, you're done with this step, but if you've modified yours, you might have a little more work to do.
Go through your config and make sure there's no more references to <old_name>.
For example, if you've changed your default shell, then you might have a line like this that requires changing:
users.users.<old_name>.shell = pkgs.fish;As with the previous example, just swap <old_name> for <new_name> where applicable.
If you're running home-manager, you'll also have to change home.username and home.homeDirectory in your home.nix file, or your home-manager config won't follow you to the renamed user.
Now to set the user's password.
This one's pretty simple. Just run this command as administrator and follow the prompts to set the renamed user's password. Skipping this step may result in you being unable to log into the account after rebooting.
# passwd <new_name>Told you that step was going to be simple. Last step: rebuilding our NixOS configuration and rebooting into it!
We're in the home stretch now, just one more command and we can reboot.
As always in this guide, run this with root permissions.
# nixos-rebuild bootNormally, most people run nixos-rebuild switch in order to enable the new configuration immediately,
but that could cause problems here.
boot means that it'll apply the next time you restart your computer.
Speaking of which, we're done!
Run reboot and your computer will restart, after which you should be greeted by a login screen featuring your newly renamed user.
it might have helped if I was here before doing the mistake 🤦