Skip to content

Conversation

@trueNAHO
Copy link
Contributor

This patchset introduces the eachDefaultSystemPassThrough and eachSystemPassThrough functions after cleaning up the README.md file and the eachSystem function.

The rationale for adding these new functions is outlined in the commit message:

commit d29791e13d0b7f83bdaaa89d09c388d7058d14ab
Author: NAHO <90870942+trueNAHO@users.noreply.github.com>
Date:   2024-09-11 20:13:13 +0200

    lib: eachDefaultSystemPassThrough/eachSystemPassThrough: init

    Expose the eachDefaultSystemPassThrough and eachSystemPassThrough
    functions to handle cases where the system key should not be injected by
    eachDefaultSystem and eachSystem:

        inputs.flake-utils.lib.eachDefaultSystem (system: {
          checks./*<SYSTEM>.*/"<CHECK>" = /* ... */;
          devShells./*<SYSTEM>.*/"<DEV_SHELL>" = /* ... */;
          packages./*<SYSTEM>.*/"<PACKAGE>" = /* ... */;
        })
        // inputs.flake-utils.lib.eachDefaultSystemPassThrough (system: {
          homeConfigurations."<HOME_CONFIGURATION>" = /* ... */;
          nixosConfigurations."<NIXOS_CONFIGURATION>" = /* ... */;
        })

    These functions prevent users from re-implementing simplified
    eachDefaultSystem and eachSystem versions to avoid system key
    injections, while benefiting from current and future complex logic, like
    handling the '--impure' flag.

    This addresses flake-utils' arguably biggest issue. [1]

    [1]: https://ayats.org/blog/no-flake-utils

Feel free to propose alternative names for the eachDefaultSystemPassThrough and eachSystemPassThrough functions.


For reference, the new eachDefaultSystemPassThrough function simplifies my Home Manager setup as follows:

From e7de6c697d32f96cfdd9a734a21222099b6f204e Mon Sep 17 00:00:00 2001
From: NAHO <90870942+trueNAHO@users.noreply.github.com>
Date: Wed, 11 Sep 2024 22:59:10 +0200
Subject: [PATCH] flake: homeConfigurations: simplify using
 eachDefaultSystemPassThrough

---
 flake.lock | 11 ++++++-----
 flake.nix  | 54 +++++++++++++++++-------------------------------------
 2 files changed, 23 insertions(+), 42 deletions(-)

diff --git a/flake.lock b/flake.lock
index 29433673..659a51bd 100644
--- a/flake.lock
+++ b/flake.lock
@@ -185,15 +185,16 @@
         ]
       },
       "locked": {
-        "lastModified": 1710146030,
-        "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
-        "owner": "numtide",
+        "lastModified": 1726087187,
+        "narHash": "sha256-kTivZ/poSIsh9nRpA3i3ozwz9xTlK75zDzRU4KJ6Mqw=",
+        "owner": "trueNAHO",
         "repo": "flake-utils",
-        "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
+        "rev": "d29791e13d0b7f83bdaaa89d09c388d7058d14ab",
         "type": "github"
       },
       "original": {
-        "owner": "numtide",
+        "owner": "trueNAHO",
+        "ref": "lib-each-default-system-pass-through-each-system-pass-through-init",
         "repo": "flake-utils",
         "type": "github"
       }
diff --git a/flake.nix b/flake.nix
index b2e8129d..72513131 100644
--- a/flake.nix
+++ b/flake.nix
@@ -20,7 +20,7 @@

     flake-utils = {
       inputs.systems.follows = "systems";
-      url = "github:numtide/flake-utils";
+      url = "github:trueNAHO/flake-utils/lib-each-default-system-pass-through-each-system-pass-through-init";
     };

     git-hooks = {
@@ -905,41 +905,21 @@
         };
       }
     )
