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. # Optionally place all packages under a namespace when used in an overlay.
# Instead of accessing packages with `pkgs.<name>`, your internal packages # Instead of accessing packages with `pkgs.<name>`, your internal packages
# will be available at `pkgs.<namespace>.<name>`. # 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 # 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. # in addition to the ones that `lib` will create from your `packages/` directory.
@ -1327,7 +1327,7 @@ Type: `Attrs -> Attrs -> [(a -> b -> c)]`
Usage: Usage:
```nix ```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: Result:
@ -1348,7 +1348,7 @@ Usage:
create-overlays { create-overlays {
src = ./my-overlays; src = ./my-overlays;
packages-src = ./my-packages; packages-src = ./my-packages;
overlay-package-namespace = "my-namespace"; package-namespace = "my-namespace";
extra-overlays = { extra-overlays = {
my-example = final: prev: {}; my-example = final: prev: {};
}; };

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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