From b2e636407591bfe7a1f9a4ff50e282ed51ce7b59 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Wed, 22 May 2024 19:03:29 -0700 Subject: [PATCH] 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; };