From e0294eb3e9dd1afb4639df7b3ab8be237e8e0e3c Mon Sep 17 00:00:00 2001 From: James Lounds Date: Sun, 8 Dec 2024 20:15:43 +0000 Subject: [PATCH 1/5] add support for the same username across multiple targets --- snowfall-lib/flake/default.nix | 23 +++++++++++++++++++++-- snowfall-lib/home/default.nix | 20 ++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index 65cbe30..59f736c 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -4,7 +4,7 @@ snowfall-lib, snowfall-config, }: 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 { flake = rec { ## Remove the `self` attribute from an attribute set. @@ -197,5 +197,24 @@ in rec { inherit overlays; }; 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; + }; + }) core-inputs.flake-utils-plus.lib.defaultSystems + )); + }; } diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index b1f71a1..aa692a1 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -91,6 +91,7 @@ in { system ? "x86_64-linux", }: let 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. pkgs = user-inputs.self.pkgs.${system}.${channelName} // {lib = home-lib;}; @@ -110,7 +111,8 @@ in { ++ modules; specialArgs = { - inherit name system; + inherit system; + name = unique-name; inherit (user-metadata) user host; format = "home"; @@ -161,16 +163,19 @@ in { get-target-homes-metadata = target: let homes = snowfall-lib.fs.get-directories target; existing-homes = builtins.filter (home: builtins.pathExists "${home}/default.nix") homes; - create-home-metadata = path: { - path = "${path}/default.nix"; + create-home-metadata = path: let # 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 # 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 # so we have to explicitly discard the string's path context to allow us to # use the name as a variable. 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; in @@ -301,8 +306,11 @@ in { ... }: let host-matches = - (created-user.specialArgs.host == host) - || (created-user.specialArgs.host == "" && created-user.specialArgs.system == system); + let + home-metadata = split-user-and-host name; + in + (name == "${user-name}@${host}") + || (home-metadata.user == user-name && home-metadata.host == system); # NOTE: To conform to the config structure of home-manager, we have to # remap the options coming from `snowfallorg.user..home.config` since `mkAliasDefinitions` From 15afb0a614255e039fbce6e64c20b10c7b8436d7 Mon Sep 17 00:00:00 2001 From: James Lounds Date: Sun, 8 Dec 2024 21:39:43 +0000 Subject: [PATCH 2/5] add output `packages..homeConfigurations` for *all* systems, not just `flake-utils.defaultSystems` --- snowfall-lib/flake/default.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index 59f736c..18c1b67 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -199,6 +199,11 @@ in rec { in flake-outputs // { packages = flake-outputs.packages // (builtins.listToAttrs ( + let + systems = builtins.attrValues ( + builtins.foldl' (acc: home: acc // { ${home.system} = home.system; }) {} (builtins.attrValues homes) + ); + in builtins.map (system: { name = system; value = flake-outputs.packages.${system} // { @@ -214,7 +219,7 @@ in rec { in renamedHomeConfigurations; }; - }) core-inputs.flake-utils-plus.lib.defaultSystems + }) systems )); }; } From f90f4691d7be7f58ae5751e77ddb31a11e368cab Mon Sep 17 00:00:00 2001 From: James Lounds Date: Sun, 8 Dec 2024 21:45:03 +0000 Subject: [PATCH 3/5] Revert "add output `packages..homeConfigurations` for *all* systems, not just `flake-utils.defaultSystems`" Stick with just the defaultSystems for now - that was more changes than I tnought --- snowfall-lib/flake/default.nix | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index 18c1b67..59f736c 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -199,11 +199,6 @@ in rec { in flake-outputs // { packages = flake-outputs.packages // (builtins.listToAttrs ( - let - systems = builtins.attrValues ( - builtins.foldl' (acc: home: acc // { ${home.system} = home.system; }) {} (builtins.attrValues homes) - ); - in builtins.map (system: { name = system; value = flake-outputs.packages.${system} // { @@ -219,7 +214,7 @@ in rec { in renamedHomeConfigurations; }; - }) systems + }) core-inputs.flake-utils-plus.lib.defaultSystems )); }; } From 738a7c4bf20610aefbe7134aa661a3dc69f40e30 Mon Sep 17 00:00:00 2001 From: James Lounds Date: Sun, 8 Dec 2024 22:16:08 +0000 Subject: [PATCH 4/5] make `host-matches` a little more readable --- snowfall-lib/home/default.nix | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index aa692a1..3ba33de 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -306,11 +306,8 @@ in { ... }: let host-matches = - let - home-metadata = split-user-and-host name; - in (name == "${user-name}@${host}") - || (home-metadata.user == user-name && home-metadata.host == system); + || (name == "${user-name}@${system}"); # NOTE: To conform to the config structure of home-manager, we have to # remap the options coming from `snowfallorg.user..home.config` since `mkAliasDefinitions` From 852b75689ee87c0fca238499abccb52f283c26e6 Mon Sep 17 00:00:00 2001 From: James Lounds Date: Sun, 8 Dec 2024 22:53:00 +0000 Subject: [PATCH 5/5] run `nix fmt` --- snowfall-lib/flake/default.nix | 31 +++++++++++++++++++------------ snowfall-lib/home/default.nix | 10 ++++++++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index 59f736c..262d0dd 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -197,24 +197,31 @@ in rec { inherit overlays; }; in - flake-outputs // { - packages = flake-outputs.packages // (builtins.listToAttrs ( + flake-outputs + // { + packages = + flake-outputs.packages + // (builtins.listToAttrs ( builtins.map (system: { name = system; - value = flake-outputs.packages.${system} // { - homeConfigurations = - let + 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; + renamedHomeConfigurations = + mapAttrs' ( + name: value: + if hasSuffix "@${system}" name + then nameValuePair (removeSuffix "@${system}" name) value + else nameValuePair name value + ) + homeConfigurations; in renamedHomeConfigurations; - }; - }) core-inputs.flake-utils-plus.lib.defaultSystems + }; + }) (builtins.attrNames flake-outputs.pkgs) )); }; } diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 3ba33de..ec782ed 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -91,7 +91,10 @@ in { system ? "x86_64-linux", }: let user-metadata = split-user-and-host name; - unique-name = if user-metadata.host == "" then "${user-metadata.user}@${system}" else 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. pkgs = user-inputs.self.pkgs.${system}.${channelName} // {lib = home-lib;}; @@ -172,7 +175,10 @@ in { # so we have to explicitly discard the string's path context to allow us to # use the name as a variable. system = builtins.unsafeDiscardStringContext (builtins.baseNameOf target); - name = if !(hasInfix "@" basename) then "${basename}@${system}" else basename; + name = + if !(hasInfix "@" basename) + then "${basename}@${system}" + else basename; in { path = "${path}/default.nix"; inherit name system;