Skip to content

Commit 012761f

Browse files
committed
feat: support same username across multiple targets
1 parent 3d03566 commit 012761f

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

snowfall-lib/flake/default.nix

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
snowfall-lib,
55
snowfall-config,
66
}: let
7-
inherit (core-inputs.nixpkgs.lib) assertMsg foldl filterAttrs const;
7+
inherit (core-inputs.nixpkgs.lib) assertMsg foldl filterAttrs const mapAttrs mapAttrs' hasSuffix removeSuffix nameValuePair;
88
in rec {
99
flake = rec {
1010
## Remove the `self` attribute from an attribute set.
@@ -197,5 +197,31 @@ in rec {
197197
inherit overlays;
198198
};
199199
in
200-
flake-outputs;
200+
flake-outputs
201+
// {
202+
packages =
203+
flake-outputs.packages
204+
// (builtins.listToAttrs (
205+
builtins.map (system: {
206+
name = system;
207+
value =
208+
flake-outputs.packages.${system}
209+
// {
210+
homeConfigurations = let
211+
homeNames = filterAttrs (_: home: home.system == system) homes;
212+
homeConfigurations = mapAttrs (home-name: _: flake-outputs.homeConfigurations.${home-name}) homeNames;
213+
renamedHomeConfigurations =
214+
mapAttrs' (
215+
name: value:
216+
if hasSuffix "@${system}" name
217+
then nameValuePair (removeSuffix "@${system}" name) value
218+
else nameValuePair name value
219+
)
220+
homeConfigurations;
221+
in
222+
renamedHomeConfigurations;
223+
};
224+
}) (builtins.attrNames flake-outputs.pkgs)
225+
));
226+
};
201227
}

snowfall-lib/home/default.nix

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ in {
9191
system ? "x86_64-linux",
9292
}: let
9393
user-metadata = split-user-and-host name;
94+
unique-name =
95+
if user-metadata.host == ""
96+
then "${user-metadata.user}@${system}"
97+
else name;
9498

9599
# NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here.
96100
pkgs = user-inputs.self.pkgs.${system}.${channelName} // {lib = home-lib;};
@@ -110,7 +114,8 @@ in {
110114
++ modules;
111115

112116
specialArgs = {
113-
inherit name system;
117+
inherit system;
118+
name = unique-name;
114119
inherit (user-metadata) user host;
115120

116121
format = "home";
@@ -161,16 +166,22 @@ in {
161166
get-target-homes-metadata = target: let
162167
homes = snowfall-lib.fs.get-directories target;
163168
existing-homes = builtins.filter (home: builtins.pathExists "${home}/default.nix") homes;
164-
create-home-metadata = path: {
165-
path = "${path}/default.nix";
169+
create-home-metadata = path: let
166170
# We are building flake outputs based on file contents. Nix doesn't like this
167171
# so we have to explicitly discard the string's path context to allow us to
168172
# use the name as a variable.
169-
name = builtins.unsafeDiscardStringContext (builtins.baseNameOf path);
173+
basename = builtins.unsafeDiscardStringContext (builtins.baseNameOf path);
170174
# We are building flake outputs based on file contents. Nix doesn't like this
171175
# so we have to explicitly discard the string's path context to allow us to
172176
# use the name as a variable.
173177
system = builtins.unsafeDiscardStringContext (builtins.baseNameOf target);
178+
name =
179+
if !(hasInfix "@" basename)
180+
then "${basename}@${system}"
181+
else basename;
182+
in {
183+
path = "${path}/default.nix";
184+
inherit name system;
174185
};
175186
home-configurations = builtins.map create-home-metadata existing-homes;
176187
in
@@ -302,8 +313,8 @@ in {
302313
...
303314
}: let
304315
host-matches =
305-
(created-user.specialArgs.host == host)
306-
|| (created-user.specialArgs.host == "" && created-user.specialArgs.system == system);
316+
(name == "${user-name}@${host}")
317+
|| (name == "${user-name}@${system}");
307318

308319
# NOTE: To conform to the config structure of home-manager, we have to
309320
# remap the options coming from `snowfallorg.user.<name>.home.config` since `mkAliasDefinitions`

0 commit comments

Comments
 (0)