-    //
-    # The outputs.homeConfigurations attribute set is not contained in
-    # inputs.flake-utils.lib.eachDefaultSystem to prevent the following invalid
-    # system key injection:
-    #
-    #     outputs.homeConfigurations.${system}.<HOME_CONFIGURATION_NAME>
-    #
-    # Due to the lib.mkMerge function being unavailable in this scope, the
-    # outputs.homeConfigurations attribute set is expanded and merged with the
-    # inputs.flake-utils.lib.defaultSystems values.
-    #
-    # Unlike inputs.flake-utils.lib.eachDefaultSystem, this implementation
-    # merely provides the system argument.
-    {
-      homeConfigurations =
-        builtins.foldl'
-        (
-          acc: system: let
-            lib = pkgs.lib.extend (
-              final: _:
-                import ./lib {
-                  inherit inputs pkgs system;
-
-                  lib = final;
-                  pkgsHyprland = inputs.nixpkgsHyprland.legacyPackages.${system};
-                }
-            );
+    // inputs.flake-utils.lib.eachDefaultSystemPassThrough (
+      system: let
+        lib = pkgs.lib.extend (
+          final: _:
+            import ./lib {
+              inherit inputs pkgs system;

-            pkgs = inputs.nixpkgs.legacyPackages.${system};
-          in
-            lib.attrsets.unionOfDisjoint acc (
-              import ./home_configurations {inherit lib system;}
-            )
-        )
-        {}
-        inputs.flake-utils.lib.defaultSystems;
-    };
+              lib = final;
+              pkgsHyprland = inputs.nixpkgsHyprland.legacyPackages.${system};
+            }
+        );
+
+        pkgs = inputs.nixpkgs.legacyPackages.${system};
+      in {
+        homeConfigurations = import ./home_configurations {inherit lib system;};
+      }
+    );
 }
--
2.45.2

To ensure a clear and easily reversible commit history, it would be best to avoid merge and squash commits and merge each commit individually.


NAHO (7):
  readme: remove trailing whitespaces
  lib: eachSystem: reformat using 'nixfmt-rfc-style --width 80'
  lib: eachSystem: improve comments
  lib: eachSystem: inline single-use local variables
  lib: eachSystem: simplify boolean expression
  lib: lib: sort inherit statements
  lib: eachDefaultSystemPassThrough/eachSystemPassThrough: init

 README.md | 13 +++++++++-
 lib.nix   | 72 +++++++++++++++++++++++++++++++++++--------------------
 2 files changed, 58 insertions(+), 27 deletions(-)

Reformatting the eachSystem function using 'nixfmt-rfc-style --width 80'
avoids formatting arguments in the following commits.
Inline single-use local variables to minimize cognitive load while
reading the code, unless they cache computational results.
Simplify boolean expression to reduce nesting and improve performance.
@trueNAHO trueNAHO force-pushed the lib-each-default-system-pass-through-each-system-pass-through-init branch from d29791e to fc95db3 Compare September 16, 2024 13:55
@trueNAHO
Copy link
Contributor Author

Changelog

v1: fc95db3

  • Add code eachDefaultSystemPassThrough example taken from the commit message
  • Optimize hot path by assuming rare --impure usage
  • Move commit lib: lib: sort inherit statements to the beginning

v0: d29791e

@trueNAHO trueNAHO marked this pull request as draft September 16, 2024 19:17
Expose the eachDefaultSystemPassThrough and eachSystemPassThrough
functions to handle cases where the system key should not be injected by
eachDefaultSystem and eachSystem:

    inputs.flake-utils.lib.eachDefaultSystem (system: {
      checks./*<SYSTEM>.*/"<CHECK>" = /* ... */;
      devShells./*<SYSTEM>.*/"<DEV_SHELL>" = /* ... */;
      packages./*<SYSTEM>.*/"<PACKAGE>" = /* ... */;
    })
    // inputs.flake-utils.lib.eachDefaultSystemPassThrough (system: {
      homeConfigurations."<HOME_CONFIGURATION>" = /* ... */;
      nixosConfigurations."<NIXOS_CONFIGURATION>" = /* ... */;
    })

These functions prevent users from re-implementing simplified
eachDefaultSystem and eachSystem versions to avoid system key
injections, while benefiting from current and future complex logic, like
handling the '--impure' flag.

This addresses flake-utils' arguably biggest issue. [1]

[1]: https://ayats.org/blog/no-flake-utils
@trueNAHO trueNAHO force-pushed the lib-each-default-system-pass-through-each-system-pass-through-init branch from fc95db3 to fa06cc1 Compare September 16, 2024 20:56
@trueNAHO
Copy link
Contributor Author

trueNAHO commented Sep 16, 2024

Changelog

v2: fa06cc1

  • Move eachDefaultSystemPassThrough code example from eachSystemPassThrough to eachDefaultSystemPassThrough
  • Remove broken eachSystem section link

v1: fc95db3

  • Add code eachDefaultSystemPassThrough example taken from the commit message
  • Optimize hot path by assuming rare --impure usage
  • Move commit lib: lib: sort inherit statements to the beginning

v0: d29791e

@trueNAHO trueNAHO marked this pull request as ready for review September 16, 2024 20:59
@trueNAHO trueNAHO requested a review from zimbatm September 16, 2024 20:59
@zimbatm zimbatm merged commit c1dfcf0 into numtide:main Sep 17, 2024
@zimbatm
Copy link
Member

zimbatm commented Sep 17, 2024

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants