Skip to content

Commit

Permalink
feat: namespace arg, inputs for packages and overlays, get lib from r…
Browse files Browse the repository at this point in the history
…oot, add checks
  • Loading branch information
jakehamilton committed May 23, 2024
1 parent d6b7669 commit b2e6364
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 38 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
aarch64-darwin = inputs.nixpkgs.legacyPackages.aarch64-darwin.alejandra;
};

_snowfall = rec {
snowfall = rec {
raw-config = config;

config = {
Expand Down
56 changes: 56 additions & 0 deletions snowfall-lib/checks/default.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}
1 change: 1 addition & 0 deletions snowfall-lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
19 changes: 12 additions & 7 deletions snowfall-lib/flake/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ in rec {
"homes"
"channels-config"
"templates"
"package-namespace"
"checks"
"alias"
"snowfall"
];
Expand Down Expand Up @@ -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 {});
Expand Down Expand Up @@ -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 {};
};

Expand All @@ -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 {};
};
Expand All @@ -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;
};
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions snowfall-lib/fs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions snowfall-lib/home/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion snowfall-lib/internal/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
{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 (
user-lib: let
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;}
Expand Down
1 change: 1 addition & 0 deletions snowfall-lib/module/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
38 changes: 20 additions & 18 deletions snowfall-lib/overlay/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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 =
Expand All @@ -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
Expand All @@ -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 = {
Expand All @@ -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;
};

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions snowfall-lib/package/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions snowfall-lib/shell/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions snowfall-lib/system/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ in {

virtual = (get-virtual-system-type target) != "";
inputs = snowfall-lib.flake.without-src user-inputs;
namespace = snowfall-config.namespace;
};
};

Expand Down
5 changes: 4 additions & 1 deletion snowfall-lib/template/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ in {
templates
// {
${metadata.name} =
(builtins.trace "name: ${metadata.name}")
(builtins.trace "path: ${metadata.path}")

This comment has been minimized.

Copy link
@msfjarvis

msfjarvis May 24, 2024

Contributor

Should these be here? This prints out trace messages during nix flake show and nix flake check.

This comment has been minimized.

Copy link
@jakehamilton

jakehamilton May 24, 2024

Author Member

Oof. Yeah those should've been removed after my debugging. Sorry about that, I need to set up a linter...

This comment has been minimized.

Copy link
@msfjarvis

msfjarvis May 24, 2024

Contributor

Removed it here #94

(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;
unused-overrides = builtins.removeAttrs overrides (builtins.map (metadata: metadata.name) templates-metadata);
templates = templates-without-aliases // aliased-templates // unused-overrides;
in
templates;
};
Expand Down

0 comments on commit b2e6364

Please sign in to comment.