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

Remove hardcoded username from HM cli features #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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