NixOS supports both declarative and imperative styles of user management. In the declarative style, users are specified in <filename>configuration.nix</filename>. For instance, the following states that a user account named <literal>alice</literal> shall exist:
Note that <literal>alice</literal> is a member of the <literal>wheel</literal> and <literal>networkmanager</literal> groups, which allows her to use <command>sudo</command> to execute commands as <literal>root</literal> and to configure the network, respectively. Also note the SSH public key that allows remote logins with the corresponding private key. Users created in this way do not have a password by default, so they cannot log in via mechanisms that require a password. However, you can use the <command>passwd</command> program to set a password, which is retained across invocations of <command>nixos-rebuild</command>.
If you set <xreflinkend="opt-users.mutableUsers"/> to false, then the contents of <literal>/etc/passwd</literal> and <literal>/etc/group</literal> will be congruent to your NixOS configuration. For instance, if you remove a user from <xreflinkend="opt-users.users"/> and run nixos-rebuild, the user account will cease to exist. Also, imperative commands for managing users and groups, such as useradd, are no longer available. Passwords may still be assigned by setting the user's <linklinkend="opt-users.users._name__.hashedPassword">hashedPassword</link> option. A hashed password can be generated using <command>mkpasswd -m sha-512</command> after installing the <literal>mkpasswd</literal> package.
In the imperative style, users and groups are managed by commands such as <command>useradd</command>, <command>groupmod</command> and so on. For instance, to create a user account named <literal>alice</literal>:
To make all nix tools available to this new user use `su - USER` which opens a login shell (==shell that loads the profile) for given user. This will create the ~/.nix-defexpr symlink. So run:
The flag <option>-m</option> causes the creation of a home directory for the new user, which is generally what you want. The user does not have an initial password and therefore cannot log in. A password can be set using the <command>passwd</command> utility:
The flag <option>-r</option> deletes the user’s home directory. Accounts can be modified using <command>usermod</command>. Unix groups can be managed using <command>groupadd</command>, <command>groupmod</command> and <command>groupdel</command>.