Skip to content

Commit

Permalink
treefmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dermetfan committed Sep 30, 2022
1 parent acaf7d7 commit 1a041db
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 101 deletions.
214 changes: 117 additions & 97 deletions nix/lib.nix
Expand Up @@ -145,42 +145,45 @@ in rec {
};

/*
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
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
g newPath value
);
in recurse [];
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.
*/
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 cond == null
then p: v: !pred p v
else cond
)
(
p: v:
if pred p v
then p
else null
)
attrs
)
attrs
);

findAttrsRecursive = findAttrsRecursiveCond null;
Expand All @@ -189,106 +192,123 @@ in rec {
findFlattenAttrsRecursiveCond = cond: pred: mkName: attrs:
__listToAttrs (
map
(path: nameValuePair
(
path:
nameValuePair
(mkName path)
(getAttrFromPath path attrs)
)
(findAttrsRecursiveCond cond pred attrs)
)
(findAttrsRecursiveCond cond pred attrs)
);

/*
Given an arbitrarily deeply nested attrset of derivations,
returns an attrset of tasks that build each derivation.
The `mkName` function receives the path to each attribute
as its first and only parameter.
Read about `findAttrsRecursiveCond` for details about the
`cond` recursion function.
The returned tasks have extra module options called
`drvToTask.{attrPath,installable}`, where
`attrPath` is a read-only list of strings and
`installable` is an UNDEFINED string that will be passed to `nix build`.
Make sure to import the returned task in another module that sets `installable`!
*/
Given an arbitrarily deeply nested attrset of derivations,
returns an attrset of tasks that build each derivation.
The `mkName` function receives the path to each attribute
as its first and only parameter.
Read about `findAttrsRecursiveCond` for details about the
`cond` recursion function.
The returned tasks have extra module options called
`drvToTask.{attrPath,installable}`, where
`attrPath` is a read-only list of strings and
`installable` is an UNDEFINED string that will be passed to `nix build`.
Make sure to import the returned task in another module that sets `installable`!
*/
drvToTaskRecursiveCond = cond: mkName: attrs:
__listToAttrs (
map
(path: nameValuePair
(
path:
nameValuePair
(mkName path)
(let
v = getAttrFromPath path attrs;
in {config, lib, ...}: {
options.drvToTask = with lib; {
attrPath = mkOption {
type = with types; listOf str;
default = path;
readOnly = true;
};
in
{
config,
lib,
...
}: {
options.drvToTask = with lib; {
attrPath = mkOption {
type = with types; listOf str;
default = path;
readOnly = true;
};

installable = mkOption {
type = types.str;
installable = mkOption {
type = types.str;
};
};
};

config = {
preset.nix.enable = true;
config = {
preset.nix.enable = true;

command.text = ''
attr=${lib.escapeShellArg config.drvToTask.installable}
echo Building "$attr"…
echo -e '\tdrv: '${lib.escapeShellArg (__unsafeDiscardStringContext v.drvPath)}
echo -e '\tout: '${lib.escapeShellArg (__unsafeDiscardStringContext v.outPath)}
nix build -L "$attr"
'';
};
})
)
(findAttrsRecursiveCond cond (_: isDerivation) attrs)
command.text = ''
attr=${lib.escapeShellArg config.drvToTask.installable}
echo Building "$attr"…
echo -e '\tdrv: '${lib.escapeShellArg (__unsafeDiscardStringContext v.drvPath)}
echo -e '\tout: '${lib.escapeShellArg (__unsafeDiscardStringContext v.outPath)}
nix build -L "$attr"
'';
};
})
)
(findAttrsRecursiveCond cond (_: isDerivation) attrs)
);

drvToTaskRecursive = drvToTaskRecursiveCond null;

/*
Given a flake output's path as a list of strings and an evaluated flake,
returns an attrset of tasks for every derivation recursively found.
The returned tasks have an extra module option called `flakeOutputTask.flakeUrl`
that defaults to `.` but can be changed.
Also see `drvToTaskRecursive` for further information about the module
that is returned for each task.
*/
flakeOutputTasks = path: flake:
let
mkFlakeFragement = p: showAttrPath (path ++ p);
in __mapAttrs
(_: task: {config, lib, ...}: {
imports = [task];
Given a flake output's path as a list of strings and an evaluated flake,
returns an attrset of tasks for every derivation recursively found.
The returned tasks have an extra module option called `flakeOutputTask.flakeUrl`
that defaults to `.` but can be changed.
Also see `drvToTaskRecursive` for further information about the module
that is returned for each task.
*/
flakeOutputTasks = path: flake: let
mkFlakeFragement = p: showAttrPath (path ++ p);
in
__mapAttrs
(_: task: {
config,
lib,
...
}: {
imports = [task];

options.flakeOutputTask.flakeUrl = with lib; mkOption {
options.flakeOutputTask.flakeUrl = with lib;
mkOption {
type = types.str;
default = ".";
};

config.drvToTask.installable =
"${config.flakeOutputTask.flakeUrl}#${mkFlakeFragement config.drvToTask.attrPath}";
})
(
drvToTaskRecursive
mkFlakeFragement
(getAttrFromPath path flake.outputs)
);
config.drvToTask.installable = "${config.flakeOutputTask.flakeUrl}#${mkFlakeFragement config.drvToTask.attrPath}";
})
(
drvToTaskRecursive
mkFlakeFragement
(getAttrFromPath path flake.outputs)
);

/*
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: tasks: taskNames: __listToAttrs (
imap0 (i: taskName: nameValuePair
(prefix + taskName)
({...}: {
imports = [tasks.${taskName}];
after = optional (i > 0) (
prefix + __elemAt taskNames (i - 1)
);
})
) taskNames
);
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: tasks: taskNames:
__listToAttrs (
imap0 (
i: taskName:
nameValuePair
(prefix + taskName)
({...}: {
imports = [tasks.${taskName}];
after = optional (i > 0) (
prefix + __elemAt taskNames (i - 1)
);
})
)
taskNames
);
}
9 changes: 5 additions & 4 deletions nix/module.nix
Expand Up @@ -1261,10 +1261,11 @@ in {
(
# Remove read-only options to avoid an evaluation error.
filterOptionValues
(path: option: value:
if option == null
then false
else !option.readOnly or false
(
path: option: value:
if option == null
then false
else !option.readOnly or false
)
(taskType.getSubOptions [])
task
Expand Down

0 comments on commit 1a041db

Please sign in to comment.