How do you organize home-manager and NixOS settings which are related?

I have for example the following config.

{ custom }: { pkgs, ... }:
{
  home-manager.users.${custom.username} = {
    home.packages = with pkgs; [
      makemkv
    ];
  };
  boot.kernelModules = [ "sg" ];
}

Ignoring my hack with custom how would one organise this code so that it lives in the same file?
Usually home-manager and NixOS configs life in separate files but what if one would like to group them together?
Can you put home-manager settings into a module and the username an option?
Would the result then be something like this?

service.makemkv = {
  enable = true;
  user = "username";
};

This does work without a problem:

{ custom }: { config, lib, pkgs, ... }:
let
  cfg = config.programs.makemkv;
in
{
  options = {
    programs.makemkv.enable = lib.mkEnableOption "MakeMKV";
  };

  config = lib.mkIf cfg.enable {
    home-manager.users.${custom.username} = {
      home.packages = with pkgs; [
        makemkv
      ];
    };
    boot.kernelModules = [ "sg" ];
  };
}
1 Like

I am interested in doing this as well.

Right now I am using a vert traditional home-manager setup with a flake.


{
  description = "NixOS Gnome configuration for Dustin Krysak";

  inputs = {

    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

    nixos-hardware.url = "github:NixOS/nixos-hardware/master";

    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    # currently used for FF extensions
    nur.url = "github:nix-community/NUR";

    # Themes
    nix-colors.url = "github:misterio77/nix-colors";

*flake.nix*
  };

  outputs = inputs@{ self, nixpkgs, home-manager, nixos-hardware, nur, nix-colors, ... }:
    let nixpkgsConfig = { overlays = [ ]; };
    in {

      nixosConfigurations = {

        # dustin-krysak = work laptop hostname
        dustin-krysak = nixpkgs.lib.nixosSystem {
          specialArgs = { inherit nix-colors; };
          system = "x86_64-linux";
          modules = [

            ./hosts/dustin-krysak
            nur.nixosModules.nur
            nix-colors.homeManagerModules.default
            nixos-hardware.nixosModules.lenovo-thinkpad-x13-yoga
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.dustin = {
                imports = [ ./home/dustin-krysak ];
              };

              # Overlays
              nixpkgs.overlays = [ nur.overlay ];

              # Allow unfree packages
              nixpkgs.config.allowUnfree = true;
            }
          ];
        };

      };
    };
}

I assume I need to modify how my imports are working,

Can someone point me to a simple repo that illustrates this method?

Thank you.

looking back at this,

Would I import this file in this same area (for example - pretending mixed-module.nix has code similar to the above):

# snippet
        rembot = nixpkgs.lib.nixosSystem {
          specialArgs = { inherit inputs; };
          system = "x86_64-linux";
          modules = [
            ./hosts/rembot
            ./hosts/mixed-module.nix

Or do I need to change the way my flake is structured?

Thank you.