Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dermetfan committed Sep 30, 2022
1 parent bda93c6 commit 4fd549f
Showing 1 changed file with 98 additions and 3 deletions.
101 changes: 98 additions & 3 deletions nix/lib.nix
@@ -1,9 +1,9 @@
inputs: let
inherit (inputs.nixpkgs.lib) evalModules filterAttrs;
inherit (inputs.nixpkgs.lib) evalModules filterAttrs collect isDerivation nameValuePair getAttrFromPath imap0 optional;
inherit (builtins) mapAttrs;

augmentPkgs = import ./augmentPkgs.nix inputs;

in rec {
evalAction = {
tasks,
rootDir ? null,
Expand Down Expand Up @@ -92,7 +92,7 @@ inputs: let
];
})
.config;
in rec {

ciceroFromStd = {
actions,
tasks,
Expand Down Expand Up @@ -143,4 +143,99 @@ in rec {
tullia = tulliaStd.tullia.${system};
cicero = tulliaStd.cicero.${system};
};

/*
Like `mapAttrsRecursiveCond` from nixpkgs
but the condition and mapping functions
take the attribute path as their first parameter.
*/
mapAttrsRecursiveCondWithPath = cond: f:
let
recurse = path: __mapAttrs (name: value:
let
newPath = path ++ [name];
g =
if __isAttrs value && cond newPath value
then recurse
else f;
in g newPath value
);
in recurse [];

/*
Returns the paths to values that satisfy the given predicate in the given attrset.
The predicate and recursion predicate functions take path and value as their parameters.
If the recursion prediate function is null, it defaults to the negated predicate.
*/
findAttrsRecursiveCond = cond: pred: attrs:
collect __isList (
mapAttrsRecursiveCondWithPath
(
if cond == null
then p: v: !pred p v
else cond
)
(p: v:
if pred p v
then p
else null
)
attrs
);

findAttrsRecursive = findAttrsRecursiveCond null;

# Returns a new attrset from the result of `findAttrsRecursiveCond` using the given naming function.
findFlattenAttrsRecursiveCond = mkName: cond: pred: attrs:
__listToAttrs
(path: nameValuePair
(mkName path)
(getAttrFromPath path attrs)
)
(findAttrsRecursiveCond cond pred attrs);

drvToTaskRecursiveCond = cond: attrs:
__mapAttrs
(k: v: {config, lib, ...} @ args: {
options.command.flakeUrl = lib.mkOption {
type = lib.types.str;
default = ".";
};

config = {
preset.nix.enable = true;

command.text = ''
attr=${lib.escapeShellArg config.command.flakeUrl}#${lib.escapeShellArg k}
echo Building "$attr"…
echo -e '\tdrv: '${lib.escapeShellArg v.drvPath}
echo -e '\tout: '${lib.escapeShellArg v.outPath}
nix build -L "$attr"
'';
};
})
(
findFlattenAttrsRecursiveCond
showAttrPath
cond
(_: isDerivation)
);

drvToTaskRecursive = drvToTaskRecursiveCond null;

/*
Returns attrset of tullia tasks named with the given prefix
that run the corresponding task and depend on each other in the order given.
*/
taskSequence = prefix: taskNames: __listToAttrs (
imap0 (i: taskName: nameValuePair
(prefix + taskName)
({...}: {
imports = [tasks.${taskName}];
after = optional (i > 0) (
prefix + __elemAt taskNames (i - 1)
);
})
) taskNames
);
}

0 comments on commit 4fd549f

Please sign in to comment.