feat: internal namespacing and callPackageWith for better ux

This commit is contained in:
Jake Hamilton 2023-01-15 14:52:52 -08:00
parent 0ba4256dbd
commit 7def9101de
No known key found for this signature in database
GPG key ID: 9762169A1B35EA68
7 changed files with 70 additions and 60 deletions

View file

@ -377,7 +377,7 @@ on `pkgs` and consumers of your flake can use the generated `<your-flake>.overla
# Optionally place all packages under a namespace when used in an overlay.
# Instead of accessing packages with `pkgs.<name>`, your internal packages
# will be available at `pkgs.<namespace>.<name>`.
overlay-package-namespace = "my-namespace";
package-namespace = "my-namespace";
# You can also pass through external packages or dynamically create new ones
# in addition to the ones that `lib` will create from your `packages/` directory.
@ -1327,7 +1327,7 @@ Type: `Attrs -> Attrs -> [(a -> b -> c)]`
Usage:
```nix
create-overlays-builder { src = ./my-overlays; overlay-package-namespace = "my-packages"; extra-overlays = []; }
create-overlays-builder { src = ./my-overlays; package-namespace = "my-packages"; extra-overlays = []; }
```
Result:
@ -1348,7 +1348,7 @@ Usage:
create-overlays {
src = ./my-overlays;
packages-src = ./my-packages;
overlay-package-namespace = "my-namespace";
package-namespace = "my-namespace";
extra-overlays = {
my-example = final: prev: {};
};

View file

@ -8,7 +8,7 @@ user-options:
let
user-inputs = user-options.inputs // { src = user-options.src; };
inherit (core-inputs.nixpkgs.lib) assertMsg fix filterAttrs mergeAttrs fold recursiveUpdate;
inherit (core-inputs.nixpkgs.lib) assertMsg fix filterAttrs mergeAttrs fold recursiveUpdate callPackageWith;
# Recursively merge a list of attribute sets.
# Type: [Attrs] -> Attrs
@ -87,10 +87,10 @@ let
attrs = {
inherit (user-options) inputs;
snowfall-inputs = core-inputs;
lib = merge-shallow [ base-lib user-lib ];
lib = merge-shallow [ base-lib { internal = user-lib; } ];
};
libs = builtins.map
(path: import path attrs)
(path: callPackageWith attrs path { })
user-lib-modules;
in
merge-deep libs
@ -104,6 +104,6 @@ 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

View file

@ -45,7 +45,7 @@ rec {
"hosts"
"channels-config"
"templates"
"overlay-package-namespace"
"package-namespace"
"alias"
];
@ -65,11 +65,11 @@ rec {
builtins.mapAttrs (name: input: input.lib) attrs-with-libs;
in
libs;
};
mkFlake = full-flake-options:
let
package-namespace = full-flake-options.package-namespace or "internal";
custom-flake-options = flake.without-snowfall-options full-flake-options;
alias = full-flake-options.alias or { };
systems = snowfall-lib.system.create-systems (full-flake-options.systems or { });
@ -83,7 +83,7 @@ rec {
alias = alias.modules or { };
};
overlays = snowfall-lib.overlay.create-overlays {
overlay-package-namespace = full-flake-options.overlay-package-namespace or null;
inherit package-namespace;
extra-overlays = full-flake-options.extra-exported-overlays or { };
};
@ -95,7 +95,7 @@ rec {
or (const { });
user-outputs = user-outputs-builder channels;
packages = snowfall-lib.package.create-packages {
inherit channels;
inherit channels package-namespace;
overrides = (full-flake-options.packages or { }) // (user-outputs.packages or { });
alias = alias.packages or { };
};
@ -125,7 +125,7 @@ rec {
channelsConfig = full-flake-options.channels-config or { };
channels.nixpkgs.overlaysBuilder = snowfall-lib.overlay.create-overlays-builder {
overlay-package-namespace = full-flake-options.overlay-package-namespace or null;
package-namespace = full-flake-options.package-namespace or null;
extra-overlays = full-flake-options.overlays or [ ];
};

View file

@ -4,7 +4,7 @@
}:
let
inherit (core-inputs.nixpkgs.lib) assertMsg fix fold filterAttrs;
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);
user-inputs-libs = snowfall-lib.flake.get-libs (snowfall-lib.flake.without-self user-inputs);
@ -27,10 +27,13 @@ let
attrs = {
inputs = snowfall-lib.flake.without-snowfall-inputs user-inputs;
snowfall-inputs = core-inputs;
lib = snowfall-lib.attrs.merge-shallow [ base-lib user-lib ];
lib = snowfall-lib.attrs.merge-shallow [
base-lib
{ internal = user-lib; }
];
};
libs = builtins.map
(path: import path attrs)
(path: callPackageWith attrs path { })
user-lib-modules;
in
snowfall-lib.attrs.merge-deep libs
@ -38,7 +41,7 @@ let
system-lib = snowfall-lib.attrs.merge-shallow [
base-lib
user-lib
{ internal = user-lib; }
];
in
{

View file

@ -13,11 +13,11 @@ in
overlay = {
# Create a flake-utils-plus overlays builder.
# Type: Attrs -> Attrs -> [(a -> b -> c)]
# Usage: create-overlays { src = ./my-overlays; overlay-package-namespace = "my-packages"; }
# Usage: create-overlays { src = ./my-overlays; package-namespace = "my-packages"; }
# result: (channels: [ ... ])
create-overlays-builder =
{ src ? user-overlays-root
, overlay-package-namespace ? null
, package-namespace ? "internal"
, extra-overlays ? [ ]
}: channels:
let
@ -29,18 +29,14 @@ in
pkgs = final;
channels = channels;
};
user-packages-without-default = builtins.removeAttrs
(user-packages) [ "default" ];
in
if overlay-package-namespace == null then
user-packages-without-default
else
{
${overlay-package-namespace} =
(prev.${overlay-package-namespace} or { })
// user-packages-without-default;
};
overlays = [ user-packages-overlay ] ++ extra-overlays ++ (builtins.map create-overlay user-overlays);
{
${package-namespace} =
(prev.${package-namespace} or { })
// user-packages;
};
overlays =
[ user-packages-overlay ] ++ extra-overlays ++ (builtins.map create-overlay user-overlays);
in
overlays;
@ -48,12 +44,12 @@ in
# Adapted from flake-utils-plus:
# https://github.com/gytis-ivaskevicius/flake-utils-plus/blob/2bf0f91643c2e5ae38c1b26893ac2927ac9bd82a/lib/exportOverlays.nix
# Type: Attrs -> Attrs
# Usage: create-overlays { src = ./my-overlays; packages-src = ./my-packages; overlay-package-namespace = "my-namespace"; extra-overlays = {}; }
# Usage: create-overlays { src = ./my-overlays; packages-src = ./my-packages; package-namespace = "my-namespace"; extra-overlays = {}; }
# result: { default = final: prev: ...; some-overlay = final: prev: ...; }
create-overlays =
{ src ? user-overlays-root
, packages-src ? user-packages-root
, overlay-package-namespace ? null
, package-namespace ? null
, extra-overlays ? { }
}:
let
@ -75,12 +71,12 @@ in
channels = channel-systems.${prev.system};
};
in
if overlay-package-namespace == null then
if package-namespace == null then
user-packages
else
{
${overlay-package-namespace} =
(prev.${overlay-package-namespace} or { })
${package-namespace} =
(prev.${package-namespace} or { })
// user-packages;
};
@ -93,13 +89,13 @@ in
user-overlay = import file (user-inputs // { inherit channels; });
packages = user-packages-overlay final prev;
prev-with-packages =
if overlay-package-namespace == null then
if package-namespace == null then
prev // packages
else
prev // {
${overlay-package-namespace} =
(prev.${overlay-package-namespace} or { })
// packages.${overlay-package-namespace};
${package-namespace} =
(prev.${package-namespace} or { })
// packages.${package-namespace};
};
user-overlay-packages =
user-overlay
@ -140,12 +136,12 @@ in
channels = channel-systems.${prev.system};
};
in
if overlay-package-namespace == null then
if package-namespace == null then
{ ${name} = packages.${name}; }
else
{
${overlay-package-namespace} =
(prev.${overlay-package-namespace} or { })
${package-namespace} =
(prev.${package-namespace} or { })
// { ${name} = packages.${name}; };
};
in

View file

@ -4,13 +4,13 @@
}:
let
inherit (core-inputs.flake-utils-plus.lib) filterPackages;
inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs;
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
{
package = {
package = rec {
# Create flake output packages.
# Type: Attrs -> Attrs
# Usage: create-packages { inherit channels; src = ./my-packages; overrides = { inherit another-package; }; alias.default = "another-package"; }
@ -21,17 +21,26 @@ in
, pkgs ? channels.nixpkgs
, overrides ? { }
, alias ? { }
, package-namespace ? "internal"
}:
let
user-packages = snowfall-lib.fs.get-default-nix-files-recursive src;
create-package-metadata = package: {
name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory package);
drv = pkgs.callPackage package {
inherit channels;
lib = snowfall-lib.internal.system-lib;
inputs = snowfall-lib.flake.without-src user-inputs;
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 = callPackageWith extra-inputs package { };
};
};
packages-metadata = builtins.map create-package-metadata user-packages;
merge-packages = packages: metadata:
packages // {

View file

@ -5,7 +5,7 @@
let
inherit (core-inputs.flake-utils-plus.lib) filterPackages;
inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs;
inherit (core-inputs.nixpkgs.lib) assertMsg foldl mapAttrs callPackageWith;
user-shells-root = snowfall-lib.fs.get-snowfall-file "shells";
in
@ -18,19 +18,23 @@ in
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:
{
name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory shell);
drv = channels.nixpkgs.callPackage 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 { };
};
shells-metadata = builtins.map create-shell-metadata user-shells;
merge-shells = shells: metadata:
@ -41,8 +45,6 @@ in
aliased-shells = mapAttrs (name: value: shells-without-aliases.${value}) alias;
shells = shells-without-aliases // aliased-shells // overrides;
in
filterPackages
channels.nixpkgs.system
shells;
filterPackages pkgs.system shells;
};
}