Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions modules/aspects/dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ let
}:
let
inherit (OS-HM) OS HM;
context = {
inherit
OS
HM
user
host
;
};
in
{
includes = [
Expand All @@ -69,14 +77,7 @@ let
(statics HM)
(owned OS)
(statics OS)
(parametric {
inherit
OS
HM
user
host
;
} OS)
(parametric.fixedTo context OS)
];
};

Expand Down
57 changes: 35 additions & 22 deletions nix/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ let
isFn = f: (builtins.isFunction f) || (f ? __functor);
canTake = import ./fn-can-take.nix lib;

# creates an aspect that inherits class from fromAspect.
owned =
aspect:
aspect
// {
includes = [ ];
__functor =
self:
# deadnix: skip
{ class, aspect-chain }:
self;
};
empty = {
includes = [ ];
__functor =
self:
# deadnix: skip
{ class, aspect-chain }:
self;
};

# an aspect producing only owned configs
owned = aspect: aspect // empty;

# only static includes from an aspect.
statics =
Expand All @@ -47,14 +46,14 @@ let

applyStatics =
ctx: f:
if isStatic f then
f ctx
else if !isFn f then
if !isFn f then
f
else if isStatic f && ctx ? class then
f ctx
else
{ };

isStatic = canTake {
isStatic = canTake.atLeast {
class = "";
aspect-chain = [ ];
};
Expand All @@ -70,16 +69,30 @@ let
parametric.exactly = funk (lib.flip take.exactly);
parametric.fixedTo = lib.flip parametric.atLeast;
parametric.expands = attrs: funk (ctx: (lib.flip take.atLeast) (ctx // attrs));
parametric.withOwn =
aspect:
aspect
// {
__functor = self: ctx: {
includes = [
(parametric.atLeast self ctx)
(owned self)
({
includes = map (applyStatics ctx) self.includes;
})
];
};
};
parametric.__functor =
self: ctx:
if ctx == true then
self: arg:
if arg == true then
self.atLeast
else if ctx == false then
else if arg == false then
self.exactly
else if isFn ctx then
funk ctx
else if builtins.isAttrs arg then
self.withOwn arg
else
self.fixedTo ctx;
funk arg;

aspects = inputs.flake-aspects.lib lib;

Expand Down
58 changes: 58 additions & 0 deletions templates/examples/modules/_example/ci/parametric-with-owned.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{ den, lib, ... }:
let
# a test module to check context was forwarded
fwdModule.nixos.options.fwd = {
a = strOpt;
b = strOpt;
c = strOpt;
d = strOpt;
};
strOpt = lib.mkOption { type = lib.types.str; };
in
{

den.aspects.rockhopper.includes = [
fwdModule
den.aspects.fwd._.first
];
den.aspects.rockhopper.nixos.fwd.c = "host owned C";

# this is an `atLeast` parametric aspect that also includes
# its owned configs and static (non-functional) includes.
# Usage: just call `parametric` with an aspect.
# or alternatively, set `__functor = den.lib.parametric;`
den.aspects.fwd._.first = den.lib.parametric {
nixos.fwd.a = "First owned A";
includes = [
den.aspects.fwd._.second
{ nixos.fwd.d = "First static includes D"; }
den.aspects.fwd._.never
];
};

# Note that second has named arguments, while first does not.
# the first aspect forwards whatever context it receives.
den.aspects.fwd._.second =
{ host, ... }:
{
nixos.fwd.b = "Second owned B for ${host.name}";
};

den.aspects.fwd._.never =
{ never-matches }:
{
nixos.fwd.a = "Imposibru! should never be included ${never-matches}";
};

perSystem =
{ checkCond, rockhopper, ... }:
{
checks.parametric-fwd = checkCond "forwarding ctx with owned" (
rockhopper.config.fwd.a == "First owned A"
&& rockhopper.config.fwd.b == "Second owned B for rockhopper"
&& rockhopper.config.fwd.c == "host owned C"
&& rockhopper.config.fwd.d == "First static includes D"
);
};

}
Loading