Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the home-manager NixOS module with multiple users #2516

Closed
1 task done
maydayv7 opened this issue Nov 29, 2021 · 3 comments
Closed
1 task done

How to use the home-manager NixOS module with multiple users #2516

maydayv7 opened this issue Nov 29, 2021 · 3 comments
Assignees
Labels

Comments

@maydayv7
Copy link

maydayv7 commented Nov 29, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Issue description

Hi,
I'm trying to declare multiple users using home-manager as a NixOS Module (using Flakes) and a custom function that creates the users.

Excerpts from configuration -
flake.nix:

# Home Manager
inputs.home =
{
  url = "github:nix-community/home-manager?ref=release-21.11";
  inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, ... } @ inputs:
let
  # System Libraries
  inherit (inputs.nixpkgs) lib;

  # Custom Functions
  util = import ./lib { inherit system version files lib inputs pkgs; };
  inherit (util) build map;
in
{
  ## Device Configuration ##
  nixosConfigurations.Vortex = build.device
  {
    name = "Vortex";
    users =
    [
      # User V7
      {
        username = "v7";
        description = "V 7";
        groups = [ "wheel" ];
        uid = 1000;
        shell = "zsh";
      }

      # User NixOS
      {
        username = "nixos";
        description = "nix";
        groups = [ "wheel" ];
        uid = 1000;
        shell = "zsh";
      }
    ];
  };

lib/build.nix:

{ build, system, version, lib, inputs, pkgs, files, ... }:
{
  ## Device Configuration Function ##
  device = { name, timezone, locale, kernel, kernelModules, hardware, desktop, apps, user }:
  let
    # User Creation
    device_user = (map (u: build.user u) user);
  in lib.nixosSystem
  {
    inherit system;
    specialArgs = { inherit system inputs files; };

    modules =
    [
      {
        # Modulated Configuration Imports
        imports = device_modules ++ device_user ++ [ inputs.home.nixosModules.home-manager ];
      }
    ];
  };

  ## User Configuration Function ##
  user = { username, description ? "", groups ? [ ], uid ? 1000, shell, password ? "", autologin ? false }:
  {
    # User Creation
    _module.args = { inherit username; };
    users.users."${username}" =
    {
      # Profile
      name = username;
    };
  };

and an example module modules/user/home.nix which uses the home-manager option in conjunction with system configuration options (all my modules are freely mixed):

{ config, username, files, ... }:
let
  version = config.system.stateVersion;
  homeDir = config.home-manager.users."${username}".home.homeDirectory;
in rec
{
  ## Home Manager Settings ##
  config =
  {
    environment.systemPackages = [ pkgs.neofetch ];
    home-manager =
    {
      useGlobalPkgs = true;
      useUserPackages = true;
      backupFileExtension = "bak";
      users."${username}" =
      {
        programs.home-manager.enable = true;
        systemd.user.startServices = true;
        home =
        {
          username = username;
          homeDirectory = "/home/${username}";
          stateVersion = version;
        };
      };
    };
  };
}

(My full configuration is here)

Now when I try running nixos-rebuild switch, I get this error:

error: The option `home-manager.users' in `/nix/store/ifvia9687n60as6ym9nhwffs0p7sxjh9-source/lib/build.nix' is already declared in `/nix/store/ifvia9687n60as6ym9nhwffs0p7sxjh9-source/lib/build.nix'.

Can you please tell why it is showing such an error (since I have defined users separately using home-manager.users.USER instead of home-manager.users = { USER = import ./home.nix };), and also help correct my configuration if something is wrong

Maintainer CC

@rycee

System information

- system: `"x86_64-linux"`
- host os: `Linux 5.14.16-lqx1, NixOS, 21.11 (Porcupine)`
- multi-user?: `yes`
- sandbox: `yes`
- version: `nix-env (Nix) 2.4`
- channels(root): `""`
- channels(v7): `""`
- nixpkgs: `/nix/store/2qirxpxpba09i0ndh55avywirphdgkg3-source`
@maydayv7 maydayv7 added bug triage Issues or feature request that have not been triaged yet labels Nov 29, 2021
@berbiche berbiche added question and removed bug triage Issues or feature request that have not been triaged yet labels Dec 2, 2021
@berbiche
Copy link
Member

berbiche commented Dec 2, 2021

Hi,

The issue is that you are importing the Home Manager module multiple times.
You only need to import it once for every user.

Instead of importing the module in your user function, you should import the module in your system configuration.

@maydayv7
Copy link
Author

maydayv7 commented Dec 2, 2021

Okay, after importing the nixosModule in the system configuration, it seems to work, but I'm facing another error:

error: The option `home-manager.users.nixosv7.home.homeDirectory' has conflicting definition values:
       - In `/nix/store/7ih387iyca3i13q6s37kk8fciyyk3p66-source/lib/build.nix': "/var/empty"
       - In `/nix/store/7ih387iyca3i13q6s37kk8fciyyk3p66-source/modules/user/home.nix': "/home/nixosv7"

And, for some reason, it seems to be concatenating both the usernames, instead of applying the function separately

EDIT: The problem seems to be the way username is passed as an argument. But there seems to be no proper way to pass the username to the system configuration modules (I have also tried making an option user.name but if there are multiple users then it will produce a conflict). Is there any way to properly pass the usernames of all the users as arguments?

@berbiche
Copy link
Member

berbiche commented Dec 4, 2021

When using HM as a module, the username and homeDirectory are automatically set.

Otherwise, you could create an option in the Home Manager configuration that has the username since it wouldn't work in the system configuration.
From the system configuration you can then set the username using home-manager.users.${username}.my-username-option = username.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants