I'd like to reference a user in some nix expression. However rather than just setting it to a string value like bob
I want to reference a user that is already defined in users.extraUsers
so I assume something like users.extraUsers.bob.userName
. Is this possible?
The reason for this, is it gives me some extra static guarentee that I'm referencing a user that will exist.
You can make sure a user is defined by looking them up.
$ nix repl '<nixpkgs/nixos>'
Welcome to Nix version 2.3.4. Type :? for help.
Loading '<nixpkgs/nixos>'...
Added 6 variables.
nix-repl> config.users.users.${"root"}.name
"root"
nix-repl> user = "bob"
nix-repl> config.users.users.${user}.name
error: attribute 'bob' missing, at (string):1:1
nix-repl> config.users.users.${user}.name or builtins.throw "You referenced a user `${user}' which was not defined."
error: You referenced a user `bob' which was not defined.
Note that extraUsers
is just an alias for users
.
It's not static in the sense that Nix doesn't static checking beyond limited scope checking, but I hope it helps.