Skip to content

Remove hardcoded username from HM cli features#31

Open
breezeight wants to merge 1 commit intoMisterio77:mainfrom
breezeight:hm-cli-remove-hardcoded-username
Open

Remove hardcoded username from HM cli features#31
breezeight wants to merge 1 commit intoMisterio77:mainfrom
breezeight:hm-cli-remove-hardcoded-username

Conversation

@breezeight
Copy link

No description provided.

@no-mood
Copy link

no-mood commented May 5, 2024

Related to this, I just rewrote my home-manager nixos module so I don't have to hardcode hostname and usernames.

So hosts/common/optional/home-manager.nix :

{
  inputs,
  outputs,
  lib,
  config,
  ...
}: let
  cfg = config.home-manager;
in {
  imports = [
    inputs.home-manager.nixosModules.home-manager
  ];

  # Don't pass arguments, instead make an option. Source:
  # https://discourse.nixos.org/t/passing-parameters-into-import/34082/4
  options.home-manager = {
    hostname = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
    };

    usernames = lib.mkOption {
      type = lib.types.nullOr (lib.types.listOf lib.types.str);
      default = null;
    };
  };

  config = lib.mkIf ((cfg.hostname != null) && (cfg.usernames != null) && (lib.length cfg.usernames > 0)) {
    home-manager = {
      useUserPackages = lib.mkDefault true;
      extraSpecialArgs = {inherit inputs outputs;};
      
      users = builtins.listToAttrs (map (username: {
          name = username;
          value = import "${inputs.self}/home/${username}/${cfg.hostname}.nix"; # inputs.self references the flakes' root
        })
        cfg.usernames);
    };
  };
}

This requires some option to be set, like this:
hosts/foo/default.nix:

{}:{
imports = [
  ../common/optional/home-manager.nix
];
  home-manager.usernames = ["misterio user2"];
  home-manager.hostname = "foo";
}

This allows for using a single module for home-manager, without needing to hardcode the path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants