Modularise NixOS and home-manager configs
This commit is contained in:
		@@ -1,20 +1,29 @@
 | 
			
		||||
{ pkgs, ... }:
 | 
			
		||||
{
 | 
			
		||||
  # So home-manager will inject the sourcing of ~/.nix-profile/etc/profile.d/nix.sh
 | 
			
		||||
  targets.genericLinux.enable = true;
 | 
			
		||||
  home-manager.homes."dev@castle" = {
 | 
			
		||||
    system = "x86_64-linux";
 | 
			
		||||
    nixpkgs = "unstable";
 | 
			
		||||
    homeDirectory = "/home/dev";
 | 
			
		||||
    username = "dev";
 | 
			
		||||
 | 
			
		||||
  my = {
 | 
			
		||||
    ssh.matchBlocks = {
 | 
			
		||||
      home = {
 | 
			
		||||
        host =
 | 
			
		||||
          "vm keep.core fw firewall moat.vm storage cellar.vm lxd ship.vm docker whale.vm kerberos gatehouse.lxd " +
 | 
			
		||||
          "nginx.lxd upnp.lxd souterrain.lxd drawbridge.lxd mailcow.lxd";
 | 
			
		||||
        user = "root";
 | 
			
		||||
    configuration = { pkgs, ... }:
 | 
			
		||||
      {
 | 
			
		||||
        # So home-manager will inject the sourcing of ~/.nix-profile/etc/profile.d/nix.sh
 | 
			
		||||
        targets.genericLinux.enable = true;
 | 
			
		||||
 | 
			
		||||
        my = {
 | 
			
		||||
          ssh.matchBlocks = {
 | 
			
		||||
            home = {
 | 
			
		||||
              host =
 | 
			
		||||
                "vm keep.core fw firewall moat.vm storage cellar.vm lxd ship.vm docker whale.vm kerberos gatehouse.lxd " +
 | 
			
		||||
                "nginx.lxd upnp.lxd souterrain.lxd drawbridge.lxd mailcow.lxd";
 | 
			
		||||
              user = "root";
 | 
			
		||||
            };
 | 
			
		||||
          };
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        programs = {
 | 
			
		||||
          kakoune.enable = true;
 | 
			
		||||
        };
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  programs = {
 | 
			
		||||
    kakoune.enable = true;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,51 +1,69 @@
 | 
			
		||||
{ lib, hmFlakes, inputs, pkgs', modules }:
 | 
			
		||||
{ lib, hmFlakes, inputs, pkgs', config, ... }:
 | 
			
		||||
let
 | 
			
		||||
  inherit (builtins) removeAttrs mapAttrs;
 | 
			
		||||
  inherit (lib) flatten optional recursiveUpdate;
 | 
			
		||||
  inherit (lib.my) homeStateVersion;
 | 
			
		||||
  inherit (builtins) head tail mapAttrs attrValues;
 | 
			
		||||
  inherit (lib) flatten optional mkOption mkOptionType;
 | 
			
		||||
  inherit (lib.my) homeStateVersion mkOpt' commonOpts inlineModule';
 | 
			
		||||
 | 
			
		||||
  mkHome = name: {
 | 
			
		||||
    system,
 | 
			
		||||
    nixpkgs ? "unstable",
 | 
			
		||||
    home-manager ? nixpkgs,
 | 
			
		||||
    config,
 | 
			
		||||
    ...
 | 
			
		||||
  }@args:
 | 
			
		||||
  let
 | 
			
		||||
    rest = removeAttrs args [ "nixpkgs" "home-manager" "config" ];
 | 
			
		||||
  in
 | 
			
		||||
  cfg = config.home-manager;
 | 
			
		||||
 | 
			
		||||
  mkHome = {
 | 
			
		||||
    config',
 | 
			
		||||
    defs,
 | 
			
		||||
  }:
 | 
			
		||||
  # homeManagerConfiguration doesn't allow us to set lib directly (inherits from passed pkgs)
 | 
			
		||||
  hmFlakes.${home-manager}.lib.homeManagerConfiguration (recursiveUpdate rest {
 | 
			
		||||
    configuration = config;
 | 
			
		||||
  hmFlakes.${config'.home-manager}.lib.homeManagerConfiguration {
 | 
			
		||||
    inherit (config') system homeDirectory username;
 | 
			
		||||
    # Pull the first def as `configuration` and add any others to `extraModules` (they should end up in the same list
 | 
			
		||||
    # of modules to evaluate anyway)
 | 
			
		||||
    configuration = head defs;
 | 
			
		||||
    # Passing pkgs here doesn't set the global pkgs, just where it'll be imported from (and where the global lib is
 | 
			
		||||
    # derived from). We want home-manager to import pkgs itself so it'll apply config and overlays modularly. Any config
 | 
			
		||||
    # and overlays previously applied will be passed on by `homeManagerConfiguration` though.
 | 
			
		||||
    pkgs = pkgs'.${nixpkgs}.${system};
 | 
			
		||||
    extraModules = modules ++ [
 | 
			
		||||
    pkgs = pkgs'.${config'.nixpkgs}.${config'.system};
 | 
			
		||||
    extraSpecialArgs = { inherit inputs; };
 | 
			
		||||
    extraModules = (attrValues cfg.modules) ++ [
 | 
			
		||||
      {
 | 
			
		||||
        warnings = flatten [
 | 
			
		||||
          (optional (nixpkgs != home-manager)
 | 
			
		||||
          (optional (config'.nixpkgs != config'.home-manager)
 | 
			
		||||
          ''
 | 
			
		||||
            Using nixpkgs ${nixpkgs} with home-manager ${home-manager} may cause issues.
 | 
			
		||||
            Using nixpkgs ${config'.nixpkgs} with home-manager ${config'.home-manager} may cause issues.
 | 
			
		||||
          '')
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        _module.args = {
 | 
			
		||||
          inherit inputs;
 | 
			
		||||
          pkgs' = mapAttrs (_: p: p.${system}) pkgs';
 | 
			
		||||
          pkgs' = mapAttrs (_: p: p.${config'.system}) pkgs';
 | 
			
		||||
        };
 | 
			
		||||
      }
 | 
			
		||||
      (homeStateVersion home-manager)
 | 
			
		||||
    ];
 | 
			
		||||
  });
 | 
			
		||||
in
 | 
			
		||||
mapAttrs mkHome {
 | 
			
		||||
  "dev@castle" = {
 | 
			
		||||
    system = "x86_64-linux";
 | 
			
		||||
    nixpkgs = "unstable";
 | 
			
		||||
    config = configs/castle.nix;
 | 
			
		||||
      (homeStateVersion config'.home-manager)
 | 
			
		||||
    ] ++ (tail defs);
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
    homeDirectory = "/home/dev";
 | 
			
		||||
    username = "dev";
 | 
			
		||||
  homeOpts = with lib.types; { config, ... }: {
 | 
			
		||||
    options = {
 | 
			
		||||
      inherit (commonOpts) system nixpkgs home-manager;
 | 
			
		||||
      # TODO: docCustom for home-manager?
 | 
			
		||||
      homeDirectory = mkOpt' str null "Absolute path to home directory.";
 | 
			
		||||
      username = mkOpt' str null "Username for the configuration.";
 | 
			
		||||
 | 
			
		||||
      configuration = mkOption {
 | 
			
		||||
        description = "home-manager configuration module.";
 | 
			
		||||
        type = mkOptionType {
 | 
			
		||||
          name = "home-manager configuration";
 | 
			
		||||
          merge = _: defs: mkHome {
 | 
			
		||||
            config' = config;
 | 
			
		||||
            defs = map (d: inlineModule' d.file d.value) defs;
 | 
			
		||||
          };
 | 
			
		||||
        };
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
in
 | 
			
		||||
{
 | 
			
		||||
  imports = [ modules/_list.nix ];
 | 
			
		||||
  options = with lib.types; {
 | 
			
		||||
    home-manager = {
 | 
			
		||||
      modules = mkOpt' (attrsOf commonOpts.moduleType) { } "home-manager modules to be exported by nixfiles.";
 | 
			
		||||
      homes = mkOpt' (attrsOf (submodule homeOpts)) { } "home-manager configurations to be exported by nixfiles.";
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								home-manager/modules/_list.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								home-manager/modules/_list.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
  home-manager.modules = {
 | 
			
		||||
    common = ./common.nix;
 | 
			
		||||
    gui = ./gui.nix;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user