Set default user in WSL2 NixOS distro

When running NixOs on WSL is it possible to change the default username? I tried changing it in /etc/nixos/configuration.nix and on rebuild and restart the boot failed with:

<3>WSL (3927) ERROR: CreateProcessParseCommon:748: getpwnam(nixos) failed 5

I too struggled with this. The solution I found was to build the WSL tarball myself using the instructions here:

I talked with upstream a little bit about it here:

https://github.com/nix-community/NixOS-WSL/issues/250#issuecomment-1598918396

Struggled too a bit, here some additional pointers for those brave enough to follow the low-level approach:

Personally I’ll stick using nixos user in NixOS-WSL for now, i. e. I had no systemd-tmpfiles or similar configured and thus permissions were always a mess when restarting the VM while using a different user.

In my instance (I used NixOS-WSL as well), I have the following in my /etc/wsl.conf (a file that ultimately links to a etc-wsl.conf entry in the nix store):

[user]
default=myusername

Do you have this entry?

yes I have that but when I reenter my distro with

“wsl -d NixOS”

I get:

<3>WSL (3260) ERROR: CreateProcessParseCommon:748: getpwnam(nixos) failed 5

and the distro start up with current user being root

Yup, had that same issue.

Had to revert by logging in as root with wsl -d NixOS.Dev --user root and reverting changes.

I did make a new user with useradd and adding users.user.m28k.isNormalUser = true to configuration.nix, but that new user isn’t defaulted when opening AND doesn’t have access to the windows executables when switching via sudo --login --user=m28k or login m28k as root. I need/would like that as that is how I run code file.js.
I have modified my remote-extensions plugin in vscode so plugsins such as rust-analyzer work with code and rust on my nixos distro.

It’s exhausting how many “workarounds” need to be done to have NixOS work with WSL as good as UBuntu if I can be honest. A lot of hacks later, I have powerline-go working and code forwarding. Giving up on username changing whist keeping same functionality for now…

There is a PR hopefully covering this.

2 Likes

The steps to properly set your default user were submitted in a somewhat large PR (More documentation by nzbr · Pull Request #406 · nix-community/NixOS-WSL · GitHub) back in January. However, that PR still has not been merged.

Rather than wait for that PR to possibly never get resolved, it seemed to be a good idea to put the instructions here.

Sorry for that, I just got around to writing the rest of the docs. It should be merged pretty soon, I just wanted someone besides me to read over it before i do that