fix: rework home-manager modules to allow option merging

This commit is contained in:
Jake Hamilton 2023-06-25 18:58:14 -07:00
parent 165d8bb203
commit cfaa78937e
No known key found for this signature in database
GPG key ID: 9762169A1B35EA68
4 changed files with 154 additions and 81 deletions

View file

@ -1,7 +1,26 @@
{ core-inputs, user-inputs, snowfall-lib }: { core-inputs, user-inputs, snowfall-lib }:
let let
inherit (core-inputs.nixpkgs.lib) assertMsg foldl head tail concatMap optionalAttrs mkIf filterAttrs mapAttrs' mkMerge mapAttrsToList optionals mkDefault mkAliasDefinitions; inherit (core-inputs.nixpkgs.lib)
assertMsg
foldl
head
tail
concatMap
optionalAttrs
optional
mkIf
filterAttrs
mapAttrs'
mkMerge
mapAttrsToList
optionals
mkDefault
traceSeqN
mkAliasDefinitions
mkAliasAndWrapDefinitions
mkOption
types;
user-homes-root = snowfall-lib.fs.get-snowfall-file "homes"; user-homes-root = snowfall-lib.fs.get-snowfall-file "homes";
user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules";
@ -136,7 +155,11 @@ in
src = "${user-modules-root}/home"; src = "${user-modules-root}/home";
}; };
user-home-modules-list = builtins.attrValues user-home-modules; user-home-modules-list = mapAttrsToList
(module-path: module: args@{ pkgs, ... }: (module args) // {
_file = "${user-homes-root}/${module-path}/default.nix";
})
user-home-modules;
create-home' = home-metadata: create-home' = home-metadata:
let let
@ -160,6 +183,30 @@ in
create-home-system-modules = users: create-home-system-modules = users:
let let
created-users = create-homes users; created-users = create-homes users;
user-home-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/home";
};
shared-modules = mapAttrsToList
(module-path: module: {
_file = "${user-modules-root}/home/${module-path}/default.nix";
config = {
home-manager.sharedModules = [ module ];
};
})
user-home-modules;
snowfall-user-home-module = {
_file = "virtual:snowfallorg/modules/home/user/default.nix";
config = {
home-manager.sharedModules = [
../../modules/home/user/default.nix
];
};
};
extra-special-args-module = extra-special-args-module =
args@{ config args@{ config
, pkgs , pkgs
@ -184,15 +231,17 @@ in
}; };
}; };
}; };
system-modules = builtins.map system-modules = builtins.map
(name: (name:
let let
created-user = created-users.${name}; created-user = created-users.${name};
user-module = head created-user.modules; user-module = head created-user.modules;
other-modules = tail created-user.modules; other-modules = users.users.${name}.modules or [ ];
user-name = created-user.specialArgs.user; user-name = created-user.specialArgs.user;
in in
args@{ config args@{ config
, options
, pkgs , pkgs
, host ? "" , host ? ""
, ... , ...
@ -200,52 +249,56 @@ in
let let
host-matches = created-user.specialArgs.host == host; host-matches = created-user.specialArgs.host == host;
# @NOTE(jakehamilton): We *must* specify named attributes here in order # @NOTE(jakehamilton): To conform to the config structure of home-manager, we have to
# for home-manager to provide them. # remap the options coming from `snowfallorg.user.<name>.home.config` since `mkAliasDefinitions`
wrapped-user-module = home-args@{ pkgs, lib, osConfig ? { }, ... }: # does not let us target options within a submodule.
let wrap-user-options = user-option:
user-module-result = import user-module home-args; if (user-option ? "_type") && user-option._type == "merge" then
user-imports = user-option // {
if user-module-result ? imports then contents = builtins.map
user-module-result.imports (merge-entry:
else merge-entry.${user-name}.home.config or { }
[ ]; )
user-config = user-option.contents;
if user-module-result ? config then }
user-module-result.config else
else (builtins.trace ''
builtins.removeAttrs user-module-result [ "imports" "options" "_file" ]; =============
user = created-user.specialArgs.user; Snowfall Lib:
in Option value for `snowfallorg.user.${user-name}` was not detected to be merged.
{
_file = builtins.toString user-module;
imports = user-imports;
config = mkMerge [ Please report the issue on GitHub with a link to your configuration so we can debug the problem:
user-config https://github.com/snowfallorg/lib/issues/new
({ =============
snowfallorg.user.name = mkDefault user; '')
}) user-option;
(osConfig.snowfallorg.resolved-homes.${user} or { })
];
};
in in
{ {
_file = "virtual:snowfallorg/home/user/${name}"; _file = "virtual:snowfallorg/home/user/${name}";
config = mkIf host-matches { config = mkIf host-matches {
# Initialize user information. # Initialize user information.
snowfallorg.user.${user-name} = { }; snowfallorg.user.${user-name}.home.config = {
snowfallorg.user.name = mkDefault user-name;
};
home-manager = { home-manager = {
users.${user-name} = wrapped-user-module; users.${user-name} = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.user;
sharedModules = other-modules;
# sharedModules = other-modules ++ optional config.snowfallorg.user.${user-name}.home.enable wrapped-user-module;
sharedModules = other-modules ++ optional config.snowfallorg.user.${user-name}.home.enable user-module;
}; };
}; };
} }
) )
(builtins.attrNames created-users); (builtins.attrNames created-users);
in in
[ extra-special-args-module ] ++ system-modules; [
extra-special-args-module
snowfall-user-home-module
]
++ (users.modules or [ ])
++ shared-modules
++ system-modules;
}; };
} }

View file

