From 6f2e2819df4770b7ab81827e3cb27b738c7c7721 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 4 Feb 2024 12:28:53 -0800 Subject: [PATCH 01/14] fix: default to using global pkgs --- snowfall-lib/home/default.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index e8d1844..6111a7d 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -333,6 +333,10 @@ in # sharedModules = other-modules ++ optional config.snowfallorg.user.${user-name}.home.enable wrapped-user-module; sharedModules = other-modules ++ optional config.snowfallorg.user.${user-name}.home.enable user-module; + + # NOTE: Without this home-manager will instead create its own package set which won't contain the same config and + # user-defined packages/overlays as the flake's nixpkgs channel. + useGlobalPkgs = mkDefault true; }; }; } From 40d3ef44b48939d9149e3bc9e144f8eb070345c9 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 4 Feb 2024 13:07:17 -0800 Subject: [PATCH 02/14] fix: use home-manager imports for user home configuration --- snowfall-lib/home/default.nix | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 6111a7d..04eb2ba 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -315,6 +315,8 @@ in ============= '') user-option; + + home-config = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.user; in { _file = "virtual:snowfallorg/home/user/${name}"; @@ -326,13 +328,14 @@ in enable = true; name = mkDefault user-name; }; + home.stateVersion = "22.11"; }; home-manager = { - users.${user-name} = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.user; - - # sharedModules = other-modules ++ optional config.snowfallorg.user.${user-name}.home.enable wrapped-user-module; - sharedModules = other-modules ++ optional config.snowfallorg.user.${user-name}.home.enable user-module; + users.${user-name} = mkIf config.snowfallorg.user.${user-name}.home.enable ({ pkgs, ... }: { + imports = (home-config.imports or [ ]) ++ other-modules ++ [user-module]; + config = home-config; + }); # NOTE: Without this home-manager will instead create its own package set which won't contain the same config and # user-defined packages/overlays as the flake's nixpkgs channel. @@ -347,7 +350,6 @@ in extra-special-args-module snowfall-user-home-module ] - ++ (users.modules or [ ]) ++ shared-modules ++ system-modules; }; From 723c16bbb5fbb459433271048b106221644f1303 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 4 Feb 2024 13:15:43 -0800 Subject: [PATCH 03/14] fix: remove stateversion --- snowfall-lib/home/default.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 04eb2ba..354df14 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -328,7 +328,6 @@ in enable = true; name = mkDefault user-name; }; - home.stateVersion = "22.11"; }; home-manager = { From 1284023e7a07db5adcce5dadda609635afeb8812 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Wed, 14 Feb 2024 01:29:59 -0800 Subject: [PATCH 04/14] refactor: standardize on formatter --- default.nix | 20 +- flake.nix | 139 +++++---- modules/darwin/user/default.nix | 76 +++-- modules/home/user/default.nix | 24 +- modules/nixos/user/default.nix | 82 ++--- snowfall-lib/attrs/default.nix | 52 ++-- snowfall-lib/default.nix | 95 +++--- snowfall-lib/flake/default.nix | 205 ++++++------ snowfall-lib/fp/default.nix | 16 +- snowfall-lib/fs/default.nix | 105 +++---- snowfall-lib/home/default.nix | 359 +++++++++++----------- snowfall-lib/home/nix-registry-module.nix | 8 +- snowfall-lib/internal/default.nix | 33 +- snowfall-lib/module/default.nix | 126 ++++---- snowfall-lib/overlay/default.nix | 278 ++++++++--------- snowfall-lib/package/default.nix | 103 ++++--- snowfall-lib/path/default.nix | 65 ++-- snowfall-lib/shell/default.nix | 74 +++-- snowfall-lib/system/default.nix | 324 +++++++++---------- snowfall-lib/template/default.nix | 58 ++-- 20 files changed, 1131 insertions(+), 1111 deletions(-) diff --git a/default.nix b/default.nix index 2cccff2..c7d0c26 100644 --- a/default.nix +++ b/default.nix @@ -1,10 +1,14 @@ -(import +( + import ( - let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } ) - { src = ./.; } -).defaultNix + {src = ./.;} +) +.defaultNix diff --git a/flake.nix b/flake.nix index a1ab023..03d3db4 100644 --- a/flake.nix +++ b/flake.nix @@ -11,75 +11,86 @@ }; }; - outputs = inputs: - let - core-inputs = inputs // { + outputs = inputs: let + core-inputs = + inputs + // { src = ./.; }; - # Create the library, extending the nixpkgs library and merging - # libraries from other inputs to make them available like - # `lib.flake-utils-plus.mkApp`. - # Usage: mkLib { inherit inputs; src = ./.; } - # result: lib - mkLib = import ./snowfall-lib core-inputs; + # Create the library, extending the nixpkgs library and merging + # libraries from other inputs to make them available like + # `lib.flake-utils-plus.mkApp`. + # Usage: mkLib { inherit inputs; src = ./.; } + # result: lib + mkLib = import ./snowfall-lib core-inputs; - # A convenience wrapper to create the library and then call `lib.mkFlake`. - # Usage: mkFlake { inherit inputs; src = ./.; ... } - # result: - mkFlake = flake-and-lib-options@{ inputs, src, snowfall ? { }, ... }: - let - lib = mkLib { - inherit inputs src snowfall; - }; - flake-options = builtins.removeAttrs flake-and-lib-options [ "inputs" "src" ]; - in - lib.mkFlake flake-options; + # A convenience wrapper to create the library and then call `lib.mkFlake`. + # Usage: mkFlake { inherit inputs; src = ./.; ... } + # result: + mkFlake = flake-and-lib-options @ { + inputs, + src, + snowfall ? {}, + ... + }: let + lib = mkLib { + inherit inputs src snowfall; + }; + flake-options = builtins.removeAttrs flake-and-lib-options ["inputs" "src"]; in - { - inherit mkLib mkFlake; + lib.mkFlake flake-options; + in { + inherit mkLib mkFlake; - nixosModules = { - user = ./modules/nixos/user/default.nix; - }; - - darwinModules = { - user = ./modules/darwin/user/default.nix; - }; - - homeModules = { - user = ./modules/home/user/default.nix; - }; - - _snowfall = rec { - - raw-config = config; - - config = { - root = ./.; - src = ./.; - namespace = "snowfall"; - lib-dir = "snowfall-lib"; - - meta = { - name = "snowfall-lib"; - title = "Snowfall Lib"; - }; - }; - - internal-lib = - let - lib = mkLib { - src = ./.; - - inputs = inputs // { - self = { }; - }; - }; - in - builtins.removeAttrs - lib.snowfall - [ "internal" ]; - }; + nixosModules = { + user = ./modules/nixos/user/default.nix; }; + + darwinModules = { + user = ./modules/darwin/user/default.nix; + }; + + homeModules = { + user = ./modules/home/user/default.nix; + }; + + formatter = { + x86_64-linux = inputs.nixpkgs.legacyPackages.x86_64-linux.alejandra; + aarch64-linux = inputs.nixpkgs.legacyPackages.aarch64-linux.alejandra; + x86_64-darwin = inputs.nixpkgs.legacyPackages.x86_64-darwin.alejandra; + aarch64-darwin = inputs.nixpkgs.legacyPackages.aarch64-darwin.alejandra; + }; + + _snowfall = rec { + raw-config = config; + + config = { + root = ./.; + src = ./.; + namespace = "snowfall"; + lib-dir = "snowfall-lib"; + + meta = { + name = "snowfall-lib"; + title = "Snowfall Lib"; + }; + }; + + internal-lib = let + lib = mkLib { + src = ./.; + + inputs = + inputs + // { + self = {}; + }; + }; + in + builtins.removeAttrs + lib.snowfall + ["internal"]; + }; + }; } diff --git a/modules/darwin/user/default.nix b/modules/darwin/user/default.nix index 28f0931..a9058ee 100644 --- a/modules/darwin/user/default.nix +++ b/modules/darwin/user/default.nix @@ -1,29 +1,33 @@ -{ pkgs, lib, options, config, inputs, ... }: - -let +{ + pkgs, + lib, + options, + config, + inputs, + ... +}: let inherit (lib) types mkOption mkDefault foldl optionalAttrs; cfg = config.snowfallorg; user-names = builtins.attrNames cfg.user; - create-system-users = system-users: name: - let - user = cfg.user.${name}; - in - system-users // (optionalAttrs user.create { + create-system-users = system-users: name: let + user = cfg.user.${name}; + in + system-users + // (optionalAttrs user.create { ${name} = { home = mkDefault user.home.path; isHidden = mkDefault false; }; }); -in -{ +in { options.snowfallorg = { user = mkOption { description = "User configuration."; - default = { }; - type = types.attrsOf (types.submodule ({ name, ... }: { + default = {}; + type = types.attrsOf (types.submodule ({name, ...}: { options = { create = mkOption { description = "Whether to create the user automatically."; @@ -46,28 +50,36 @@ in # HM-compatible options taken from: # https://github.com/nix-community/home-manager/blob/0ee5ab611dc1fbb5180bd7d88d2aeb7841a4d179/nixos/common.nix#L14 type = types.submoduleWith { - specialArgs = { - osConfig = config; - modulesPath = "${inputs.home-manager}/modules"; - } // config.home-manager.extraSpecialArgs; - modules = [ - ({ lib, modulesPath, ... }: { - imports = import "${modulesPath}/modules.nix" { - inherit pkgs lib; - useNixpkgsModule = !config.home-manager.useGlobalPkgs; - }; + specialArgs = + { + osConfig = config; + modulesPath = "${inputs.home-manager}/modules"; + } + // config.home-manager.extraSpecialArgs; + modules = + [ + ({ + lib, + modulesPath, + ... + }: { + imports = import "${modulesPath}/modules.nix" { + inherit pkgs lib; + useNixpkgsModule = !config.home-manager.useGlobalPkgs; + }; - config = { - submoduleSupport.enable = true; - submoduleSupport.externalPackageInstall = cfg.useUserPackages; + config = { + submoduleSupport.enable = true; + submoduleSupport.externalPackageInstall = cfg.useUserPackages; - home.username = config.users.users.${name}.name; - home.homeDirectory = config.users.users.${name}.home; + home.username = config.users.users.${name}.name; + home.homeDirectory = config.users.users.${name}.home; - nix.package = config.nix.package; - }; - }) - ] ++ config.home-manager.sharedModules; + nix.package = config.nix.package; + }; + }) + ] + ++ config.home-manager.sharedModules; }; }; }; @@ -77,6 +89,6 @@ in }; config = { - users.users = (foldl create-system-users { } (user-names)); + users.users = foldl create-system-users {} user-names; }; } diff --git a/modules/home/user/default.nix b/modules/home/user/default.nix index 7c7c93a..9ea443b 100644 --- a/modules/home/user/default.nix +++ b/modules/home/user/default.nix @@ -1,6 +1,10 @@ -inputs@{ pkgs, lib, options, config, ... }: - -let +inputs @ { + pkgs, + lib, + options, + config, + ... +}: let inherit (lib) types mkOption mkIf mkDefault; cfg = config.snowfallorg; @@ -12,14 +16,12 @@ let has-user-name = (cfg.user.name or null) != null; default-home-directory = - if (os-user-home != null) then - os-user-home - else if pkgs.stdenv.isDarwin then - "/Users/${cfg.user.name}" - else - "/home/${cfg.user.name}"; -in -{ + if (os-user-home != null) + then os-user-home + else if pkgs.stdenv.isDarwin + then "/Users/${cfg.user.name}" + else "/home/${cfg.user.name}"; +in { options.snowfallorg = { user = { enable = mkOption { diff --git a/modules/nixos/user/default.nix b/modules/nixos/user/default.nix index e8483f1..d1d195b 100644 --- a/modules/nixos/user/default.nix +++ b/modules/nixos/user/default.nix @@ -1,19 +1,23 @@ -args@{ pkgs, lib, options, config, ... }: - -let +args @ { + pkgs, + lib, + options, + config, + ... +}: let inherit (lib) types mkOption mkDefault foldl optionalAttrs optional; cfg = config.snowfallorg; - inputs = args.inputs or { }; + inputs = args.inputs or {}; user-names = builtins.attrNames cfg.user; - create-system-users = system-users: name: - let - user = cfg.user.${name}; - in - system-users // (optionalAttrs user.create { + create-system-users = system-users: name: let + user = cfg.user.${name}; + in + system-users + // (optionalAttrs user.create { ${name} = { isNormalUser = mkDefault true; @@ -25,14 +29,12 @@ let extraGroups = optional user.admin "wheel"; }; }); - -in -{ +in { options.snowfallorg = { user = mkOption { description = "User configuration."; - default = { }; - type = types.attrsOf (types.submodule ({ name, ... }: { + default = {}; + type = types.attrsOf (types.submodule ({name, ...}: { options = { create = mkOption { description = "Whether to create the user automatically."; @@ -63,29 +65,39 @@ in # @NOTE(jakehamilton): This has been adapted to support documentation generation without # having home-manager options fully declared. type = types.submoduleWith { - specialArgs = { - osConfig = config; - modulesPath = "${inputs.home-manager or "/"}/modules"; - } // (config.home-manager.extraSpecialArgs or { }); - modules = [ - ({ lib, modulesPath, ... }: - if inputs ? home-manager then { - imports = import "${modulesPath}/modules.nix" { - inherit pkgs lib; - useNixpkgsModule = !(config.home-manager.useGlobalPkgs or false); - }; + specialArgs = + { + osConfig = config; + modulesPath = "${inputs.home-manager or "/"}/modules"; + } + // (config.home-manager.extraSpecialArgs or {}); + modules = + [ + ({ + lib, + modulesPath, + ... + }: + if inputs ? home-manager + then { + imports = import "${modulesPath}/modules.nix" { + inherit pkgs lib; + useNixpkgsModule = !(config.home-manager.useGlobalPkgs or false); + }; - config = { - submoduleSupport.enable = true; - submoduleSupport.externalPackageInstall = cfg.useUserPackages; + config = { + submoduleSupport.enable = true; + submoduleSupport.externalPackageInstall = cfg.useUserPackages; - home.username = config.users.users.${name}.name; - home.homeDirectory = config.users.users.${name}.home; + home.username = config.users.users.${name}.name; + home.homeDirectory = config.users.users.${name}.home; - nix.package = config.nix.package; - }; - } else { }) - ] ++ (config.home-manager.sharedModules or [ ]); + nix.package = config.nix.package; + }; + } + else {}) + ] + ++ (config.home-manager.sharedModules or []); }; }; }; @@ -95,6 +107,6 @@ in }; config = { - users.users = (foldl (create-system-users) { } (user-names)); + users.users = foldl create-system-users {} user-names; }; } diff --git a/snowfall-lib/attrs/default.nix b/snowfall-lib/attrs/default.nix index ea5dbee..2f7e0bd 100644 --- a/snowfall-lib/attrs/default.nix +++ b/snowfall-lib/attrs/default.nix @@ -1,11 +1,11 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let - inherit (core-inputs.nixpkgs.lib) +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let + inherit + (core-inputs.nixpkgs.lib) assertMsg mapAttrsToList mapAttrs @@ -13,9 +13,9 @@ let foldl recursiveUpdate mergeAttrs - isDerivation; -in -{ + isDerivation + ; +in { attrs = { ## Map and flatten an attribute set into a list. ## Example Usage: @@ -40,7 +40,7 @@ in ## { x = 2; } ## ``` #@ [Attrs] -> Attrs - merge-deep = foldl recursiveUpdate { }; + merge-deep = foldl recursiveUpdate {}; ## Merge the root of a list of attribute sets. ## Example Usage: @@ -52,7 +52,7 @@ in ## { x = 2; } ## ``` #@ [Attrs] -> Attrs - merge-shallow = foldl mergeAttrs { }; + merge-shallow = foldl mergeAttrs {}; ## Merge shallow for packages, but allow one deeper layer of attribute sets. ## Example Usage: @@ -66,19 +66,21 @@ in #@ [Attrs] -> Attrs merge-shallow-packages = items: foldl - (result: item: - result // (mapAttrs - (name: value: - if isDerivation value then - value - else if builtins.isAttrs value then - (result.${name} or { }) // value - else - value + ( + result: item: + result + // (mapAttrs + ( + name: value: + if isDerivation value + then value + else if builtins.isAttrs value + then (result.${name} or {}) // value + else value ) item) - ) - { } - items; + ) + {} + items; }; } diff --git a/snowfall-lib/default.nix b/snowfall-lib/default.nix index 4b4b70d..f4f560d 100644 --- a/snowfall-lib/default.nix +++ b/snowfall-lib/default.nix @@ -1,23 +1,22 @@ -# @NOTE(jakehamilton): The role of this file is to bootstrap the +# NOTE: The role of this file is to bootstrap the # Snowfall library. There is some duplication shared between this # file and the library itself due to the library needing to pass through # another extended library for its own applications. -core-inputs: -user-options: - -let - raw-snowfall-config = user-options.snowfall or { }; - snowfall-config = raw-snowfall-config // { - src = user-options.src; - root = raw-snowfall-config.root or user-options.src; - namespace = raw-snowfall-config.namespace or "internal"; - meta = { - name = raw-snowfall-config.meta.name or null; - title = raw-snowfall-config.meta.title or null; +core-inputs: user-options: let + raw-snowfall-config = user-options.snowfall or {}; + snowfall-config = + raw-snowfall-config + // { + src = user-options.src; + root = raw-snowfall-config.root or user-options.src; + namespace = raw-snowfall-config.namespace or "internal"; + meta = { + name = raw-snowfall-config.meta.name or null; + title = raw-snowfall-config.meta.title or null; + }; }; - }; - user-inputs = user-options.inputs // { src = user-options.src; }; + user-inputs = user-options.inputs // {src = user-options.src;}; inherit (core-inputs.nixpkgs.lib) assertMsg fix filterAttrs mergeAttrs fold recursiveUpdate callPackageWith; @@ -25,13 +24,13 @@ let # Type: [Attrs] -> Attrs # Usage: merge-deep [{ x = 1; } { x = 2; }] # result: { x = 2; } - merge-deep = fold recursiveUpdate { }; + merge-deep = fold recursiveUpdate {}; # Merge the root of a list of attribute sets. # Type: [Attrs] -> Attrs # Usage: merge-shallow [{ x = 1; } { x = 2; }] # result: { x = 2; } - merge-shallow = fold mergeAttrs { }; + merge-shallow = fold mergeAttrs {}; # Transform an attribute set of inputs into an attribute set where # the values are the inputs' `lib` attribute. Entries without a `lib` @@ -39,47 +38,47 @@ let # Type: Attrs -> Attrs # Usage: get-lib { x = nixpkgs; y = {}; } # result: { x = nixpkgs.lib; } - get-libs = attrs: - let - # @PERF(jakehamilton): Replace filter+map with a fold. - attrs-with-libs = filterAttrs - (name: value: builtins.isAttrs (value.lib or null)) - attrs; - libs = - builtins.mapAttrs (name: input: input.lib) attrs-with-libs; - in + get-libs = attrs: let + # @PERF(jakehamilton): Replace filter+map with a fold. + attrs-with-libs = + filterAttrs + (name: value: builtins.isAttrs (value.lib or null)) + attrs; + libs = + builtins.mapAttrs (name: input: input.lib) attrs-with-libs; + in libs; # Remove the `self` attribute from an attribute set. # Type: Attrs -> Attrs # Usage: without-self { self = {}; x = true; } # result: { x = true; } - without-self = attrs: builtins.removeAttrs attrs [ "self" ]; + without-self = attrs: builtins.removeAttrs attrs ["self"]; core-inputs-libs = get-libs (without-self core-inputs); user-inputs-libs = get-libs (without-self user-inputs); - # @NOTE(jakehamilton): This root is different to accomodate the creation + # NOTE: This root is different to accomodate the creation # of a fake user-lib in order to run documentation on this flake. snowfall-lib-root = "${core-inputs.src}/snowfall-lib"; - snowfall-lib-dirs = - let - files = builtins.readDir snowfall-lib-root; - dirs = filterAttrs (name: kind: kind == "directory") files; - names = builtins.attrNames dirs; - in + snowfall-lib-dirs = let + files = builtins.readDir snowfall-lib-root; + dirs = filterAttrs (name: kind: kind == "directory") files; + names = builtins.attrNames dirs; + in names; - snowfall-lib = fix (snowfall-lib: - let + snowfall-lib = fix ( + snowfall-lib: let attrs = { inherit snowfall-lib snowfall-config core-inputs user-inputs; }; - libs = builtins.map + libs = + builtins.map (dir: import "${snowfall-lib-root}/${dir}" attrs) snowfall-lib-dirs; in - merge-deep libs + merge-deep libs ); snowfall-top-level-lib = filterAttrs (name: value: !builtins.isAttrs value) snowfall-lib; @@ -89,24 +88,25 @@ let core-inputs-libs user-inputs-libs snowfall-top-level-lib - { snowfall = snowfall-lib; } + {snowfall = snowfall-lib;} ]; user-lib-root = "${user-inputs.src}/lib"; user-lib-modules = snowfall-lib.fs.get-default-nix-files-recursive user-lib-root; - user-lib = fix (user-lib: - let + user-lib = fix ( + user-lib: let attrs = { inherit (user-options) inputs; snowfall-inputs = core-inputs; - lib = merge-shallow [ base-lib { ${snowfall-config.namespace} = user-lib; } ]; + lib = merge-shallow [base-lib {${snowfall-config.namespace} = user-lib;}]; }; - libs = builtins.map - (path: callPackageWith attrs path { }) + libs = + builtins.map + (path: callPackageWith attrs path {}) user-lib-modules; in - merge-deep libs + merge-deep libs ); lib = merge-deep [ @@ -117,6 +117,5 @@ let user-inputs-has-self = builtins.elem "self" (builtins.attrNames user-inputs); user-inputs-has-src = builtins.elem "src" (builtins.attrNames user-inputs); in -assert (assertMsg (user-inputs-has-self) "Missing attribute `self` for mkLib."); -assert (assertMsg (user-inputs-has-src) "Missing attribute `src` for mkLib."); -lib + assert (assertMsg user-inputs-has-self "Missing attribute `self` for mkLib."); + assert (assertMsg user-inputs-has-src "Missing attribute `src` for mkLib."); lib diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index afd037e..a0904ec 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -1,13 +1,11 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (core-inputs.nixpkgs.lib) assertMsg foldl filterAttrs const; -in -rec { +in rec { flake = rec { ## Remove the `self` attribute from an attribute set. ## Example Usage: @@ -19,7 +17,7 @@ rec { ## { x = true; } ## ``` #@ Attrs -> Attrs - without-self = flake-inputs: builtins.removeAttrs flake-inputs [ "self" ]; + without-self = flake-inputs: builtins.removeAttrs flake-inputs ["self"]; ## Remove the `src` attribute from an attribute set. ## Example Usage: @@ -31,7 +29,7 @@ rec { ## { x = true; } ## ``` #@ Attrs -> Attrs - without-src = flake-inputs: builtins.removeAttrs flake-inputs [ "src" ]; + without-src = flake-inputs: builtins.removeAttrs flake-inputs ["src"]; ## Remove the `src` and `self` attributes from an attribute set. ## Example Usage: @@ -57,22 +55,22 @@ rec { #@ Attrs -> Attrs without-snowfall-options = flake-options: builtins.removeAttrs - flake-options - [ - "systems" - "modules" - "overlays" - "packages" - "outputs-builder" - "outputsBuilder" - "packagesPrefix" - "hosts" - "channels-config" - "templates" - "package-namespace" - "alias" - "snowfall" - ]; + flake-options + [ + "systems" + "modules" + "overlays" + "packages" + "outputs-builder" + "outputsBuilder" + "packagesPrefix" + "hosts" + "channels-config" + "templates" + "package-namespace" + "alias" + "snowfall" + ]; ## Transform an attribute set of inputs into an attribute set where the values are the inputs' `lib` attribute. Entries without a `lib` attribute are removed. ## Example Usage: @@ -84,80 +82,80 @@ rec { ## { x = nixpkgs.lib; } ## ``` #@ Attrs -> Attrs - get-libs = attrs: - let - # @PERF(jakehamilton): Replace filter+map with a fold. - attrs-with-libs = filterAttrs - (name: value: builtins.isAttrs (value.lib or null)) - attrs; - libs = - builtins.mapAttrs (name: input: input.lib) attrs-with-libs; - in + get-libs = attrs: let + # @PERF(jakehamilton): Replace filter+map with a fold. + attrs-with-libs = + filterAttrs + (name: value: builtins.isAttrs (value.lib or null)) + attrs; + libs = + builtins.mapAttrs (name: input: input.lib) attrs-with-libs; + in libs; }; - mkFlake = full-flake-options: - let - package-namespace = full-flake-options.package-namespace or snowfall-config.namespace or "internal"; - custom-flake-options = flake.without-snowfall-options full-flake-options; - alias = full-flake-options.alias or { }; - homes = snowfall-lib.home.create-homes (full-flake-options.homes or { }); - systems = snowfall-lib.system.create-systems { - systems = (full-flake-options.systems or { }); - homes = (full-flake-options.homes or { }); + mkFlake = full-flake-options: let + package-namespace = full-flake-options.package-namespace or snowfall-config.namespace or "internal"; + custom-flake-options = flake.without-snowfall-options full-flake-options; + alias = full-flake-options.alias or {}; + homes = snowfall-lib.home.create-homes (full-flake-options.homes or {}); + systems = snowfall-lib.system.create-systems { + systems = full-flake-options.systems or {}; + homes = full-flake-options.homes or {}; + }; + hosts = snowfall-lib.attrs.merge-shallow [(full-flake-options.systems.hosts or {}) systems homes]; + templates = snowfall-lib.template.create-templates { + overrides = full-flake-options.templates or {}; + alias = alias.templates or {}; + }; + nixos-modules = snowfall-lib.module.create-modules { + src = snowfall-lib.fs.get-snowfall-file "modules/nixos"; + overrides = full-flake-options.modules.nixos or {}; + alias = alias.modules.nixos or {}; + }; + darwin-modules = snowfall-lib.module.create-modules { + src = snowfall-lib.fs.get-snowfall-file "modules/darwin"; + overrides = full-flake-options.modules.darwin or {}; + alias = alias.modules.darwin or {}; + }; + home-modules = snowfall-lib.module.create-modules { + src = snowfall-lib.fs.get-snowfall-file "modules/home"; + overrides = full-flake-options.modules.home or {}; + alias = alias.modules.home or {}; + }; + overlays = snowfall-lib.overlay.create-overlays { + inherit package-namespace; + extra-overlays = full-flake-options.extra-exported-overlays or {}; + }; + + outputs-builder = channels: let + user-outputs-builder = + full-flake-options.outputs-builder + or full-flake-options.outputsBuilder + or (const {}); + user-outputs = user-outputs-builder channels; + packages = snowfall-lib.package.create-packages { + inherit channels package-namespace; + overrides = (full-flake-options.packages or {}) // (user-outputs.packages or {}); + alias = alias.packages or {}; }; - hosts = snowfall-lib.attrs.merge-shallow [ (full-flake-options.systems.hosts or { }) systems homes ]; - templates = snowfall-lib.template.create-templates { - overrides = (full-flake-options.templates or { }); - alias = alias.templates or { }; - }; - nixos-modules = snowfall-lib.module.create-modules { - src = snowfall-lib.fs.get-snowfall-file "modules/nixos"; - overrides = (full-flake-options.modules.nixos or { }); - alias = alias.modules.nixos or { }; - }; - darwin-modules = snowfall-lib.module.create-modules { - src = snowfall-lib.fs.get-snowfall-file "modules/darwin"; - overrides = (full-flake-options.modules.darwin or { }); - alias = alias.modules.darwin or { }; - }; - home-modules = snowfall-lib.module.create-modules { - src = snowfall-lib.fs.get-snowfall-file "modules/home"; - overrides = (full-flake-options.modules.home or { }); - alias = alias.modules.home or { }; - }; - overlays = snowfall-lib.overlay.create-overlays { - inherit package-namespace; - extra-overlays = full-flake-options.extra-exported-overlays or { }; + shells = snowfall-lib.shell.create-shells { + inherit channels; + overrides = (full-flake-options.shells or {}) // (user-outputs.devShells or {}); + alias = alias.shells or {}; }; - outputs-builder = channels: - let - user-outputs-builder = - full-flake-options.outputs-builder - or full-flake-options.outputsBuilder - or (const { }); - user-outputs = user-outputs-builder channels; - packages = snowfall-lib.package.create-packages { - inherit channels package-namespace; - overrides = (full-flake-options.packages or { }) // (user-outputs.packages or { }); - alias = alias.packages or { }; - }; - shells = snowfall-lib.shell.create-shells { - inherit channels; - overrides = (full-flake-options.shells or { }) // (user-outputs.devShells or { }); - alias = alias.shells or { }; - }; + outputs = { + inherit packages; - outputs = { - inherit packages; + devShells = shells; + }; + in + snowfall-lib.attrs.merge-deep [user-outputs outputs]; - devShells = shells; - }; - in - snowfall-lib.attrs.merge-deep [ user-outputs outputs ]; - - flake-options = custom-flake-options // { + flake-options = + custom-flake-options + // { inherit hosts templates; inherit (user-inputs) self; @@ -168,29 +166,30 @@ rec { darwinModules = darwin-modules; homeModules = home-modules; - channelsConfig = full-flake-options.channels-config or { }; + channelsConfig = full-flake-options.channels-config or {}; channels.nixpkgs.overlaysBuilder = snowfall-lib.overlay.create-overlays-builder { inherit package-namespace; - extra-overlays = full-flake-options.overlays or [ ]; + extra-overlays = full-flake-options.overlays or []; }; outputsBuilder = outputs-builder; _snowfall = { config = snowfall-config; - raw-config = full-flake-options.snowfall or { }; + raw-config = full-flake-options.snowfall or {}; user-lib = snowfall-lib.internal.user-lib; }; }; - flake-utils-plus-outputs = - core-inputs.flake-utils-plus.lib.mkFlake flake-options; + flake-utils-plus-outputs = + core-inputs.flake-utils-plus.lib.mkFlake flake-options; - flake-outputs = - flake-utils-plus-outputs // { - inherit overlays; - }; - in + flake-outputs = + flake-utils-plus-outputs + // { + inherit overlays; + }; + in flake-outputs; } diff --git a/snowfall-lib/fp/default.nix b/snowfall-lib/fp/default.nix index 04dd03c..1f9fdd9 100644 --- a/snowfall-lib/fp/default.nix +++ b/snowfall-lib/fp/default.nix @@ -1,14 +1,12 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (builtins) baseNameOf dirOf; inherit (core-inputs.nixpkgs.lib) id foldr flip; -in -{ +in { fp = rec { ## Compose two functions. ## Example Usage: diff --git a/snowfall-lib/fs/default.nix b/snowfall-lib/fs/default.nix index 3877408..1a6e060 100644 --- a/snowfall-lib/fs/default.nix +++ b/snowfall-lib/fs/default.nix @@ -1,17 +1,15 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (builtins) readDir pathExists; inherit (core-inputs) flake-utils-plus; inherit (core-inputs.nixpkgs.lib) assertMsg filterAttrs mapAttrsToList flatten; file-name-regex = "(.*)\\.(.*)$"; -in -{ +in { fs = rec { ## Matchers for file kinds. These are often used with `readDir`. ## Example Usage: @@ -75,10 +73,9 @@ in ## ``` #@ Path -> Attrs safe-read-directory = path: - if pathExists path then - readDir path - else - { }; + if pathExists path + then readDir path + else {}; ## Get directories at a given path. ## Example Usage: @@ -90,11 +87,10 @@ in ## [ "./something/a-directory" ] ## ``` #@ Path -> [Path] - get-directories = path: - let - entries = safe-read-directory path; - filtered-entries = filterAttrs (name: kind: is-directory-kind kind) entries; - in + get-directories = path: let + entries = safe-read-directory path; + filtered-entries = filterAttrs (name: kind: is-directory-kind kind) entries; + in mapAttrsToList (name: kind: "${path}/${name}") filtered-entries; ## Get files at a given path. @@ -107,11 +103,10 @@ in ## [ "./something/a-file" ] ## ``` #@ Path -> [Path] - get-files = path: - let - entries = safe-read-directory path; - filtered-entries = filterAttrs (name: kind: is-file-kind kind) entries; - in + get-files = path: let + entries = safe-read-directory path; + filtered-entries = filterAttrs (name: kind: is-file-kind kind) entries; + in mapAttrsToList (name: kind: "${path}/${name}") filtered-entries; ## Get files at a given path, traversing any directories within. @@ -124,25 +119,23 @@ in ## [ "./something/some-directory/a-file" ] ## ``` #@ Path -> [Path] - get-files-recursive = path: - let - entries = safe-read-directory path; - filtered-entries = - filterAttrs - (name: kind: (is-file-kind kind) || (is-directory-kind kind)) - entries; - map-file = name: kind: - let - path' = "${path}/${name}"; - in - if is-directory-kind kind then - get-files-recursive path' - else - path'; - files = snowfall-lib.attrs.map-concat-attrs-to-list - map-file - filtered-entries; + get-files-recursive = path: let + entries = safe-read-directory path; + filtered-entries = + filterAttrs + (name: kind: (is-file-kind kind) || (is-directory-kind kind)) + entries; + map-file = name: kind: let + path' = "${path}/${name}"; in + if is-directory-kind kind + then get-files-recursive path' + else path'; + files = + snowfall-lib.attrs.map-concat-attrs-to-list + map-file + filtered-entries; + in files; ## Get nix files at a given path. @@ -157,8 +150,8 @@ in #@ Path -> [Path] get-nix-files = path: builtins.filter - (snowfall-lib.path.has-file-extension "nix") - (get-files path); + (snowfall-lib.path.has-file-extension "nix") + (get-files path); ## Get nix files at a given path, traversing any directories within. ## Example Usage: @@ -172,8 +165,8 @@ in #@ Path -> [Path] get-nix-files-recursive = path: builtins.filter - (snowfall-lib.path.has-file-extension "nix") - (get-files-recursive path); + (snowfall-lib.path.has-file-extension "nix") + (get-files-recursive path); ## Get nix files at a given path named "default.nix". ## Example Usage: @@ -187,8 +180,8 @@ in #@ Path -> [Path] get-default-nix-files = path: builtins.filter - (name: builtins.baseNameOf name == "default.nix") - (get-files path); + (name: builtins.baseNameOf name == "default.nix") + (get-files path); ## Get nix files at a given path named "default.nix", traversing any directories within. ## Example Usage: @@ -202,8 +195,8 @@ in #@ Path -> [Path] get-default-nix-files-recursive = path: builtins.filter - (name: builtins.baseNameOf name == "default.nix") - (get-files-recursive path); + (name: builtins.baseNameOf name == "default.nix") + (get-files-recursive path); ## Get nix files at a given path not named "default.nix". ## Example Usage: @@ -217,11 +210,12 @@ in #@ Path -> [Path] get-non-default-nix-files = path: builtins.filter - (name: + ( + name: (snowfall-lib.path.has-file-extension "nix" name) && (builtins.baseNameOf name != "default.nix") - ) - (get-files path); + ) + (get-files path); ## Get nix files at a given path not named "default.nix", traversing any directories within. ## Example Usage: @@ -235,10 +229,11 @@ in #@ Path -> [Path] get-non-default-nix-files-recursive = path: builtins.filter - (name: + ( + name: (snowfall-lib.path.has-file-extension "nix" name) && (builtins.baseNameOf name != "default.nix") - ) - (get-files-recursive path); + ) + (get-files-recursive path); }; } diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 354df14..f928063 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -1,11 +1,11 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let - inherit (core-inputs.nixpkgs.lib) +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let + inherit + (core-inputs.nixpkgs.lib) assertMsg foldl head @@ -23,29 +23,29 @@ let mkAliasDefinitions mkAliasAndWrapDefinitions mkOption - types; + types + ; user-homes-root = snowfall-lib.fs.get-snowfall-file "homes"; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; -in -{ +in { home = rec { # Modules in home-manager expect `hm` to be available directly on `lib` itself. home-lib = # @NOTE(jakehamilton): This prevents an error during evaluation if the input does # not exist. - if user-inputs ? home-manager then + if user-inputs ? home-manager + then snowfall-lib.internal.system-lib.extend - (final: prev: - # @NOTE(jakehamilton): This order is important, this library's extend and other utilities must write - # _over_ the original `system-lib`. + (final: prev: + # @NOTE(jakehamilton): This order is important, this library's extend and other utilities must write + # _over_ the original `system-lib`. snowfall-lib.internal.system-lib // prev // { hm = snowfall-lib.internal.system-lib.home-manager.hm; }) - else - { }; + else {}; ## Get the user and host from a combined string. ## Example Usage: @@ -57,22 +57,18 @@ in ## { user = "myuser"; host = "myhost"; } ## ``` #@ String -> Attrs - split-user-and-host = target: - let - raw-name-parts = builtins.split "@" target; - name-parts = builtins.filter builtins.isString raw-name-parts; - - user = builtins.elemAt name-parts 0; - host = - if builtins.length name-parts > 1 then - builtins.elemAt name-parts 1 - else - ""; - in - { - inherit user host; - }; + split-user-and-host = target: let + raw-name-parts = builtins.split "@" target; + name-parts = builtins.filter builtins.isString raw-name-parts; + user = builtins.elemAt name-parts 0; + host = + if builtins.length name-parts > 1 + then builtins.elemAt name-parts 1 + else ""; + in { + inherit user host; + }; ## Create a home. ## Example Usage: @@ -84,32 +80,32 @@ in ## ## ``` #@ Attrs -> Attrs - create-home = - { path - , name ? builtins.unsafeDiscardStringContext (snowfall-lib.system.get-inferred-system-name path) - , modules ? [ ] - , specialArgs ? { } - , channelName ? "nixpkgs" - , system ? "x86_64-linux" - }: - let - user-metadata = split-user-and-host name; + create-home = { + path, + name ? builtins.unsafeDiscardStringContext (snowfall-lib.system.get-inferred-system-name path), + modules ? [], + specialArgs ? {}, + channelName ? "nixpkgs", + system ? "x86_64-linux", + }: let + user-metadata = split-user-and-host name; - # @NOTE(jakehamilton): home-manager has trouble with `pkgs` recursion if it isn't passed in here. - pkgs = user-inputs.self.pkgs.${system}.${channelName} // { lib = home-lib; }; - lib = home-lib; - in + # @NOTE(jakehamilton): home-manager has trouble with `pkgs` recursion if it isn't passed in here. + pkgs = user-inputs.self.pkgs.${system}.${channelName} // {lib = home-lib;}; + lib = home-lib; + in assert assertMsg (user-inputs ? home-manager) "In order to create home-manager configurations, you must include `home-manager` as a flake input."; - assert assertMsg (user-metadata.host != "") "Snowfall Lib homes must be named with the format: user@system"; - { + assert assertMsg (user-metadata.host != "") "Snowfall Lib homes must be named with the format: user@system"; { inherit channelName system; output = "homeConfigurations"; - modules = [ - path - ../../modules/home/user/default.nix - ] ++ modules; + modules = + [ + path + ../../modules/home/user/default.nix + ] + ++ modules; specialArgs = { inherit name; @@ -125,20 +121,25 @@ in builder = args: user-inputs.home-manager.lib.homeManagerConfiguration - ((builtins.removeAttrs args [ "system" "specialArgs" ]) // { + ((builtins.removeAttrs args ["system" "specialArgs"]) + // { inherit pkgs lib; - modules = args.modules ++ [ - (module-args: import ./nix-registry-module.nix (module-args // { - inherit user-inputs core-inputs; - })) - ({ - snowfallorg.user = { - name = mkDefault user-metadata.user; - enable = mkDefault true; - }; - }) - ]; + modules = + args.modules + ++ [ + (module-args: + import ./nix-registry-module.nix (module-args + // { + inherit user-inputs core-inputs; + })) + { + snowfallorg.user = { + name = mkDefault user-metadata.user; + enable = mkDefault true; + }; + } + ]; extraSpecialArgs = specialArgs // args.specialArgs; }); @@ -154,23 +155,22 @@ in ## [ { system = "x86_64-linux"; name = "my-home"; path = "/homes/x86_64-linux/my-home";} ] ## ``` #@ String -> [Attrs] - 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"; - # 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); - # 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); - }; - home-configurations = builtins.map create-home-metadata existing-homes; - 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"; + # 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); + # 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); + }; + home-configurations = builtins.map create-home-metadata existing-homes; + in home-configurations; ## Create all available homes. @@ -183,34 +183,36 @@ in ## { "my-user@my-system" = ; } ## ``` #@ Attrs -> Attrs - create-homes = homes: - let - targets = snowfall-lib.fs.get-directories user-homes-root; - target-homes-metadata = concatMap get-target-homes-metadata targets; + create-homes = homes: let + targets = snowfall-lib.fs.get-directories user-homes-root; + target-homes-metadata = concatMap get-target-homes-metadata targets; - user-home-modules = snowfall-lib.module.create-modules { - src = "${user-modules-root}/home"; - }; + user-home-modules = snowfall-lib.module.create-modules { + src = "${user-modules-root}/home"; + }; - user-home-modules-list = mapAttrsToList - (module-path: module: args@{ pkgs, ... }: (module args) // { + user-home-modules-list = + mapAttrsToList + (module-path: module: args @ {pkgs, ...}: + (module args) + // { _file = "${user-homes-root}/${module-path}/default.nix"; }) - user-home-modules; + user-home-modules; - create-home' = home-metadata: - let - inherit (home-metadata) name; - overrides = homes.users.${name} or { }; - in - { - "${name}" = create-home (overrides // home-metadata // { - modules = user-home-modules-list ++ (homes.users.${name}.modules or [ ]) ++ (homes.modules or [ ]); - }); - }; + create-home' = home-metadata: let + inherit (home-metadata) name; + overrides = homes.users.${name} or {}; + in { + "${name}" = create-home (overrides + // home-metadata + // { + modules = user-home-modules-list ++ (homes.users.${name}.modules or []) ++ (homes.modules or []); + }); + }; - created-homes = foldl (homes: home-metadata: homes // (create-home' home-metadata)) { } target-homes-metadata; - in + created-homes = foldl (homes: home-metadata: homes // (create-home' home-metadata)) {} target-homes-metadata; + in created-homes; ## Create system modules for home-manager integration. @@ -223,84 +225,88 @@ in ## [Module] ## ``` #@ Attrs -> [Module] - create-home-system-modules = users: - let - created-users = create-homes users; - user-home-modules = snowfall-lib.module.create-modules { - src = "${user-modules-root}/home"; - }; + create-home-system-modules = users: let + created-users = create-homes users; + user-home-modules = snowfall-lib.module.create-modules { + src = "${user-modules-root}/home"; + }; - shared-modules = mapAttrsToList - (module-path: module: { - _file = "${user-modules-root}/home/${module-path}/default.nix"; - - config = { - home-manager.sharedModules = [ module ]; - }; - }) - user-home-modules; - - snowfall-user-home-module = { - _file = "virtual:snowfallorg/modules/home/user/default.nix"; + shared-modules = + mapAttrsToList + (module-path: module: { + _file = "${user-modules-root}/home/${module-path}/default.nix"; config = { - home-manager.sharedModules = [ - ../../modules/home/user/default.nix - ]; + home-manager.sharedModules = [module]; + }; + }) + user-home-modules; + + snowfall-user-home-module = { + _file = "virtual:snowfallorg/modules/home/user/default.nix"; + + config = { + home-manager.sharedModules = [ + ../../modules/home/user/default.nix + ]; + }; + }; + + extra-special-args-module = args @ { + config, + pkgs, + system ? pkgs.system, + target ? system, + format ? "home", + host ? "", + virtual ? (snowfall-lib.system.is-virtual target), + systems ? {}, + ... + }: { + _file = "virtual:snowfallorg/home/extra-special-args"; + + config = { + home-manager.extraSpecialArgs = { + inherit system target format virtual systems host; + + lib = home-lib; + + inputs = snowfall-lib.flake.without-src user-inputs; }; }; + }; - extra-special-args-module = - args@{ config - , pkgs - , system ? pkgs.system - , target ? system - , format ? "home" - , host ? "" - , virtual ? (snowfall-lib.system.is-virtual target) - , systems ? { } - , ... - }: - { - _file = "virtual:snowfallorg/home/extra-special-args"; - - config = { - home-manager.extraSpecialArgs = { - inherit system target format virtual systems host; - - lib = home-lib; - - inputs = snowfall-lib.flake.without-src user-inputs; - }; - }; - }; - - system-modules = builtins.map - (name: - let - created-user = created-users.${name}; - user-module = head created-user.modules; - other-modules = users.users.${name}.modules or [ ]; - user-name = created-user.specialArgs.user; - in - args@{ config - , options - , pkgs - , host ? "" - , ... - }: - let + system-modules = + builtins.map + ( + name: let + created-user = created-users.${name}; + user-module = head created-user.modules; + other-modules = users.users.${name}.modules or []; + user-name = created-user.specialArgs.user; + in + args @ { + config, + options, + pkgs, + host ? "", + ... + }: let host-matches = created-user.specialArgs.host == host; # @NOTE(jakehamilton): To conform to the config structure of home-manager, we have to # remap the options coming from `snowfallorg.user..home.config` since `mkAliasDefinitions` # does not let us target options within a submodule. wrap-user-options = user-option: - if (user-option ? "_type") && user-option._type == "merge" then - user-option // { - contents = builtins.map - (merge-entry: - merge-entry.${user-name}.home.config or { } + if (user-option ? "_type") && user-option._type == "merge" + then + user-option + // { + contents = + builtins.map + ( + merge-entry: + merge-entry.${user-name}.home.config or {} ) user-option.contents; } @@ -314,11 +320,10 @@ in https://github.com/snowfallorg/lib/issues/new ============= '') - user-option; + user-option; home-config = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.user; - in - { + in { _file = "virtual:snowfallorg/home/user/${name}"; config = mkIf host-matches { @@ -331,8 +336,8 @@ in }; home-manager = { - users.${user-name} = mkIf config.snowfallorg.user.${user-name}.home.enable ({ pkgs, ... }: { - imports = (home-config.imports or [ ]) ++ other-modules ++ [user-module]; + users.${user-name} = mkIf config.snowfallorg.user.${user-name}.home.enable ({pkgs, ...}: { + imports = (home-config.imports or []) ++ other-modules ++ [user-module]; config = home-config; }); @@ -342,9 +347,9 @@ in }; }; } - ) - (builtins.attrNames created-users); - in + ) + (builtins.attrNames created-users); + in [ extra-special-args-module snowfall-user-home-module diff --git a/snowfall-lib/home/nix-registry-module.nix b/snowfall-lib/home/nix-registry-module.nix index c15f9e2..200da73 100644 --- a/snowfall-lib/home/nix-registry-module.nix +++ b/snowfall-lib/home/nix-registry-module.nix @@ -1,8 +1,12 @@ # This code is adapted from flake-utils-plus: # https://github.com/gytis-ivaskevicius/flake-utils-plus/blob/2bf0f91643c2e5ae38c1b26893ac2927ac9bd82a/lib/options.nix -{ lib, config, user-inputs, core-inputs, ... }: - { + lib, + config, + user-inputs, + core-inputs, + ... +}: { disabledModules = [ # The module from flake-utils-plus only works on NixOS and nix-darwin. For home-manager # to build, this module needs to be disabled. diff --git a/snowfall-lib/internal/default.nix b/snowfall-lib/internal/default.nix index e61eb73..fb01412 100644 --- a/snowfall-lib/internal/default.nix +++ b/snowfall-lib/internal/default.nix @@ -1,10 +1,9 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (core-inputs.nixpkgs.lib) assertMsg fix fold filterAttrs callPackageWith; core-inputs-libs = snowfall-lib.flake.get-libs (snowfall-lib.flake.without-self core-inputs); @@ -17,35 +16,35 @@ let core-inputs-libs user-inputs-libs snowfall-top-level-lib - { snowfall = snowfall-lib; } + {snowfall = snowfall-lib;} ]; user-lib-root = snowfall-lib.fs.get-file "lib"; user-lib-modules = snowfall-lib.fs.get-default-nix-files-recursive user-lib-root; - user-lib = fix (user-lib: - let + user-lib = fix ( + user-lib: let attrs = { inputs = snowfall-lib.flake.without-snowfall-inputs user-inputs; snowfall-inputs = core-inputs; lib = snowfall-lib.attrs.merge-shallow [ base-lib - { internal = user-lib; } + {internal = user-lib;} ]; }; - libs = builtins.map - (path: callPackageWith attrs path { }) + libs = + builtins.map + (path: callPackageWith attrs path {}) user-lib-modules; in - snowfall-lib.attrs.merge-deep libs + snowfall-lib.attrs.merge-deep libs ); system-lib = snowfall-lib.attrs.merge-shallow [ base-lib - { "${snowfall-config.namespace}" = user-lib; } + {"${snowfall-config.namespace}" = user-lib;} ]; -in -{ +in { internal = { inherit system-lib user-lib; }; diff --git a/snowfall-lib/module/default.nix b/snowfall-lib/module/default.nix index 7b12e45..8fe7601 100644 --- a/snowfall-lib/module/default.nix +++ b/snowfall-lib/module/default.nix @@ -1,16 +1,14 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (builtins) baseNameOf; inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs hasPrefix; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; -in -{ +in { module = { ## Create flake output modules. ## Example Usage: @@ -22,65 +20,61 @@ in ## { another-module = ...; my-module = ...; default = ...; } ## ``` #@ Attrs -> Attrs - create-modules = - { src ? "${user-modules-root}/nixos" - , overrides ? { } - , alias ? { } - }: - let - user-modules = snowfall-lib.fs.get-default-nix-files-recursive src; - create-module-metadata = module: { - name = - let - path-name = builtins.replaceStrings [ src "/default.nix" ] [ "" "" ] (builtins.unsafeDiscardStringContext module); + create-modules = { + src ? "${user-modules-root}/nixos", + overrides ? {}, + alias ? {}, + }: let + user-modules = snowfall-lib.fs.get-default-nix-files-recursive src; + create-module-metadata = module: { + name = let + path-name = builtins.replaceStrings [src "/default.nix"] ["" ""] (builtins.unsafeDiscardStringContext module); + in + if hasPrefix "/" path-name + then builtins.substring 1 ((builtins.stringLength path-name) - 1) path-name + else path-name; + path = module; + }; + modules-metadata = builtins.map create-module-metadata user-modules; + merge-modules = modules: metadata: + modules + // { + # NOTE: home-manager *requires* modules to specify named arguments or it will not + # pass values in. For this reason we must specify things like `pkgs` as a named attribute. + ${metadata.name} = args @ {pkgs, ...}: let + system = args.system or args.pkgs.system; + target = args.target or system; + + format = let + virtual-system-type = snowfall-lib.system.get-virtual-system-type target; in - if hasPrefix "/" path-name then - builtins.substring 1 ((builtins.stringLength path-name) - 1) path-name - else - path-name; - path = module; + if virtual-system-type != "" + then virtual-system-type + else if snowfall-lib.system.is-darwin target + then "darwin" + else "linux"; + + # Replicates the specialArgs from Snowfall Lib's system builder. + modified-args = + args + // { + inherit system target format; + virtual = args.virtual or (snowfall-lib.system.get-virtual-system-type target != ""); + systems = args.systems or {}; + + lib = snowfall-lib.internal.system-lib; + pkgs = user-inputs.self.pkgs.${system}.nixpkgs; + + inputs = snowfall-lib.flake.without-src user-inputs; + }; + user-module = import metadata.path modified-args; + in + user-module // {_file = metadata.path;}; }; - modules-metadata = builtins.map create-module-metadata user-modules; - merge-modules = modules: metadata: - modules // { - # @NOTE(jakehamilton): home-manager *requires* modules to specify named arguments or it will not - # pass values in. For this reason we must specify things like `pkgs` as a named attribute. - ${metadata.name} = args@{ pkgs, ... }: - let - system = args.system or args.pkgs.system; - target = args.target or system; - - format = - let - virtual-system-type = snowfall-lib.system.get-virtual-system-type target; - in - if virtual-system-type != "" then - virtual-system-type - else if snowfall-lib.system.is-darwin target then - "darwin" - else - "linux"; - - # Replicates the specialArgs from Snowfall Lib's system builder. - modified-args = args // { - inherit system target format; - virtual = args.virtual or (snowfall-lib.system.get-virtual-system-type target != ""); - systems = args.systems or { }; - - - lib = snowfall-lib.internal.system-lib; - pkgs = user-inputs.self.pkgs.${system}.nixpkgs; - - inputs = snowfall-lib.flake.without-src user-inputs; - }; - user-module = import metadata.path modified-args; - in - user-module // { _file = metadata.path; }; - }; - modules-without-aliases = foldl merge-modules { } modules-metadata; - aliased-modules = mapAttrs (name: value: modules-without-aliases.${value}) alias; - modules = modules-without-aliases // aliased-modules // overrides; - in + modules-without-aliases = foldl merge-modules {} modules-metadata; + aliased-modules = mapAttrs (name: value: modules-without-aliases.${value}) alias; + modules = modules-without-aliases // aliased-modules // overrides; + in modules; }; } diff --git a/snowfall-lib/overlay/default.nix b/snowfall-lib/overlay/default.nix index f1fd589..5dd702c 100644 --- a/snowfall-lib/overlay/default.nix +++ b/snowfall-lib/overlay/default.nix @@ -1,16 +1,14 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (core-inputs.nixpkgs.lib) assertMsg foldl concatStringsSep; user-overlays-root = snowfall-lib.fs.get-snowfall-file "overlays"; user-packages-root = snowfall-lib.fs.get-snowfall-file "packages"; -in -{ +in { overlay = { ## Create a flake-utils-plus overlays builder. ## Example Usage: @@ -22,29 +20,26 @@ in ## (channels: [ ... ]) ## ``` #@ Attrs -> Attrs -> [(a -> b -> c)] - create-overlays-builder = - { src ? user-overlays-root - , package-namespace ? "internal" - , extra-overlays ? [ ] - }: channels: - let - user-overlays = snowfall-lib.fs.get-default-nix-files-recursive src; - create-overlay = overlay: import overlay (user-inputs // { inherit channels; }); - user-packages-overlay = final: prev: - let - user-packages = snowfall-lib.package.create-packages { - pkgs = final; - channels = channels; - }; - in - { - ${package-namespace} = - (prev.${package-namespace} or { }) - // user-packages; - }; - overlays = - [ user-packages-overlay ] ++ extra-overlays ++ (builtins.map create-overlay user-overlays); - in + create-overlays-builder = { + src ? user-overlays-root, + package-namespace ? "internal", + extra-overlays ? [], + }: channels: let + user-overlays = snowfall-lib.fs.get-default-nix-files-recursive src; + create-overlay = overlay: import overlay (user-inputs // {inherit channels;}); + user-packages-overlay = final: prev: let + user-packages = snowfall-lib.package.create-packages { + pkgs = final; + channels = channels; + }; + in { + ${package-namespace} = + (prev.${package-namespace} or {}) + // user-packages; + }; + overlays = + [user-packages-overlay] ++ extra-overlays ++ (builtins.map create-overlay user-overlays); + in overlays; ## Create exported overlays from the user flake. Adapted [from flake-utils-plus](https://github.com/gytis-ivaskevicius/flake-utils-plus/blob/2bf0f91643c2e5ae38c1b26893ac2927ac9bd82a/lib/exportOverlays.nix). @@ -58,133 +53,126 @@ in ## { default = final: prev: ...; some-overlay = final: prev: ...; } ## ``` #@ Attrs -> Attrs - create-overlays = - { src ? user-overlays-root - , packages-src ? user-packages-root - , package-namespace ? null - , extra-overlays ? { } - }: - let - fake-pkgs = { - callPackage = x: x; - isFakePkgs = true; - lib = { }; - system = "fake-system"; + create-overlays = { + src ? user-overlays-root, + packages-src ? user-packages-root, + package-namespace ? null, + extra-overlays ? {}, + }: let + fake-pkgs = { + callPackage = x: x; + isFakePkgs = true; + lib = {}; + system = "fake-system"; + }; + + user-overlays = snowfall-lib.fs.get-default-nix-files-recursive src; + + channel-systems = user-inputs.self.pkgs; + + user-packages-overlay = final: prev: let + user-packages = snowfall-lib.package.create-packages { + pkgs = final; + channels = channel-systems.${prev.system}; + }; + in + if package-namespace == null + then user-packages + else { + ${package-namespace} = + (prev.${package-namespace} or {}) + // user-packages; }; - user-overlays = snowfall-lib.fs.get-default-nix-files-recursive src; - - channel-systems = user-inputs.self.pkgs; - - user-packages-overlay = final: prev: - let - user-packages = snowfall-lib.package.create-packages { - pkgs = final; - channels = channel-systems.${prev.system}; - }; - in - if package-namespace == null then - user-packages - else - { - ${package-namespace} = - (prev.${package-namespace} or { }) - // user-packages; - }; - - create-overlay = (overlays: file: - let - name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory file); - overlay = final: prev: - let - channels = channel-systems.${prev.system}; - user-overlay = import file (user-inputs // { inherit channels; }); - packages = user-packages-overlay final prev; - prev-with-packages = - if package-namespace == null then - prev // packages - else - prev // { - ${package-namespace} = - (prev.${package-namespace} or { }) - // packages.${package-namespace}; - }; - user-overlay-packages = - user-overlay - final - prev-with-packages; - outputs = - user-overlay-packages; - in - if user-overlay-packages.__dontExport or false == true then - outputs // { __dontExport = true; } + create-overlay = ( + overlays: file: let + name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory file); + overlay = final: prev: let + channels = channel-systems.${prev.system}; + user-overlay = import file (user-inputs // {inherit channels;}); + packages = user-packages-overlay final prev; + prev-with-packages = + if package-namespace == null + then prev // packages else - outputs; - fake-overlay-result = overlay fake-pkgs fake-pkgs; + prev + // { + ${package-namespace} = + (prev.${package-namespace} or {}) + // packages.${package-namespace}; + }; + user-overlay-packages = + user-overlay + final + prev-with-packages; + outputs = + user-overlay-packages; in - if fake-overlay-result.__dontExport or false == true then - overlays + if user-overlay-packages.__dontExport or false == true + then outputs // {__dontExport = true;} + else outputs; + fake-overlay-result = overlay fake-pkgs fake-pkgs; + in + if fake-overlay-result.__dontExport or false == true + then overlays else - overlays // { + overlays + // { ${name} = overlay; } - ); + ); - overlays = - foldl - create-overlay - { } - user-overlays; + overlays = + foldl + create-overlay + {} + user-overlays; - user-packages = snowfall-lib.fs.get-default-nix-files-recursive packages-src; + user-packages = snowfall-lib.fs.get-default-nix-files-recursive packages-src; - create-package-overlay = package-overlays: file: - let - name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory file); - overlay = final: prev: - let - channels = channel-systems.${prev.system}; - packages = snowfall-lib.package.create-packages { - channels = channel-systems.${prev.system}; - }; - in - if package-namespace == null then - { ${name} = packages.${name}; } - else - { - ${package-namespace} = - (prev.${package-namespace} or { }) - // { ${name} = packages.${name}; }; - }; - in - package-overlays // - { - "package/${name}" = overlay; + create-package-overlay = package-overlays: file: let + name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory file); + overlay = final: prev: let + channels = channel-systems.${prev.system}; + packages = snowfall-lib.package.create-packages { + channels = channel-systems.${prev.system}; + }; + in + if package-namespace == null + then {${name} = packages.${name};} + else { + ${package-namespace} = + (prev.${package-namespace} or {}) + // {${name} = packages.${name};}; }; - - package-overlays = - foldl - create-package-overlay - { } - user-packages; - - default-overlay = final: prev: - let - overlays-list = builtins.attrValues overlays; - package-overlays-list = builtins.attrValues package-overlays; - - overlays-results = builtins.map (overlay: overlay final prev) overlays-list; - package-overlays-results = builtins.map (overlay: overlay final prev) package-overlays-list; - - merged-results = snowfall-lib.attrs.merge-shallow-packages - (package-overlays-results ++ overlays-results); - in - merged-results; in + package-overlays + // { + "package/${name}" = overlay; + }; + + package-overlays = + foldl + create-package-overlay + {} + user-packages; + + default-overlay = final: prev: let + overlays-list = builtins.attrValues overlays; + package-overlays-list = builtins.attrValues package-overlays; + + overlays-results = builtins.map (overlay: overlay final prev) overlays-list; + package-overlays-results = builtins.map (overlay: overlay final prev) package-overlays-list; + + merged-results = + snowfall-lib.attrs.merge-shallow-packages + (package-overlays-results ++ overlays-results); + in + merged-results; + in package-overlays // overlays - // { default = default-overlay; } + // {default = default-overlay;} // extra-overlays; }; } - diff --git a/snowfall-lib/package/default.nix b/snowfall-lib/package/default.nix index 3fea3b5..ce7b218 100644 --- a/snowfall-lib/package/default.nix +++ b/snowfall-lib/package/default.nix @@ -1,16 +1,14 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (core-inputs.flake-utils-plus.lib) filterPackages allSystems; inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs filterAttrs callPackageWith; user-packages-root = snowfall-lib.fs.get-snowfall-file "packages"; -in -{ +in { package = rec { ## Create flake output packages. ## Example Usage: @@ -22,51 +20,54 @@ in ## { another-package = ...; my-package = ...; default = ...; } ## ``` #@ Attrs -> Attrs - create-packages = - { channels - , src ? user-packages-root - , pkgs ? channels.nixpkgs - , overrides ? { } - , alias ? { } - , package-namespace ? "internal" - }: - let - user-packages = snowfall-lib.fs.get-default-nix-files-recursive src; - create-package-metadata = package: - let - namespaced-packages = { - ${package-namespace} = packages-without-aliases; - }; - extra-inputs = pkgs // namespaced-packages // { - inherit channels; - lib = snowfall-lib.internal.system-lib; - pkgs = pkgs // namespaced-packages; - inputs = snowfall-lib.flake.without-snowfall-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; - }; + create-packages = { + channels, + src ? user-packages-root, + pkgs ? channels.nixpkgs, + overrides ? {}, + alias ? {}, + package-namespace ? "internal", + }: let + user-packages = snowfall-lib.fs.get-default-nix-files-recursive src; + create-package-metadata = package: let + namespaced-packages = { + ${package-namespace} = packages-without-aliases; + }; + extra-inputs = + pkgs + // namespaced-packages + // { + inherit channels; + lib = snowfall-lib.internal.system-lib; + pkgs = pkgs // namespaced-packages; + inputs = snowfall-lib.flake.without-snowfall-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-without-aliases = foldl merge-packages { } packages-metadata; - aliased-packages = mapAttrs (name: value: packages-without-aliases.${value}) alias; - packages = packages-without-aliases // aliased-packages // overrides; - in + }; + packages-metadata = builtins.map create-package-metadata user-packages; + merge-packages = packages: metadata: + packages + // { + ${metadata.name} = metadata.drv; + }; + packages-without-aliases = foldl merge-packages {} packages-metadata; + aliased-packages = mapAttrs (name: value: packages-without-aliases.${value}) alias; + packages = packages-without-aliases // aliased-packages // overrides; + in filterPackages pkgs.system packages; }; } diff --git a/snowfall-lib/path/default.nix b/snowfall-lib/path/default.nix index d09173e..7d35455 100644 --- a/snowfall-lib/path/default.nix +++ b/snowfall-lib/path/default.nix @@ -1,16 +1,14 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (builtins) toString baseNameOf dirOf concatStringsSep; inherit (core-inputs.nixpkgs.lib) assertMsg last init; file-name-regex = "(.*)\\.(.*)$"; -in -{ +in { path = rec { ## Split a file name and its extension. ## Example Usage: @@ -22,12 +20,10 @@ in ## [ "my-file" "md" ] ## ``` #@ String -> [String] - split-file-extension = file: - let - match = builtins.match file-name-regex file; - in - assert assertMsg (match != null) "lib.snowfall.split-file-extension: File must have an extension to split."; - match; + split-file-extension = file: let + match = builtins.match file-name-regex file; + in + assert assertMsg (match != null) "lib.snowfall.split-file-extension: File must have an extension to split."; match; ## Check if a file name has a file extension. ## Example Usage: @@ -39,10 +35,9 @@ in ## true ## ``` #@ String -> Bool - has-any-file-extension = file: - let - match = builtins.match file-name-regex (toString file); - in + has-any-file-extension = file: let + match = builtins.match file-name-regex (toString file); + in match != null; ## Get the file extension of a file name. @@ -56,13 +51,12 @@ in ## ``` #@ String -> String get-file-extension = file: - if has-any-file-extension file then - let - match = builtins.match file-name-regex (toString file); - in + if has-any-file-extension file + then let + match = builtins.match file-name-regex (toString file); + in last match - else - ""; + else ""; ## Check if a file name has a specific file extension. ## Example Usage: @@ -75,10 +69,9 @@ in ## ``` #@ String -> String -> Bool has-file-extension = extension: file: - if has-any-file-extension file then - extension == get-file-extension file - else - false; + if has-any-file-extension file + then extension == get-file-extension file + else false; ## Get the parent directory for a given path. ## Example Usage: @@ -102,13 +95,11 @@ in ## "my-file" ## ``` #@ Path -> String - get-file-name-without-extension = path: - let - file-name = baseNameOf path; - in - if has-any-file-extension file-name then - concatStringsSep "" (init (split-file-extension file-name)) - else - file-name; + get-file-name-without-extension = path: let + file-name = baseNameOf path; + in + if has-any-file-extension file-name + then concatStringsSep "" (init (split-file-extension file-name)) + else file-name; }; } diff --git a/snowfall-lib/shell/default.nix b/snowfall-lib/shell/default.nix index c1cb6a7..70da329 100644 --- a/snowfall-lib/shell/default.nix +++ b/snowfall-lib/shell/default.nix @@ -1,16 +1,14 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (core-inputs.flake-utils-plus.lib) filterPackages; inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs callPackageWith; user-shells-root = snowfall-lib.fs.get-snowfall-file "shells"; -in -{ +in { shell = { ## Create flake output packages. ## Example Usage: @@ -22,36 +20,36 @@ in ## { another-shell = ...; my-shell = ...; default = ...; } ## ``` #@ Attrs -> Attrs - create-shells = - { channels - , src ? user-shells-root - , pkgs ? channels.nixpkgs - , overrides ? { } - , alias ? { } - }: - let - user-shells = snowfall-lib.fs.get-default-nix-files-recursive src; - create-shell-metadata = shell: - let - extra-inputs = pkgs // { - inherit channels; - lib = snowfall-lib.internal.system-lib; - inputs = snowfall-lib.flake.without-src user-inputs; - }; - in - { - name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory shell); - drv = callPackageWith extra-inputs shell { }; + create-shells = { + channels, + src ? user-shells-root, + pkgs ? channels.nixpkgs, + overrides ? {}, + alias ? {}, + }: let + user-shells = snowfall-lib.fs.get-default-nix-files-recursive src; + create-shell-metadata = shell: let + extra-inputs = + pkgs + // { + inherit channels; + lib = snowfall-lib.internal.system-lib; + inputs = snowfall-lib.flake.without-src user-inputs; }; - shells-metadata = builtins.map create-shell-metadata user-shells; - merge-shells = shells: metadata: - shells // { - ${metadata.name} = metadata.drv; - }; - shells-without-aliases = foldl merge-shells { } shells-metadata; - aliased-shells = mapAttrs (name: value: shells-without-aliases.${value}) alias; - shells = shells-without-aliases // aliased-shells // overrides; - in + in { + name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory shell); + drv = callPackageWith extra-inputs shell {}; + }; + shells-metadata = builtins.map create-shell-metadata user-shells; + merge-shells = shells: metadata: + shells + // { + ${metadata.name} = metadata.drv; + }; + shells-without-aliases = foldl merge-shells {} shells-metadata; + aliased-shells = mapAttrs (name: value: shells-without-aliases.${value}) alias; + shells = shells-without-aliases // aliased-shells // overrides; + in filterPackages pkgs.system shells; }; } diff --git a/snowfall-lib/system/default.nix b/snowfall-lib/system/default.nix index 5e3e345..acb34d5 100644 --- a/snowfall-lib/system/default.nix +++ b/snowfall-lib/system/default.nix @@ -1,10 +1,9 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (builtins) dirOf baseNameOf; inherit (core-inputs.nixpkgs.lib) assertMsg fix hasInfix concatMap foldl optionals singleton; @@ -12,8 +11,7 @@ let user-systems-root = snowfall-lib.fs.get-snowfall-file "systems"; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; -in -{ +in { system = rec { ## Get the name of a system based on its file path. ## Example Usage: @@ -26,10 +24,9 @@ in ## ``` #@ Path -> String get-inferred-system-name = path: - if snowfall-lib.path.has-file-extension "nix" path then - snowfall-lib.path.get-parent-directory path - else - baseNameOf path; + if snowfall-lib.path.has-file-extension "nix" path + then snowfall-lib.path.get-parent-directory path + else baseNameOf path; ## Check whether a named system is macOS. ## Example Usage: @@ -80,14 +77,14 @@ in #@ String -> String get-virtual-system-type = target: foldl - (result: virtual-system: - if result == "" && hasInfix virtual-system target then - virtual-system - else - result - ) - "" - virtual-systems; + ( + result: virtual-system: + if result == "" && hasInfix virtual-system target + then virtual-system + else result + ) + "" + virtual-systems; ## Get structured data about all systems for a given target. ## Example Usage: @@ -99,23 +96,22 @@ in ## [ { target = "x86_64-linux"; name = "my-machine"; path = "/systems/x86_64-linux/my-machine"; } ] ## ``` #@ String -> [Attrs] - get-target-systems-metadata = target: - let - systems = snowfall-lib.fs.get-directories target; - existing-systems = builtins.filter (system: builtins.pathExists "${system}/default.nix") systems; - create-system-metadata = path: { - path = "${path}/default.nix"; - # 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); - # 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. - target = builtins.unsafeDiscardStringContext (builtins.baseNameOf target); - }; - system-configurations = builtins.map create-system-metadata existing-systems; - in + get-target-systems-metadata = target: let + systems = snowfall-lib.fs.get-directories target; + existing-systems = builtins.filter (system: builtins.pathExists "${system}/default.nix") systems; + create-system-metadata = path: { + path = "${path}/default.nix"; + # 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); + # 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. + target = builtins.unsafeDiscardStringContext (builtins.baseNameOf target); + }; + system-configurations = builtins.map create-system-metadata existing-systems; + in system-configurations; ## Get the system builder for a given target. @@ -128,49 +124,62 @@ in ## (args: ) ## ``` #@ String -> Function - get-system-builder = target: - let - virtual-system-type = get-virtual-system-type target; - virtual-system-builder = args: - assert assertMsg (user-inputs ? nixos-generators) "In order to create virtual systems, you must include `nixos-generators` as a flake input."; + get-system-builder = target: let + virtual-system-type = get-virtual-system-type target; + virtual-system-builder = args: + assert assertMsg (user-inputs ? nixos-generators) "In order to create virtual systems, you must include `nixos-generators` as a flake input."; user-inputs.nixos-generators.nixosGenerate - (args // { + (args + // { format = virtual-system-type; - specialArgs = args.specialArgs // { - format = virtual-system-type; - }; - modules = args.modules ++ [ - ../../modules/nixos/user/default.nix - ]; + specialArgs = + args.specialArgs + // { + format = virtual-system-type; + }; + modules = + args.modules + ++ [ + ../../modules/nixos/user/default.nix + ]; }); - darwin-system-builder = args: - assert assertMsg (user-inputs ? darwin) "In order to create virtual systems, you must include `darwin` as a flake input."; + darwin-system-builder = args: + assert assertMsg (user-inputs ? darwin) "In order to create virtual systems, you must include `darwin` as a flake input."; user-inputs.darwin.lib.darwinSystem - ((builtins.removeAttrs args [ "system" "modules" ]) // { - specialArgs = args.specialArgs // { - format = "darwin"; - }; - modules = args.modules ++ [ - ../../modules/darwin/user/default.nix - ]; + ((builtins.removeAttrs args ["system" "modules"]) + // { + specialArgs = + args.specialArgs + // { + format = "darwin"; + }; + modules = + args.modules + ++ [ + ../../modules/darwin/user/default.nix + ]; }); - linux-system-builder = args: - core-inputs.nixpkgs.lib.nixosSystem - (args // { - specialArgs = args.specialArgs // { + linux-system-builder = args: + core-inputs.nixpkgs.lib.nixosSystem + (args + // { + specialArgs = + args.specialArgs + // { format = "linux"; }; - modules = args.modules ++ [ + modules = + args.modules + ++ [ ../../modules/nixos/user/default.nix ]; - }); - in - if virtual-system-type != "" then - virtual-system-builder - else if is-darwin target then - darwin-system-builder - else - linux-system-builder; + }); + in + if virtual-system-type != "" + then virtual-system-builder + else if is-darwin target + then darwin-system-builder + else linux-system-builder; ## Get the flake output attribute for a system target. ## Example Usage: @@ -182,16 +191,14 @@ in ## "darwinConfigurations" ## ``` #@ String -> String - get-system-output = target: - let - virtual-system-type = get-virtual-system-type target; - in - if virtual-system-type != "" then - "${virtual-system-type}Configurations" - else if is-darwin target then - "darwinConfigurations" - else - "nixosConfigurations"; + get-system-output = target: let + virtual-system-type = get-virtual-system-type target; + in + if virtual-system-type != "" + then "${virtual-system-type}Configurations" + else if is-darwin target + then "darwinConfigurations" + else "nixosConfigurations"; ## Get the resolved (non-virtual) system target. ## Example Usage: @@ -203,14 +210,12 @@ in ## "x86_64-linux" ## ``` #@ String -> String - get-resolved-system-target = target: - let - virtual-system-type = get-virtual-system-type target; - in - if virtual-system-type != "" then - builtins.replaceStrings [ virtual-system-type ] [ "linux" ] target - else - target; + get-resolved-system-target = target: let + virtual-system-type = get-virtual-system-type target; + in + if virtual-system-type != "" + then builtins.replaceStrings [virtual-system-type] ["linux"] target + else target; ## Create a system. ## Example Usage: @@ -222,42 +227,41 @@ in ## ## ``` #@ Attrs -> Attrs - create-system = - { target ? "x86_64-linux" - , system ? get-resolved-system-target target - , path - , name ? builtins.unsafeDiscardStringContext (get-inferred-system-name path) - , modules ? [ ] - , specialArgs ? { } - , channelName ? "nixpkgs" - , builder ? get-system-builder target - , output ? get-system-output target - , systems ? { } - , homes ? { } - }: - let - lib = snowfall-lib.internal.system-lib; - home-system-modules = snowfall-lib.home.create-home-system-modules homes; - home-manager-module = - if is-darwin system then - user-inputs.home-manager.darwinModules.home-manager - else - user-inputs.home-manager.nixosModules.home-manager; - home-manager-modules = [ home-manager-module ] ++ home-system-modules; - in - { - inherit channelName system builder output; + create-system = { + target ? "x86_64-linux", + system ? get-resolved-system-target target, + path, + name ? builtins.unsafeDiscardStringContext (get-inferred-system-name path), + modules ? [], + specialArgs ? {}, + channelName ? "nixpkgs", + builder ? get-system-builder target, + output ? get-system-output target, + systems ? {}, + homes ? {}, + }: let + lib = snowfall-lib.internal.system-lib; + home-system-modules = snowfall-lib.home.create-home-system-modules homes; + home-manager-module = + if is-darwin system + then user-inputs.home-manager.darwinModules.home-manager + else user-inputs.home-manager.nixosModules.home-manager; + home-manager-modules = [home-manager-module] ++ home-system-modules; + in { + inherit channelName system builder output; - modules = [ path ] ++ modules ++ (optionals (user-inputs ? home-manager) home-manager-modules); + modules = [path] ++ modules ++ (optionals (user-inputs ? home-manager) home-manager-modules); - specialArgs = specialArgs // { + specialArgs = + specialArgs + // { inherit target system systems lib; host = name; virtual = (get-virtual-system-type target) != ""; inputs = snowfall-lib.flake.without-src user-inputs; }; - }; + }; ## Create all available systems. ## Example Usage: @@ -269,50 +273,52 @@ in ## { my-host = ; } ## ``` #@ Attrs -> Attrs - create-systems = { systems ? { }, homes ? { } }: - let - targets = snowfall-lib.fs.get-directories user-systems-root; - target-systems-metadata = concatMap get-target-systems-metadata targets; - user-nixos-modules = snowfall-lib.module.create-modules { - src = "${user-modules-root}/nixos"; - }; - user-darwin-modules = snowfall-lib.module.create-modules { - src = "${user-modules-root}/darwin"; - }; - nixos-modules = systems.modules.nixos or [ ]; - darwin-modules = systems.modules.darwin or [ ]; + create-systems = { + systems ? {}, + homes ? {}, + }: let + targets = snowfall-lib.fs.get-directories user-systems-root; + target-systems-metadata = concatMap get-target-systems-metadata targets; + user-nixos-modules = snowfall-lib.module.create-modules { + src = "${user-modules-root}/nixos"; + }; + user-darwin-modules = snowfall-lib.module.create-modules { + src = "${user-modules-root}/darwin"; + }; + nixos-modules = systems.modules.nixos or []; + darwin-modules = systems.modules.darwin or []; - create-system' = created-systems: system-metadata: - let - overrides = systems.hosts.${system-metadata.name} or { }; - user-modules = - if is-darwin system-metadata.target then - user-darwin-modules - else - user-nixos-modules; - user-modules-list = builtins.attrValues user-modules; - system-modules = - if is-darwin system-metadata.target then - darwin-modules - else - nixos-modules; - in - { - ${system-metadata.name} = create-system (overrides // system-metadata // { - systems = created-systems; - modules = user-modules-list ++ (overrides.modules or [ ]) ++ system-modules; - inherit homes; - }); - }; - created-systems = fix (created-systems: + create-system' = created-systems: system-metadata: let + overrides = systems.hosts.${system-metadata.name} or {}; + user-modules = + if is-darwin system-metadata.target + then user-darwin-modules + else user-nixos-modules; + user-modules-list = builtins.attrValues user-modules; + system-modules = + if is-darwin system-metadata.target + then darwin-modules + else nixos-modules; + in { + ${system-metadata.name} = create-system (overrides + // system-metadata + // { + systems = created-systems; + modules = user-modules-list ++ (overrides.modules or []) ++ system-modules; + inherit homes; + }); + }; + created-systems = fix ( + created-systems: foldl - (systems: system-metadata: + ( + systems: system-metadata: systems // (create-system' created-systems system-metadata) - ) - { } - target-systems-metadata - ); - in + ) + {} + target-systems-metadata + ); + in created-systems; }; } diff --git a/snowfall-lib/template/default.nix b/snowfall-lib/template/default.nix index 40099e7..5b1b100 100644 --- a/snowfall-lib/template/default.nix +++ b/snowfall-lib/template/default.nix @@ -1,16 +1,14 @@ -{ core-inputs -, user-inputs -, snowfall-lib -, snowfall-config -}: - -let +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let inherit (builtins) baseNameOf; inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs; user-templates-root = snowfall-lib.fs.get-snowfall-file "templates"; -in -{ +in { template = { ## Create flake templates. ## @@ -24,28 +22,30 @@ in ## { another-template = ...; my-template = ...; default = ...; } ## ``` #@ Attrs -> Attrs - create-templates = - { src ? user-templates-root - , overrides ? { } - , alias ? { } - }: - let - user-templates = snowfall-lib.fs.get-directories src; - create-template-metadata = template: { - name = builtins.unsafeDiscardStringContext (baseNameOf template); - path = template; - }; - templates-metadata = builtins.map create-template-metadata user-templates; - merge-templates = templates: metadata: - templates // { - ${metadata.name} = (overrides.${metadata.name} or { }) // { + create-templates = { + src ? user-templates-root, + overrides ? {}, + alias ? {}, + }: let + user-templates = snowfall-lib.fs.get-directories src; + create-template-metadata = template: { + name = builtins.unsafeDiscardStringContext (baseNameOf template); + path = template; + }; + templates-metadata = builtins.map create-template-metadata user-templates; + merge-templates = templates: metadata: + templates + // { + ${metadata.name} = + (overrides.${metadata.name} or {}) + // { inherit (metadata) path; }; - }; - templates-without-aliases = foldl merge-templates { } templates-metadata; - aliased-templates = mapAttrs (name: value: templates-without-aliases.${value}) alias; - templates = templates-without-aliases // aliased-templates // overrides; - in + }; + templates-without-aliases = foldl merge-templates {} templates-metadata; + aliased-templates = mapAttrs (name: value: templates-without-aliases.${value}) alias; + templates = templates-without-aliases // aliased-templates // overrides; + in templates; }; } From 9b718da230d07de8f6f76455bb499a53a7d4a9b4 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Fri, 23 Feb 2024 08:57:44 -0800 Subject: [PATCH 05/14] feat: add lib and inputs args to overlay Overlays previously would only receive a "channels" argument and the rest of the attribute set would be the flake's inputs. This is now changing to support passing "lib", the same instance you would get in your system configuration, and "inputs", an attribute set of the flake's inputs. Referencing flake inputs by name here is now deprecated and will be removed in a future update. --- snowfall-lib/overlay/default.nix | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/snowfall-lib/overlay/default.nix b/snowfall-lib/overlay/default.nix index 5dd702c..4419542 100644 --- a/snowfall-lib/overlay/default.nix +++ b/snowfall-lib/overlay/default.nix @@ -26,7 +26,16 @@ in { extra-overlays ? [], }: channels: let user-overlays = snowfall-lib.fs.get-default-nix-files-recursive src; - create-overlay = overlay: import overlay (user-inputs // {inherit channels;}); + create-overlay = overlay: + import overlay ( + # Deprecated: Use `inputs.*` instead of referencing the input name directly. + user-inputs + // { + inherit channels; + inputs = user-inputs; + lib = snowfall-lib.internal.system-lib; + } + ); user-packages-overlay = final: prev: let user-packages = snowfall-lib.package.create-packages { pkgs = final; @@ -89,7 +98,15 @@ in { name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory file); overlay = final: prev: let channels = channel-systems.${prev.system}; - user-overlay = import file (user-inputs // {inherit channels;}); + user-overlay = import file ( + # Deprecated: Use `inputs.*` instead of referencing the input name directly. + user-inputs + // { + inherit channels; + inputs = user-inputs; + lib = snowfall-lib.internal.system-lib; + } + ); packages = user-packages-overlay final prev; prev-with-packages = if package-namespace == null From 11476c3b905e88a6fbf9430465d7b0d03e1c5799 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Fri, 23 Feb 2024 09:51:32 -0800 Subject: [PATCH 06/14] fix: pass home specialArgs to home in nixos module --- modules/home/user/default.nix | 2 +- modules/nixos/user/default.nix | 2 +- snowfall-lib/home/default.nix | 16 +++++++++++----- snowfall-lib/system/virtual-systems.nix | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/home/user/default.nix b/modules/home/user/default.nix index 9ea443b..4f04b62 100644 --- a/modules/home/user/default.nix +++ b/modules/home/user/default.nix @@ -9,7 +9,7 @@ inputs @ { cfg = config.snowfallorg; - # @NOTE(jakehamilton): The module system chokes if it finds `osConfig` named in the module arguments + # NOTE: The module system chokes if it finds `osConfig` named in the module arguments # when being used in standalone home-manager. To remedy this, we have to refer to the arguments set directly. os-user-home = inputs.osConfig.users.users.${cfg.name}.home or null; diff --git a/modules/nixos/user/default.nix b/modules/nixos/user/default.nix index d1d195b..9e11060 100644 --- a/modules/nixos/user/default.nix +++ b/modules/nixos/user/default.nix @@ -62,7 +62,7 @@ in { config = mkOption { # HM-compatible options taken from: # https://github.com/nix-community/home-manager/blob/0ee5ab611dc1fbb5180bd7d88d2aeb7841a4d179/nixos/common.nix#L14 - # @NOTE(jakehamilton): This has been adapted to support documentation generation without + # NOTE: This has been adapted to support documentation generation without # having home-manager options fully declared. type = types.submoduleWith { specialArgs = diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index f928063..8d0a386 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -32,13 +32,13 @@ in { home = rec { # Modules in home-manager expect `hm` to be available directly on `lib` itself. home-lib = - # @NOTE(jakehamilton): This prevents an error during evaluation if the input does + # NOTE: This prevents an error during evaluation if the input does # not exist. if user-inputs ? home-manager then snowfall-lib.internal.system-lib.extend (final: prev: - # @NOTE(jakehamilton): This order is important, this library's extend and other utilities must write + # NOTE: This order is important, this library's extend and other utilities must write # _over_ the original `system-lib`. snowfall-lib.internal.system-lib // prev @@ -90,7 +90,7 @@ in { }: let user-metadata = split-user-and-host name; - # @NOTE(jakehamilton): 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;}; lib = home-lib; in @@ -115,7 +115,7 @@ in { inputs = snowfall-lib.flake.without-src user-inputs; - # @NOTE(jakehamilton): 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. inherit pkgs lib; }; @@ -294,7 +294,7 @@ in { }: let host-matches = created-user.specialArgs.host == host; - # @NOTE(jakehamilton): 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..home.config` since `mkAliasDefinitions` # does not let us target options within a submodule. wrap-user-options = user-option: @@ -333,6 +333,12 @@ in { enable = true; name = mkDefault user-name; }; + + # NOTE: specialArgs are not propagated by Home-Manager without this. + # However, not all specialArgs values can be set when using `_module.args`. + _module.args = + builtins.removeAttrs (users.users.${name}.specialArgs or {}) + ["options" "config" "lib" "pkgs" "specialArgs"]; }; home-manager = { diff --git a/snowfall-lib/system/virtual-systems.nix b/snowfall-lib/system/virtual-systems.nix index b7dac48..af0f548 100644 --- a/snowfall-lib/system/virtual-systems.nix +++ b/snowfall-lib/system/virtual-systems.nix @@ -1,4 +1,4 @@ -# @NOTE(jakehamilton): The order of these entries matters. We search them +# NOTE: The order of these entries matters. We search them # from start to finish and only match based on whether they appear in a # system target. This means that entries like "vm" would match all cases # of "vm-bootloader", "vm-no-gui", and "vmware". To avoid this mismatch, From 8e0d7a3cf69ae15e3d3aad37b6d81c0839347e6d Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Fri, 23 Feb 2024 10:34:34 -0800 Subject: [PATCH 07/14] fix: use mkDefault for default values --- snowfall-lib/home/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 8d0a386..c654919 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -330,7 +330,7 @@ in { # Initialize user information. snowfallorg.user.${user-name}.home.config = { snowfallorg.user = { - enable = true; + enable = mkDefault true; name = mkDefault user-name; }; From c13316eacefe63893289d19c198bfbd6b77ddaa2 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 25 Feb 2024 04:03:02 -0800 Subject: [PATCH 08/14] fix: install shared modules, strip homes from config --- snowfall-lib/flake/default.nix | 1 + snowfall-lib/home/default.nix | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index a0904ec..391172b 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -65,6 +65,7 @@ in rec { "outputsBuilder" "packagesPrefix" "hosts" + "homes" "channels-config" "templates" "package-namespace" diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index c654919..23a9957 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -231,7 +231,11 @@ in { src = "${user-modules-root}/home"; }; - shared-modules = + shared-modules = builtins.map (module: { + config.home-manager.sharedModules = [module]; + }) (users.modules or []); + + shared-user-modules = mapAttrsToList (module-path: module: { _file = "${user-modules-root}/home/${module-path}/default.nix"; @@ -361,6 +365,7 @@ in { snowfall-user-home-module ] ++ shared-modules + ++ shared-user-modules ++ system-modules; }; } From 1fd6a70a823f12402e1fcf59bc3836eef610d501 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Fri, 1 Mar 2024 13:34:23 -0800 Subject: [PATCH 09/14] fix: check for hyperv install iso first --- snowfall-lib/system/virtual-systems.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snowfall-lib/system/virtual-systems.nix b/snowfall-lib/system/virtual-systems.nix index af0f548..6690c08 100644 --- a/snowfall-lib/system/virtual-systems.nix +++ b/snowfall-lib/system/virtual-systems.nix @@ -10,8 +10,8 @@ "docker" "do" "gce" - "hyperv" "install-iso-hyperv" + "hyperv" "install-iso" "iso" "kexec" From 8d6612527a7783c6783c5fa77464c2cd485514cc Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Mon, 4 Mar 2024 09:42:48 -0800 Subject: [PATCH 10/14] refactor: rename snowfallorg.user to snowfallorg.users in system configs --- modules/darwin/user/default.nix | 20 ++++++++++++++++---- modules/nixos/user/default.nix | 21 +++++++++++++++++---- snowfall-lib/home/default.nix | 10 +++++----- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/modules/darwin/user/default.nix b/modules/darwin/user/default.nix index a9058ee..a0303d6 100644 --- a/modules/darwin/user/default.nix +++ b/modules/darwin/user/default.nix @@ -6,14 +6,22 @@ inputs, ... }: let - inherit (lib) types mkOption mkDefault foldl optionalAttrs; + inherit + (lib) + types + mkOption + mkDefault + mkRenamedOptionModule + foldl + optionalAttrs + ; cfg = config.snowfallorg; - user-names = builtins.attrNames cfg.user; + user-names = builtins.attrNames cfg.users; create-system-users = system-users: name: let - user = cfg.user.${name}; + user = cfg.users.${name}; in system-users // (optionalAttrs user.create { @@ -23,8 +31,12 @@ }; }); in { + imports = [ + (mkRenamedOptionModule ["snowfallorg" "user"] ["snowfallorg" "users"]) + ]; + options.snowfallorg = { - user = mkOption { + users = mkOption { description = "User configuration."; default = {}; type = types.attrsOf (types.submodule ({name, ...}: { diff --git a/modules/nixos/user/default.nix b/modules/nixos/user/default.nix index 9e11060..b19d155 100644 --- a/modules/nixos/user/default.nix +++ b/modules/nixos/user/default.nix @@ -5,16 +5,25 @@ args @ { config, ... }: let - inherit (lib) types mkOption mkDefault foldl optionalAttrs optional; + inherit + (lib) + types + mkOption + mkDefault + mkRenamedOptionModule + foldl + optionalAttrs + optional + ; cfg = config.snowfallorg; inputs = args.inputs or {}; - user-names = builtins.attrNames cfg.user; + user-names = builtins.attrNames cfg.users; create-system-users = system-users: name: let - user = cfg.user.${name}; + user = cfg.users.${name}; in system-users // (optionalAttrs user.create { @@ -30,8 +39,12 @@ args @ { }; }); in { + imports = [ + (mkRenamedOptionModule ["snowfallorg" "user"] ["snowfallorg" "users"]) + ]; + options.snowfallorg = { - user = mkOption { + users = mkOption { description = "User configuration."; default = {}; type = types.attrsOf (types.submodule ({name, ...}: { diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 23a9957..314c022 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -318,7 +318,7 @@ in { (builtins.trace '' ============= Snowfall Lib: - Option value for `snowfallorg.user.${user-name}` was not detected to be merged. + Option value for `snowfallorg.users.${user-name}` was not detected to be merged. Please report the issue on GitHub with a link to your configuration so we can debug the problem: https://github.com/snowfallorg/lib/issues/new @@ -326,13 +326,13 @@ in { '') user-option; - home-config = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.user; + home-config = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.users; in { _file = "virtual:snowfallorg/home/user/${name}"; config = mkIf host-matches { # Initialize user information. - snowfallorg.user.${user-name}.home.config = { + snowfallorg.users.${user-name}.home.config = { snowfallorg.user = { enable = mkDefault true; name = mkDefault user-name; @@ -346,9 +346,9 @@ in { }; home-manager = { - users.${user-name} = mkIf config.snowfallorg.user.${user-name}.home.enable ({pkgs, ...}: { + users.${user-name} = mkIf config.snowfallorg.users.${user-name}.home.enable ({pkgs, ...}: { imports = (home-config.imports or []) ++ other-modules ++ [user-module]; - config = home-config; + config = builtins.removeAttrs home-config ["imports"]; }); # NOTE: Without this home-manager will instead create its own package set which won't contain the same config and From 63a1abf65b5051429efcab9a3f96ef3fc346254d Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Mon, 4 Mar 2024 10:23:31 -0800 Subject: [PATCH 11/14] feat: support target-wide user home --- snowfall-lib/home/default.nix | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index 314c022..d53d76e 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -24,6 +24,8 @@ mkAliasAndWrapDefinitions mkOption types + hasInfix + hasSuffix ; user-homes-root = snowfall-lib.fs.get-snowfall-file "homes"; @@ -95,7 +97,7 @@ in { lib = home-lib; in assert assertMsg (user-inputs ? home-manager) "In order to create home-manager configurations, you must include `home-manager` as a flake input."; - assert assertMsg (user-metadata.host != "") "Snowfall Lib homes must be named with the format: user@system"; { + assert assertMsg ((user-metadata.host != "") || !(hasInfix "@" name)) "Snowfall Lib homes must be named with the format: user@system"; { inherit channelName system; output = "homeConfigurations"; @@ -108,7 +110,7 @@ in { ++ modules; specialArgs = { - inherit name; + inherit name system; inherit (user-metadata) user host; format = "home"; @@ -294,9 +296,12 @@ in { options, pkgs, host ? "", + system ? pkgs.system, ... }: let - host-matches = created-user.specialArgs.host == host; + host-matches = + (created-user.specialArgs.host == host) + || (created-user.specialArgs.host == "" && created-user.specialArgs.system == 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` @@ -342,7 +347,7 @@ in { # However, not all specialArgs values can be set when using `_module.args`. _module.args = builtins.removeAttrs (users.users.${name}.specialArgs or {}) - ["options" "config" "lib" "pkgs" "specialArgs"]; + ["options" "config" "lib" "pkgs" "specialArgs" "host"]; }; home-manager = { From 1ee256fa62eb6372bab70f403a607c2a970b406c Mon Sep 17 00:00:00 2001 From: PerchunPak Date: Tue, 16 Apr 2024 11:19:30 +0200 Subject: [PATCH 12/14] fix: allow modules modules that are not functions --- snowfall-lib/default.nix | 14 +++++++++++--- snowfall-lib/internal/default.nix | 12 ++++++++++-- snowfall-lib/module/default.nix | 8 ++++++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/snowfall-lib/default.nix b/snowfall-lib/default.nix index f4f560d..b59e1d7 100644 --- a/snowfall-lib/default.nix +++ b/snowfall-lib/default.nix @@ -18,7 +18,7 @@ core-inputs: user-options: let user-inputs = user-options.inputs // {src = user-options.src;}; - inherit (core-inputs.nixpkgs.lib) assertMsg fix filterAttrs mergeAttrs fold recursiveUpdate callPackageWith; + inherit (core-inputs.nixpkgs.lib) assertMsg fix filterAttrs mergeAttrs fold recursiveUpdate callPackageWith isFunction; # Recursively merge a list of attribute sets. # Type: [Attrs] -> Attrs @@ -58,7 +58,7 @@ core-inputs: user-options: let core-inputs-libs = get-libs (without-self core-inputs); user-inputs-libs = get-libs (without-self user-inputs); - # NOTE: This root is different to accomodate the creation + # NOTE: This root is different to accommodate the creation # of a fake user-lib in order to run documentation on this flake. snowfall-lib-root = "${core-inputs.src}/snowfall-lib"; snowfall-lib-dirs = let @@ -103,7 +103,15 @@ core-inputs: user-options: let }; libs = builtins.map - (path: callPackageWith attrs path {}) + ( + path: let + imported-module = import path; + in + if isFunction imported-module + then callPackageWith attrs path {} + # the only difference is that there is no `override` and `overrideDerivation` on returned value + else imported-module + ) user-lib-modules; in merge-deep libs diff --git a/snowfall-lib/internal/default.nix b/snowfall-lib/internal/default.nix index fb01412..334f3f1 100644 --- a/snowfall-lib/internal/default.nix +++ b/snowfall-lib/internal/default.nix @@ -4,7 +4,7 @@ snowfall-lib, snowfall-config, }: let - inherit (core-inputs.nixpkgs.lib) assertMsg fix fold filterAttrs callPackageWith; + inherit (core-inputs.nixpkgs.lib) fix filterAttrs callPackageWith isFunction; core-inputs-libs = snowfall-lib.flake.get-libs (snowfall-lib.flake.without-self core-inputs); user-inputs-libs = snowfall-lib.flake.get-libs (snowfall-lib.flake.without-self user-inputs); @@ -34,7 +34,15 @@ }; libs = builtins.map - (path: callPackageWith attrs path {}) + ( + path: let + imported-module = import path; + in + if isFunction imported-module + then callPackageWith attrs path {} + # the only difference is that there is no `override` and `overrideDerivation` on returned value + else imported-module + ) user-lib-modules; in snowfall-lib.attrs.merge-deep libs diff --git a/snowfall-lib/module/default.nix b/snowfall-lib/module/default.nix index 8fe7601..2a00827 100644 --- a/snowfall-lib/module/default.nix +++ b/snowfall-lib/module/default.nix @@ -5,7 +5,7 @@ snowfall-config, }: let inherit (builtins) baseNameOf; - inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs hasPrefix; + inherit (core-inputs.nixpkgs.lib) foldl mapAttrs hasPrefix isFunction; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; in { @@ -67,7 +67,11 @@ in { inputs = snowfall-lib.flake.without-src user-inputs; }; - user-module = import metadata.path modified-args; + imported-user-module = import metadata.path; + user-module = + if isFunction imported-user-module + then imported-user-module modified-args + else imported-user-module; in user-module // {_file = metadata.path;}; }; From d6b766939af0350fcfad505cf3b693dbaf297c3b Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 19 May 2024 23:19:54 -0700 Subject: [PATCH 13/14] chore: bump nixpkgs and flake-utils-plus --- flake.lock | 8 ++++---- flake.nix | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/flake.lock b/flake.lock index 09d501f..81a831c 100644 --- a/flake.lock +++ b/flake.lock @@ -54,16 +54,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1677028070, - "narHash": "sha256-sUKqd8HYBrtPxCRXFWvsnQDnwqnw1uIDwu4khcZuL2k=", + "lastModified": 1716181197, + "narHash": "sha256-IXOUlMlt0f5n9BoJ56+CseCLV9aaBDoEygIWV/hnUso=", "owner": "nixos", "repo": "nixpkgs", - "rev": "d3a15cd8dc917f4364ba0b332a1c389dc3177603", + "rev": "461aad7a53142b9f9e2a666c810e86b20f5da76b", "type": "github" }, "original": { "owner": "nixos", - "ref": "release-22.11", + "ref": "release-23.11", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 03d3db4..cfb606c 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,7 @@ description = "Snowfall Lib"; inputs = { - nixpkgs.url = "github:nixos/nixpkgs/release-22.11"; + nixpkgs.url = "github:nixos/nixpkgs/release-23.11"; flake-utils-plus.url = "github:gytis-ivaskevicius/flake-utils-plus"; flake-compat = { From b2e636407591bfe7a1f9a4ff50e282ed51ce7b59 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Wed, 22 May 2024 19:03:29 -0700 Subject: [PATCH 14/14] feat: namespace arg, inputs for packages and overlays, get lib from root, add checks --- flake.nix | 2 +- snowfall-lib/checks/default.nix | 56 +++++++++++++++++++++++++++++++ snowfall-lib/default.nix | 1 + snowfall-lib/flake/default.nix | 19 +++++++---- snowfall-lib/fs/default.nix | 6 ++-- snowfall-lib/home/default.nix | 9 +++-- snowfall-lib/internal/default.nix | 3 +- snowfall-lib/module/default.nix | 1 + snowfall-lib/overlay/default.nix | 38 +++++++++++---------- snowfall-lib/package/default.nix | 8 ++--- snowfall-lib/shell/default.nix | 1 + snowfall-lib/system/default.nix | 1 + snowfall-lib/template/default.nix | 5 ++- 13 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 snowfall-lib/checks/default.nix diff --git a/flake.nix b/flake.nix index cfb606c..470cc1b 100644 --- a/flake.nix +++ b/flake.nix @@ -62,7 +62,7 @@ aarch64-darwin = inputs.nixpkgs.legacyPackages.aarch64-darwin.alejandra; }; - _snowfall = rec { + snowfall = rec { raw-config = config; config = { diff --git a/snowfall-lib/checks/default.nix b/snowfall-lib/checks/default.nix new file mode 100644 index 0000000..9a59769 --- /dev/null +++ b/snowfall-lib/checks/default.nix @@ -0,0 +1,56 @@ +{ + core-inputs, + user-inputs, + snowfall-lib, + snowfall-config, +}: let + inherit (core-inputs.flake-utils-plus.lib) filterPackages; + inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs callPackageWith; + + user-checks-root = snowfall-lib.fs.get-snowfall-file "checks"; +in { + check = { + ## Create flake output packages. + ## Example Usage: + ## ```nix + ## create-checks { inherit channels; src = ./my-checks; overrides = { inherit another-check; }; alias = { default = "another-check"; }; } + ## ``` + ## Result: + ## ```nix + ## { another-check = ...; my-check = ...; default = ...; } + ## ``` + #@ Attrs -> Attrs + create-checks = { + channels, + src ? user-checks-root, + pkgs ? channels.nixpkgs, + overrides ? {}, + alias ? {}, + }: let + user-checks = snowfall-lib.fs.get-default-nix-files-recursive src; + create-check-metadata = check: let + extra-inputs = + pkgs + // { + inherit channels; + lib = snowfall-lib.internal.system-lib; + inputs = snowfall-lib.flake.without-src user-inputs; + namespace = snowfall-config.namespace; + }; + in { + name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory check); + drv = callPackageWith extra-inputs check {}; + }; + checks-metadata = builtins.map create-check-metadata user-checks; + merge-checks = checks: metadata: + checks + // { + ${metadata.name} = metadata.drv; + }; + checks-without-aliases = foldl merge-checks {} checks-metadata; + aliased-checks = mapAttrs (name: value: checks-without-aliases.${value}) alias; + checks = checks-without-aliases // aliased-checks // overrides; + in + filterPackages pkgs.system checks; + }; +} diff --git a/snowfall-lib/default.nix b/snowfall-lib/default.nix index b59e1d7..aa0d199 100644 --- a/snowfall-lib/default.nix +++ b/snowfall-lib/default.nix @@ -99,6 +99,7 @@ core-inputs: user-options: let attrs = { inherit (user-options) inputs; snowfall-inputs = core-inputs; + namespace = snowfall-config.namespace; lib = merge-shallow [base-lib {${snowfall-config.namespace} = user-lib;}]; }; libs = diff --git a/snowfall-lib/flake/default.nix b/snowfall-lib/flake/default.nix index 391172b..65cbe30 100644 --- a/snowfall-lib/flake/default.nix +++ b/snowfall-lib/flake/default.nix @@ -68,7 +68,7 @@ in rec { "homes" "channels-config" "templates" - "package-namespace" + "checks" "alias" "snowfall" ]; @@ -96,7 +96,7 @@ in rec { }; mkFlake = full-flake-options: let - package-namespace = full-flake-options.package-namespace or snowfall-config.namespace or "internal"; + namespace = snowfall-config.namespace or "internal"; custom-flake-options = flake.without-snowfall-options full-flake-options; alias = full-flake-options.alias or {}; homes = snowfall-lib.home.create-homes (full-flake-options.homes or {}); @@ -125,7 +125,7 @@ in rec { alias = alias.modules.home or {}; }; overlays = snowfall-lib.overlay.create-overlays { - inherit package-namespace; + inherit namespace; extra-overlays = full-flake-options.extra-exported-overlays or {}; }; @@ -136,7 +136,7 @@ in rec { or (const {}); user-outputs = user-outputs-builder channels; packages = snowfall-lib.package.create-packages { - inherit channels package-namespace; + inherit channels namespace; overrides = (full-flake-options.packages or {}) // (user-outputs.packages or {}); alias = alias.packages or {}; }; @@ -145,9 +145,14 @@ in rec { overrides = (full-flake-options.shells or {}) // (user-outputs.devShells or {}); alias = alias.shells or {}; }; + checks = snowfall-lib.check.create-checks { + inherit channels; + overrides = (full-flake-options.checks or {}) // (user-outputs.checks or {}); + alias = alias.checks or {}; + }; outputs = { - inherit packages; + inherit packages checks; devShells = shells; }; @@ -170,13 +175,13 @@ in rec { channelsConfig = full-flake-options.channels-config or {}; channels.nixpkgs.overlaysBuilder = snowfall-lib.overlay.create-overlays-builder { - inherit package-namespace; + inherit namespace; extra-overlays = full-flake-options.overlays or []; }; outputsBuilder = outputs-builder; - _snowfall = { + snowfall = { config = snowfall-config; raw-config = full-flake-options.snowfall or {}; user-lib = snowfall-lib.internal.user-lib; diff --git a/snowfall-lib/fs/default.nix b/snowfall-lib/fs/default.nix index 1a6e060..c91bc1d 100644 --- a/snowfall-lib/fs/default.nix +++ b/snowfall-lib/fs/default.nix @@ -36,7 +36,7 @@ in { ## "/user-source/systems" ## ``` #@ Path -> Path - get-file = path: "${user-inputs.src}/${path}"; + get-file = path: user-inputs.src + "/${path}"; ## Get a file path relative to the user's snowfall directory. ## Example Usage: @@ -48,7 +48,7 @@ in { ## "/user-source/snowfall-dir/systems" ## ``` #@ Path -> Path - get-snowfall-file = path: "${snowfall-config.root}/${path}"; + get-snowfall-file = path: snowfall-config.root + "/${path}"; ## Get a file path relative to the this flake. ## Example Usage: @@ -60,7 +60,7 @@ in { ## "/user-source/systems" ## ``` #@ Path -> Path - internal-get-file = path: "${core-inputs.src}/${path}"; + internal-get-file = path: core-inputs.src + "/${path}"; ## Safely read from a directory if it exists. ## Example Usage: diff --git a/snowfall-lib/home/default.nix b/snowfall-lib/home/default.nix index d53d76e..174d578 100644 --- a/snowfall-lib/home/default.nix +++ b/snowfall-lib/home/default.nix @@ -116,6 +116,7 @@ in { format = "home"; inputs = snowfall-lib.flake.without-src user-inputs; + namespace = snowfall-config.namespace; # NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here. inherit pkgs lib; @@ -345,9 +346,11 @@ in { # NOTE: specialArgs are not propagated by Home-Manager without this. # However, not all specialArgs values can be set when using `_module.args`. - _module.args = - builtins.removeAttrs (users.users.${name}.specialArgs or {}) - ["options" "config" "lib" "pkgs" "specialArgs" "host"]; + _module.args = builtins.removeAttrs ((users.users.${name}.specialArgs or {}) + // { + namespace = snowfall-config.namespace; + }) + ["options" "config" "lib" "pkgs" "specialArgs" "host"]; }; home-manager = { diff --git a/snowfall-lib/internal/default.nix b/snowfall-lib/internal/default.nix index 334f3f1..be5503c 100644 --- a/snowfall-lib/internal/default.nix +++ b/snowfall-lib/internal/default.nix @@ -19,7 +19,7 @@ {snowfall = snowfall-lib;} ]; - user-lib-root = snowfall-lib.fs.get-file "lib"; + user-lib-root = snowfall-lib.fs.get-snowfall-file "lib"; user-lib-modules = snowfall-lib.fs.get-default-nix-files-recursive user-lib-root; user-lib = fix ( @@ -27,6 +27,7 @@ attrs = { inputs = snowfall-lib.flake.without-snowfall-inputs user-inputs; snowfall-inputs = core-inputs; + namespace = snowfall-config.namespace; lib = snowfall-lib.attrs.merge-shallow [ base-lib {internal = user-lib;} diff --git a/snowfall-lib/module/default.nix b/snowfall-lib/module/default.nix index 2a00827..62408ad 100644 --- a/snowfall-lib/module/default.nix +++ b/snowfall-lib/module/default.nix @@ -66,6 +66,7 @@ in { pkgs = user-inputs.self.pkgs.${system}.nixpkgs; inputs = snowfall-lib.flake.without-src user-inputs; + namespace = snowfall-config.namespace; }; imported-user-module = import metadata.path; user-module = diff --git a/snowfall-lib/overlay/default.nix b/snowfall-lib/overlay/default.nix index 4419542..0aa19e9 100644 --- a/snowfall-lib/overlay/default.nix +++ b/snowfall-lib/overlay/default.nix @@ -13,7 +13,7 @@ in { ## Create a flake-utils-plus overlays builder. ## Example Usage: ## ```nix - ## create-overlays { src = ./my-overlays; package-namespace = "my-packages"; } + ## create-overlays { src = ./my-overlays; namespace = "my-packages"; } ## ``` ## Result: ## ```nix @@ -22,7 +22,7 @@ in { #@ Attrs -> Attrs -> [(a -> b -> c)] create-overlays-builder = { src ? user-overlays-root, - package-namespace ? "internal", + namespace ? snowfall-config.namespace, extra-overlays ? [], }: channels: let user-overlays = snowfall-lib.fs.get-default-nix-files-recursive src; @@ -39,11 +39,11 @@ in { user-packages-overlay = final: prev: let user-packages = snowfall-lib.package.create-packages { pkgs = final; - channels = channels; + inherit channels namespace; }; in { - ${package-namespace} = - (prev.${package-namespace} or {}) + ${namespace} = + (prev.${namespace} or {}) // user-packages; }; overlays = @@ -55,7 +55,7 @@ in { ## ## Example Usage: ## ```nix - ## create-overlays { src = ./my-overlays; packages-src = ./my-packages; package-namespace = "my-namespace"; extra-overlays = {}; } + ## create-overlays { src = ./my-overlays; packages-src = ./my-packages; namespace = "my-namespace"; extra-overlays = {}; } ## ``` ## Result: ## ```nix @@ -65,7 +65,7 @@ in { create-overlays = { src ? user-overlays-root, packages-src ? user-packages-root, - package-namespace ? null, + namespace ? snowfall-config.namespace, extra-overlays ? {}, }: let fake-pkgs = { @@ -83,13 +83,14 @@ in { user-packages = snowfall-lib.package.create-packages { pkgs = final; channels = channel-systems.${prev.system}; + inherit namespace; }; in - if package-namespace == null + if namespace == null then user-packages else { - ${package-namespace} = - (prev.${package-namespace} or {}) + ${namespace} = + (prev.${namespace} or {}) // user-packages; }; @@ -102,21 +103,21 @@ in { # Deprecated: Use `inputs.*` instead of referencing the input name directly. user-inputs // { - inherit channels; + inherit channels namespace; inputs = user-inputs; lib = snowfall-lib.internal.system-lib; } ); packages = user-packages-overlay final prev; prev-with-packages = - if package-namespace == null + if namespace == null then prev // packages else prev // { - ${package-namespace} = - (prev.${package-namespace} or {}) - // packages.${package-namespace}; + ${namespace} = + (prev.${namespace} or {}) + // packages.${namespace}; }; user-overlay-packages = user-overlay @@ -152,14 +153,15 @@ in { overlay = final: prev: let channels = channel-systems.${prev.system}; packages = snowfall-lib.package.create-packages { + inherit namespace; channels = channel-systems.${prev.system}; }; in - if package-namespace == null + if namespace == null then {${name} = packages.${name};} else { - ${package-namespace} = - (prev.${package-namespace} or {}) + ${namespace} = + (prev.${namespace} or {}) // {${name} = packages.${name};}; }; in diff --git a/snowfall-lib/package/default.nix b/snowfall-lib/package/default.nix index ce7b218..585708f 100644 --- a/snowfall-lib/package/default.nix +++ b/snowfall-lib/package/default.nix @@ -26,21 +26,21 @@ in { pkgs ? channels.nixpkgs, overrides ? {}, alias ? {}, - package-namespace ? "internal", + namespace ? snowfall-config.namespace, }: let user-packages = snowfall-lib.fs.get-default-nix-files-recursive src; create-package-metadata = package: let namespaced-packages = { - ${package-namespace} = packages-without-aliases; + ${namespace} = packages-without-aliases; }; extra-inputs = pkgs // namespaced-packages // { - inherit channels; + inherit channels namespace; lib = snowfall-lib.internal.system-lib; pkgs = pkgs // namespaced-packages; - inputs = snowfall-lib.flake.without-snowfall-inputs user-inputs; + inputs = user-inputs; }; in { name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory package); diff --git a/snowfall-lib/shell/default.nix b/snowfall-lib/shell/default.nix index 70da329..20668dc 100644 --- a/snowfall-lib/shell/default.nix +++ b/snowfall-lib/shell/default.nix @@ -35,6 +35,7 @@ in { inherit channels; lib = snowfall-lib.internal.system-lib; inputs = snowfall-lib.flake.without-src user-inputs; + namespace = snowfall-config.namespace; }; in { name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory shell); diff --git a/snowfall-lib/system/default.nix b/snowfall-lib/system/default.nix index acb34d5..95b9e9f 100644 --- a/snowfall-lib/system/default.nix +++ b/snowfall-lib/system/default.nix @@ -260,6 +260,7 @@ in { virtual = (get-virtual-system-type target) != ""; inputs = snowfall-lib.flake.without-src user-inputs; + namespace = snowfall-config.namespace; }; }; diff --git a/snowfall-lib/template/default.nix b/snowfall-lib/template/default.nix index 5b1b100..59b2144 100644 --- a/snowfall-lib/template/default.nix +++ b/snowfall-lib/template/default.nix @@ -37,6 +37,8 @@ in { templates // { ${metadata.name} = + (builtins.trace "name: ${metadata.name}") + (builtins.trace "path: ${metadata.path}") (overrides.${metadata.name} or {}) // { inherit (metadata) path; @@ -44,7 +46,8 @@ in { }; templates-without-aliases = foldl merge-templates {} templates-metadata; aliased-templates = mapAttrs (name: value: templates-without-aliases.${value}) alias; - templates = templates-without-aliases // aliased-templates // overrides; + unused-overrides = builtins.removeAttrs overrides (builtins.map (metadata: metadata.name) templates-metadata); + templates = templates-without-aliases // aliased-templates // unused-overrides; in templates; };