mirror of
https://github.com/Gerg-L/nixos.git
synced 2025-12-10 00:43:56 -05:00
lib: add overlay
This commit is contained in:
parent
8fcc7d718e
commit
b92568fbde
2 changed files with 138 additions and 134 deletions
135
lib/_default.nix
135
lib/_default.nix
|
|
@ -1,141 +1,8 @@
|
||||||
{
|
{
|
||||||
unstable,
|
unstable,
|
||||||
self,
|
|
||||||
...
|
...
|
||||||
}@inputs:
|
}@inputs:
|
||||||
let
|
let
|
||||||
inherit (unstable) lib;
|
inherit (unstable) lib;
|
||||||
in
|
in
|
||||||
lib.fix (myLib: {
|
lib.fix (self: (import ./overlay.nix inputs self lib))
|
||||||
wrench = lib.flip lib.pipe;
|
|
||||||
|
|
||||||
needsSystem = lib.flip builtins.elem [
|
|
||||||
"apps"
|
|
||||||
"checks"
|
|
||||||
"defaultPackage"
|
|
||||||
"devShell"
|
|
||||||
"devShells"
|
|
||||||
"formatter"
|
|
||||||
"legacyPackages"
|
|
||||||
"packages"
|
|
||||||
];
|
|
||||||
|
|
||||||
constructInputs' =
|
|
||||||
system:
|
|
||||||
myLib.wrench [
|
|
||||||
(lib.filterAttrs (_: lib.isType "flake"))
|
|
||||||
(lib.mapAttrs (
|
|
||||||
_: lib.mapAttrs (name: value: if myLib.needsSystem name then value.${system} else value)
|
|
||||||
))
|
|
||||||
];
|
|
||||||
|
|
||||||
listNixFilesRecursive = myLib.wrench [
|
|
||||||
builtins.unsafeDiscardStringContext
|
|
||||||
lib.filesystem.listFilesRecursive
|
|
||||||
(builtins.filter (x: !lib.hasPrefix "_" (builtins.baseNameOf x) && lib.hasSuffix ".nix" x))
|
|
||||||
];
|
|
||||||
|
|
||||||
addSchizophreniaToModule =
|
|
||||||
x:
|
|
||||||
let
|
|
||||||
# the imported module
|
|
||||||
imported = import x;
|
|
||||||
in
|
|
||||||
/*
|
|
||||||
If the module isn't a function then
|
|
||||||
it doesn't need arguments and error
|
|
||||||
message locations will function correctly
|
|
||||||
*/
|
|
||||||
if !lib.isFunction imported then
|
|
||||||
x
|
|
||||||
else
|
|
||||||
let
|
|
||||||
# all arguments defined in the module
|
|
||||||
funcArgs = lib.functionArgs imported;
|
|
||||||
/*
|
|
||||||
The names of all arguments which will be
|
|
||||||
available to be inserted into the module arguments
|
|
||||||
*/
|
|
||||||
argNames = builtins.attrNames inputs ++ [
|
|
||||||
"inputs"
|
|
||||||
"inputs'"
|
|
||||||
"self'"
|
|
||||||
"_dir"
|
|
||||||
];
|
|
||||||
|
|
||||||
/*
|
|
||||||
arguments to be passed minus
|
|
||||||
per system attributes
|
|
||||||
for example flake-parts-esque inputs'
|
|
||||||
*/
|
|
||||||
argsPre = {
|
|
||||||
inherit inputs self;
|
|
||||||
/*
|
|
||||||
_dir is the "self" derived
|
|
||||||
path to the directory containing the module
|
|
||||||
*/
|
|
||||||
_dir = builtins.dirOf x;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
arguments which will be inserted
|
|
||||||
set to the before per-system values
|
|
||||||
*/
|
|
||||||
providedArgs = lib.pipe funcArgs [
|
|
||||||
(lib.filterAttrs (n: _: builtins.elem n argNames))
|
|
||||||
(lib.mapAttrs (n: _: argsPre.${n} or { }))
|
|
||||||
];
|
|
||||||
|
|
||||||
/*
|
|
||||||
arguments which the module system
|
|
||||||
not provided here. either to be
|
|
||||||
provided by the module system or invalid
|
|
||||||
*/
|
|
||||||
neededArgs = lib.filterAttrs (n: _: !builtins.elem n argNames) funcArgs;
|
|
||||||
in
|
|
||||||
{
|
|
||||||
__functionArgs = neededArgs // {
|
|
||||||
/*
|
|
||||||
always require pkgs to be passed
|
|
||||||
to derive system from pkgs.stdenv.system
|
|
||||||
*/
|
|
||||||
pkgs = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
__functor =
|
|
||||||
/*
|
|
||||||
args is specialArgs + _module.args which are needed
|
|
||||||
and always pkgs
|
|
||||||
*/
|
|
||||||
_: args:
|
|
||||||
imported (
|
|
||||||
/*
|
|
||||||
take module system provided arguments
|
|
||||||
filter them so only the required ones are passed
|
|
||||||
*/
|
|
||||||
(lib.filterAttrs (n: _: neededArgs ? ${n}) args)
|
|
||||||
# add needed arguments
|
|
||||||
// (
|
|
||||||
providedArgs
|
|
||||||
# add system dependent arguments
|
|
||||||
// (
|
|
||||||
let
|
|
||||||
inputs' = myLib.constructInputs' args.pkgs.stdenv.system inputs;
|
|
||||||
|
|
||||||
actuallyAllArgs = inputs' // {
|
|
||||||
inherit inputs';
|
|
||||||
self' = inputs'.self;
|
|
||||||
inherit (inputs) self;
|
|
||||||
};
|
|
||||||
in
|
|
||||||
lib.filterAttrs (n: _: providedArgs ? ${n}) actuallyAllArgs
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
# add _file to the final module attribute set
|
|
||||||
// {
|
|
||||||
_file = x;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
|
||||||
137
lib/overlay.nix
Normal file
137
lib/overlay.nix
Normal file
|
|
@ -0,0 +1,137 @@
|
||||||
|
{ self, ... }@inputs:
|
||||||
|
|
||||||
|
myLib: lib: {
|
||||||
|
overlay = import ./overlay.nix inputs;
|
||||||
|
|
||||||
|
wrench = lib.flip lib.pipe;
|
||||||
|
|
||||||
|
needsSystem = lib.flip builtins.elem [
|
||||||
|
"apps"
|
||||||
|
"checks"
|
||||||
|
"defaultPackage"
|
||||||
|
"devShell"
|
||||||
|
"devShells"
|
||||||
|
"formatter"
|
||||||
|
"legacyPackages"
|
||||||
|
"packages"
|
||||||
|
];
|
||||||
|
|
||||||
|
constructInputs' =
|
||||||
|
system:
|
||||||
|
myLib.wrench [
|
||||||
|
(lib.filterAttrs (_: lib.isType "flake"))
|
||||||
|
(lib.mapAttrs (
|
||||||
|
_: lib.mapAttrs (name: value: if myLib.needsSystem name then value.${system} else value)
|
||||||
|
))
|
||||||
|
];
|
||||||
|
|
||||||
|
listNixFilesRecursive = myLib.wrench [
|
||||||
|
builtins.unsafeDiscardStringContext
|
||||||
|
lib.filesystem.listFilesRecursive
|
||||||
|
(builtins.filter (x: !lib.hasPrefix "_" (builtins.baseNameOf x) && lib.hasSuffix ".nix" x))
|
||||||
|
];
|
||||||
|
|
||||||
|
addSchizophreniaToModule =
|
||||||
|
x:
|
||||||
|
let
|
||||||
|
# the imported module
|
||||||
|
imported = import x;
|
||||||
|
in
|
||||||
|
/*
|
||||||
|
If the module isn't a function then
|
||||||
|
it doesn't need arguments and error
|
||||||
|
message locations will function correctly
|
||||||
|
*/
|
||||||
|
if !lib.isFunction imported then
|
||||||
|
x
|
||||||
|
else
|
||||||
|
let
|
||||||
|
# all arguments defined in the module
|
||||||
|
funcArgs = lib.functionArgs imported;
|
||||||
|
/*
|
||||||
|
The names of all arguments which will be
|
||||||
|
available to be inserted into the module arguments
|
||||||
|
*/
|
||||||
|
argNames = builtins.attrNames inputs ++ [
|
||||||
|
"inputs"
|
||||||
|
"inputs'"
|
||||||
|
"self'"
|
||||||
|
"_dir"
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
arguments to be passed minus
|
||||||
|
per system attributes
|
||||||
|
for example flake-parts-esque inputs'
|
||||||
|
*/
|
||||||
|
argsPre = {
|
||||||
|
inherit inputs self;
|
||||||
|
/*
|
||||||
|
_dir is the "self" derived
|
||||||
|
path to the directory containing the module
|
||||||
|
*/
|
||||||
|
_dir = builtins.dirOf x;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
arguments which will be inserted
|
||||||
|
set to the before per-system values
|
||||||
|
*/
|
||||||
|
providedArgs = lib.pipe funcArgs [
|
||||||
|
(lib.filterAttrs (n: _: builtins.elem n argNames))
|
||||||
|
(lib.mapAttrs (n: _: argsPre.${n} or { }))
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
arguments which the module system
|
||||||
|
not provided here. either to be
|
||||||
|
provided by the module system or invalid
|
||||||
|
*/
|
||||||
|
neededArgs = lib.filterAttrs (n: _: !builtins.elem n argNames) funcArgs;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
__functionArgs = neededArgs // {
|
||||||
|
/*
|
||||||
|
always require pkgs to be passed
|
||||||
|
to derive system from pkgs.stdenv.system
|
||||||
|
*/
|
||||||
|
pkgs = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
__functor =
|
||||||
|
/*
|
||||||
|
args is specialArgs + _module.args which are needed
|
||||||
|
and always pkgs
|
||||||
|
*/
|
||||||
|
_: args:
|
||||||
|
imported (
|
||||||
|
/*
|
||||||
|
take module system provided arguments
|
||||||
|
filter them so only the required ones are passed
|
||||||
|
*/
|
||||||
|
(lib.filterAttrs (n: _: neededArgs ? ${n}) args)
|
||||||
|
# add needed arguments
|
||||||
|
// (
|
||||||
|
providedArgs
|
||||||
|
# add system dependent arguments
|
||||||
|
// (
|
||||||
|
let
|
||||||
|
inputs' = myLib.constructInputs' args.pkgs.stdenv.system inputs;
|
||||||
|
|
||||||
|
actuallyAllArgs = inputs' // {
|
||||||
|
inherit inputs';
|
||||||
|
self' = inputs'.self;
|
||||||
|
inherit (inputs) self;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
lib.filterAttrs (n: _: providedArgs ? ${n}) actuallyAllArgs
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# add _file to the final module attribute set
|
||||||
|
// {
|
||||||
|
_file = x;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue