Creating a standalone flake, but need to get username

Hi there,

I am porting my hyprland setup into a standalone flake where it, in theory, could be consumed by everyone. As part of the configuration, there are certain times when I will need to use the username or the home folder path.

Is there a way to reference the current user via Nix code without using an environment variable? I could, in theory, create a mandatory option where the user sets their username, but that doesn’t feel right and would be my fallback option if I don’t figure this out.

Suggestions?

Cheers! (And thank you!)

No. Not with flakes.

You probably want to write a module option or a function and have users of your flake set that option or pass the user as an argument. If memory serves there’s also home.user or a very similarly named option, but I don’t use HM.

1 Like

That’s the correct way.
Or make it attrsOf and use the attribute name as the username.

For clarification, the username will be used in Nix or Home Manager configuration options. Not the flake itself. I am just using the flake as a consumption/distribution point. My question is more about Nix itself.

Cheers!

Thanks for the feedback!

I precisely did that in this project: ECPHP / Devs profile · GitLab

You might need to use --impure to have access to the $USER and $HOME environment variables.

See where it used in: modules/profiles/homeConfigurations.nix · main · ECPHP / Devs profile · GitLab

Enjoy!

Thanks for the link!

Is there a way to reference the current user via Nix code without using an environment variable?

But I was trying to avoid using env vars. :slight_smile:

There’s no way to pass arguments to a flake (yet?) sadly.