fix: User nixos.nix import only on systems where the user is enabled

This commit is contained in:
Hunter Halloran
2025-12-17 10:56:34 -05:00
parent 5fe7c08830
commit b462bbebe0

View File

@@ -46,26 +46,45 @@ let
usersData = import ../users.nix { inherit pkgs; }; usersData = import ../users.nix { inherit pkgs; };
accounts = usersData.ugaif.users or { }; accounts = usersData.ugaif.users or { };
# Extract external user NixOS modules (if they exist) # Build a map of user names to their nixos module paths (if they exist)
# External user modules can optionally provide a nixos.nix file for system-level config # We'll use this to conditionally import modules based on user.enable
userNixosModules = lib.mapAttrsToList ( userNixosModulePaths = lib.filterAttrs (_: v: v != null) (
name: user: lib.mapAttrs (
if (user ? home && user.home != null) then name: user:
let if (user ? home && user.home != null) then
homePath = let
if builtins.isAttrs user.home && user.home ? outPath then user.home.outPath else user.home; homePath =
nixosModulePath = homePath + "/nixos.nix"; if builtins.isAttrs user.home && user.home ? outPath then user.home.outPath else user.home;
in nixosModulePath = homePath + "/nixos.nix";
if in
(builtins.isPath homePath || (builtins.isString homePath && lib.hasPrefix "/" homePath)) if
&& builtins.pathExists nixosModulePath (builtins.isPath homePath || (builtins.isString homePath && lib.hasPrefix "/" homePath))
then && builtins.pathExists nixosModulePath
import nixosModulePath { inherit inputs; } then
nixosModulePath
else
null
else else
{ } null
else ) accounts
{ } );
) accounts;
# Create conditional wrapper modules for each user's nixos.nix
# Each wrapper checks if the user is enabled before applying the module content
userNixosModules = lib.mapAttrsToList (
name: modulePath:
{ config, lib, pkgs, ... }@args:
let
# Import the user's nixos module - it returns a function or attrset
importedModuleFunc = import modulePath { inherit inputs; };
# If it's a function, call it with the module args; otherwise use as-is
importedModule =
if lib.isFunction importedModuleFunc then importedModuleFunc args else importedModuleFunc;
in
{
config = lib.mkIf (config.ugaif.users.${name}.enable or false) importedModule;
}
) userNixosModulePaths;
# Load the host type module # Load the host type module
typeFile = ./types + "/${hostType}.nix"; typeFile = ./types + "/${hostType}.nix";