feat: support same username across multiple targets

This commit is contained in:
anntnzrb
2025-10-08 21:31:14 -05:00
parent 3d03566997
commit 012761f5f3
2 changed files with 45 additions and 8 deletions

View File

@@ -4,7 +4,7 @@
snowfall-lib, snowfall-lib,
snowfall-config, snowfall-config,
}: let }: let
inherit (core-inputs.nixpkgs.lib) assertMsg foldl filterAttrs const; inherit (core-inputs.nixpkgs.lib) assertMsg foldl filterAttrs const mapAttrs mapAttrs' hasSuffix removeSuffix nameValuePair;
in rec { in rec {
flake = rec { flake = rec {
## Remove the `self` attribute from an attribute set. ## Remove the `self` attribute from an attribute set.
@@ -197,5 +197,31 @@ in rec {
inherit overlays; inherit overlays;
}; };
in in
flake-outputs; flake-outputs
// {
packages =
flake-outputs.packages
// (builtins.listToAttrs (
builtins.map (system: {
name = system;
value =
flake-outputs.packages.${system}
// {
homeConfigurations = let
homeNames = filterAttrs (_: home: home.system == system) homes;
homeConfigurations = mapAttrs (home-name: _: flake-outputs.homeConfigurations.${home-name}) homeNames;
renamedHomeConfigurations =
mapAttrs' (
name: value:
if hasSuffix "@${system}" name
then nameValuePair (removeSuffix "@${system}" name) value
else nameValuePair name value
)
homeConfigurations;
in
renamedHomeConfigurations;
};
}) (builtins.attrNames flake-outputs.pkgs)
));
};
} }

View File

@@ -91,6 +91,10 @@ in {
system ? "x86_64-linux", system ? "x86_64-linux",
}: let }: let
user-metadata = split-user-and-host name; user-metadata = split-user-and-host name;
unique-name =
if user-metadata.host == ""
then "${user-metadata.user}@${system}"
else name;
# NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here. # NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here.
pkgs = user-inputs.self.pkgs.${system}.${channelName} // {lib = home-lib;}; pkgs = user-inputs.self.pkgs.${system}.${channelName} // {lib = home-lib;};
@@ -110,7 +114,8 @@ in {
++ modules; ++ modules;
specialArgs = { specialArgs = {
inherit name system; inherit system;
name = unique-name;
inherit (user-metadata) user host; inherit (user-metadata) user host;
format = "home"; format = "home";
@@ -161,16 +166,22 @@ in {
get-target-homes-metadata = target: let get-target-homes-metadata = target: let
homes = snowfall-lib.fs.get-directories target; homes = snowfall-lib.fs.get-directories target;
existing-homes = builtins.filter (home: builtins.pathExists "${home}/default.nix") homes; existing-homes = builtins.filter (home: builtins.pathExists "${home}/default.nix") homes;
create-home-metadata = path: { create-home-metadata = path: let
path = "${path}/default.nix";
# We are building flake outputs based on file contents. Nix doesn't like this # We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to # so we have to explicitly discard the string's path context to allow us to
# use the name as a variable. # use the name as a variable.
name = builtins.unsafeDiscardStringContext (builtins.baseNameOf path); basename = builtins.unsafeDiscardStringContext (builtins.baseNameOf path);
# We are building flake outputs based on file contents. Nix doesn't like this # We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to # so we have to explicitly discard the string's path context to allow us to
# use the name as a variable. # use the name as a variable.
system = builtins.unsafeDiscardStringContext (builtins.baseNameOf target); system = builtins.unsafeDiscardStringContext (builtins.baseNameOf target);
name =
if !(hasInfix "@" basename)
then "${basename}@${system}"
else basename;
in {
path = "${path}/default.nix";
inherit name system;
}; };
home-configurations = builtins.map create-home-metadata existing-homes; home-configurations = builtins.map create-home-metadata existing-homes;
in in
@@ -302,8 +313,8 @@ in {
... ...
}: let }: let
host-matches = host-matches =
(created-user.specialArgs.host == host) (name == "${user-name}@${host}")
|| (created-user.specialArgs.host == "" && created-user.specialArgs.system == system); || (name == "${user-name}@${system}");
# NOTE: To conform to the config structure of home-manager, we have to # NOTE: To conform to the config structure of home-manager, we have to
# remap the options coming from `snowfallorg.user.<name>.home.config` since `mkAliasDefinitions` # remap the options coming from `snowfallorg.user.<name>.home.config` since `mkAliasDefinitions`