@ -1,4 +1,4 @@
{ pkgs, lib, options, config, ... }: { pkgs, lib, options, config, inputs, ... }:
let let
inherit (lib) types mkOption mkDefault foldl optionalAttrs; inherit (lib) types mkOption mkDefault foldl optionalAttrs;
@ -17,14 +17,6 @@ let
isHidden = mkDefault false; isHidden = mkDefault false;
}; };
}); });
create-resolved-home = resolved-homes: name:
let
user = cfg.user.${name};
in
resolved-homes // {
${name} = user.home.config;
};
in in
{ {
options.snowfallorg = { options.snowfallorg = {
@ -40,32 +32,51 @@ in
}; };
home = { home = {
enable = mkOption {
type = types.bool;
default = true;
};
path = mkOption { path = mkOption {
type = types.str; type = types.str;
default = "/Users/${name}"; default = "/Users/${name}";
}; };
config = mkOption { config = mkOption {
type = types.attrs; # HM-compatible options taken from:
default = { }; # https://github.com/nix-community/home-manager/blob/0ee5ab611dc1fbb5180bd7d88d2aeb7841a4d179/nixos/common.nix#L14
type = types.submoduleWith {
specialArgs = {
osConfig = config;
modulesPath = "${inputs.home-manager}/modules";
} // config.home-manager.extraSpecialArgs;
modules = [
({ lib, modulesPath, ... }: {
imports = import "${modulesPath}/modules.nix" {
inherit pkgs lib;
useNixpkgsModule = !config.home-manager.useGlobalPkgs;
};
config = {
submoduleSupport.enable = true;
submoduleSupport.externalPackageInstall = cfg.useUserPackages;
home.username = config.users.users.${name}.name;
home.homeDirectory = config.users.users.${name}.home;
nix.package = config.nix.package;
};
})
] ++ config.home-manager.sharedModules;
};
}; };
}; };
}; };
})); }));
}; };
resolved-homes = mkOption {
type = types.attrs;
default = { };
internal = true;
};
}; };
config = { config = {
users.users = (foldl (create-system-users) { } (user-names)); users.users = (foldl create-system-users { } (user-names));
snowfallorg = {
resolved-homes = (foldl (create-resolved-home) { } (user-names));
};
}; };
} }

View file

@ -9,6 +9,8 @@ let
# when being used in standalone home-manager. To remedy this, we have to refer to the arguments set directly. # when being used in standalone home-manager. To remedy this, we have to refer to the arguments set directly.
os-user-home = inputs.osConfig.users.users.${cfg.name}.home or null; os-user-home = inputs.osConfig.users.users.${cfg.name}.home or null;
has-user-name = (cfg.user.name or null) != null;
default-home-directory = default-home-directory =
if (os-user-home != null) then if (os-user-home != null) then
os-user-home os-user-home
@ -43,8 +45,8 @@ in
config = mkIf cfg.user.enable { config = mkIf cfg.user.enable {
home = { home = {
username = mkIf (cfg.user.name or null != null) (mkDefault cfg.user.name); username = mkIf has-user-name (mkDefault cfg.user.name);
homeDirectory = mkIf (cfg.user.name or null != null) (mkDefault cfg.user.home.directory); homeDirectory = mkIf has-user-name (mkDefault cfg.user.home.directory);
}; };
}; };
} }

View file

@ -1,4 +1,4 @@
{ pkgs, lib, options, config, ... }: { pkgs, lib, options, config, inputs, ... }:
let let
inherit (lib) types mkOption mkDefault foldl optionalAttrs optional; inherit (lib) types mkOption mkDefault foldl optionalAttrs optional;
@ -15,22 +15,15 @@ let
${name} = { ${name} = {
isNormalUser = mkDefault true; isNormalUser = mkDefault true;
name = mkDefault cfg.name; name = mkDefault name;
home = mkDefault user.home.path; home = mkDefault user.home.path;
group = mkDefault "users"; group = mkDefault "users";
extraGroups = (builtins.trace user.admin) optional user.admin "wheel"; extraGroups = optional user.admin "wheel";
}; };
}); });
create-resolved-home = resolved-homes: name:
let
user = cfg.user.${name};
in
resolved-homes // {
${name} = user.home.config;
};
in in
{ {
options.snowfallorg = { options.snowfallorg = {
@ -58,26 +51,40 @@ in
}; };
config = mkOption { config = mkOption {
type = types.attrs; # HM-compatible options taken from:
default = { }; # https://github.com/nix-community/home-manager/blob/0ee5ab611dc1fbb5180bd7d88d2aeb7841a4d179/nixos/common.nix#L14
type = types.submoduleWith {
specialArgs = {
osConfig = config;
modulesPath = "${inputs.home-manager}/modules";
} // config.home-manager.extraSpecialArgs;
modules = [
({ lib, modulesPath, ... }: {
imports = import "${modulesPath}/modules.nix" {
inherit pkgs lib;
useNixpkgsModule = !config.home-manager.useGlobalPkgs;
};
config = {
submoduleSupport.enable = true;
submoduleSupport.externalPackageInstall = cfg.useUserPackages;
home.username = config.users.users.${name}.name;
home.homeDirectory = config.users.users.${name}.home;
nix.package = config.nix.package;
};
})
] ++ config.home-manager.sharedModules;
};
}; };
}; };
}; };
})); }));
}; };
resolved-homes = mkOption {
type = types.attrs;
default = { };
internal = true;
};
}; };
config = { config = {
users.users = (foldl (create-system-users) { } (user-names)); users.users = (foldl (create-system-users) { } (user-names));
snowfallorg = {
resolved-homes = (foldl (create-resolved-home) { } (user-names));
};
}; };
} }