From cedfe30aab58cb68ab49f2332cdacec3475740c6 Mon Sep 17 00:00:00 2001 From: anntnzrb Date: Wed, 8 Oct 2025 23:11:33 -0500 Subject: [PATCH] refactor: remove rec keyword across codebase Replace rec keyword with modern Nix patterns: - Use explicit let...in bindings with inherit statements - Use fix for recursive package bindings in package/default.nix - This improves maintainability and follows idiomatic Nix patterns Files modified: - snowfall-lib/flake/default.nix - snowfall-lib/fp/default.nix - snowfall-lib/home/default.nix - snowfall-lib/system/default.nix - snowfall-lib/package/default.nix --- snowfall-lib/flake/default.nix | 9 +++- snowfall-lib/fp/default.nix | 28 +++++------ snowfall-lib/home/default.nix | 11 ++++- snowfall-lib/package/default.nix | 80 ++++++++++++++++++-------------- snowfall-lib/system/default.nix | 16 ++++++- 5 files changed, 93 insertions(+), 51 deletions(-) diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index a696927..92e16bb 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -18,8 +18,8 @@ let traceVal ; in -rec { - flake = rec { +let + flake = let ## Remove the `self` attribute from an attribute set. ## Example Usage: ## ```nix @@ -43,6 +43,8 @@ rec { ## ``` #@ Attrs -> Attrs without-src = flake-inputs: builtins.removeAttrs flake-inputs [ "src" ]; + in { + inherit without-self without-src; ## Remove the `src` and `self` attributes from an attribute set. ## Example Usage: @@ -248,4 +250,7 @@ rec { }) (builtins.attrNames flake-outputs.pkgs) )); }; +in +{ + inherit flake mkFlake; } diff --git a/snowfall-lib/fp/default.nix b/snowfall-lib/fp/default.nix index ad91584..74a8f02 100644 --- a/snowfall-lib/fp/default.nix +++ b/snowfall-lib/fp/default.nix @@ -9,7 +9,7 @@ let inherit (core-inputs.nixpkgs.lib) id foldr flip; in { - fp = rec { + fp = let ## Compose two functions. ## Example Usage: ## ```nix @@ -24,18 +24,6 @@ in f: g: x: f (g x); - ## Compose many functions. - ## Example Usage: - ## ```nix - ## compose-all [ add-two add-one ] - ## ``` - ## Result: - ## ```nix - ## (x: add-two (add-one x)) - ## ``` - #@ [(x -> y)] -> a -> b - compose-all = foldr compose id; - ## Call a function with an argument. ## Example Usage: ## ```nix @@ -47,6 +35,20 @@ in ## ``` #@ (a -> b) -> a -> b call = f: x: f x; + in { + inherit compose call; + + ## Compose many functions. + ## Example Usage: + ## ```nix + ## compose-all [ add-two add-one ] + ## ``` + ## Result: + ## ```nix + ## (x: add-two (add-one x)) + ## ``` + #@ [(x -> y)] -> a -> b + compose-all = foldr compose id; ## Apply an argument to a function. ## Example Usage: diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 41617fa..a4c76d4 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -32,7 +32,7 @@ let user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; in { - home = rec { + home = let # Modules in home-manager expect `hm` to be available directly on `lib` itself. home-lib = # NOTE: This prevents an error during evaluation if the input does @@ -427,5 +427,14 @@ in ++ shared-modules ++ shared-user-modules ++ system-modules; + in { + inherit + home-lib + split-user-and-host + create-home + get-target-homes-metadata + create-homes + create-home-system-modules + ; }; } diff --git a/snowfall-lib/package/default.nix b/snowfall-lib/package/default.nix index 377d88d..86ad260 100644 --- a/snowfall-lib/package/default.nix +++ b/snowfall-lib/package/default.nix @@ -8,6 +8,7 @@ let inherit (core-inputs.flake-utils-plus.lib) filterPackages allSystems; inherit (core-inputs.nixpkgs.lib) assertMsg + fix foldl mapAttrs filterAttrs @@ -17,7 +18,7 @@ let user-packages-root = snowfall-lib.fs.get-snowfall-file "packages"; in { - package = rec { + package = let ## Create flake output packages. ## Example Usage: ## ```nix @@ -39,46 +40,57 @@ in }: let user-packages = snowfall-lib.fs.get-default-nix-files-recursive src; - create-package-metadata = - package: - let - namespaced-packages = { - ${namespace} = packages-without-aliases; - }; - extra-inputs = - pkgs - // namespaced-packages - // { - inherit channels namespace; - lib = snowfall-lib.internal.system-lib; - pkgs = pkgs // namespaced-packages; - inputs = user-inputs; - }; - in - { - name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory package); - drv = - let - pkg = callPackageWith extra-inputs package { }; - in - pkg - // { - meta = (pkg.meta or { }) // { - snowfall = { - path = package; - }; - }; - }; - }; - packages-metadata = builtins.map create-package-metadata user-packages; merge-packages = packages: metadata: packages // { ${metadata.name} = metadata.drv; }; - packages = snowfall-lib.attrs.merge-with-aliases merge-packages packages-metadata alias // overrides; + packages-without-aliases = fix ( + packages-without-aliases: + let + create-package-metadata = + package: + let + namespaced-packages = { + ${namespace} = packages-without-aliases; + }; + extra-inputs = + pkgs + // namespaced-packages + // { + inherit channels namespace; + lib = snowfall-lib.internal.system-lib; + pkgs = pkgs // namespaced-packages; + inputs = user-inputs; + }; + in + { + # We are building flake outputs based on file paths. Nix doesn't allow this + # so we have to explicitly discard the string's path context to use it as an attribute name. + name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory package); + drv = + let + pkg = callPackageWith extra-inputs package { }; + in + pkg + // { + meta = (pkg.meta or { }) // { + snowfall = { + path = package; + }; + }; + }; + }; + packages-metadata = builtins.map create-package-metadata user-packages; + in + foldl merge-packages { } packages-metadata + ); + aliased-items = mapAttrs (name: value: packages-without-aliases.${value}) alias; + packages = packages-without-aliases // aliased-items // overrides; in filterPackages pkgs.stdenv.hostPlatform.system packages; + in { + inherit create-packages; }; } diff --git a/snowfall-lib/system/default.nix b/snowfall-lib/system/default.nix index 69531bc..620b21b 100644 --- a/snowfall-lib/system/default.nix +++ b/snowfall-lib/system/default.nix @@ -22,7 +22,7 @@ let user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; in { - system = rec { + system = let ## Get the name of a system based on its file path. ## Example Usage: ## ```nix @@ -349,5 +349,19 @@ in ); in created-systems; + in { + inherit + get-inferred-system-name + is-darwin + is-linux + is-virtual + get-virtual-system-type + get-target-systems-metadata + get-system-builder + get-system-output + get-resolved-system-target + create-system + create-systems + ; }; }