Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

da/type path resolver #127

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions modules/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let
_module.args.baseModules = modules;
_module.args.pkgsPath = lib.mkDefault pkgs.path;
_module.args.pkgs = lib.mkDefault pkgs;
_module.args._devshelltoml = config.lib._tomlfile or null; # set by importTOML
};
};
in
Expand Down
2 changes: 1 addition & 1 deletion nix/importTOML.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ in
{
_file = file;
imports = map importModule (data.imports or [ ]);
config = builtins.removeAttrs data [ "imports" ];
config = (builtins.removeAttrs data [ "imports" ]) // { lib._tomlfile = file; };
}
37 changes: 37 additions & 0 deletions nix/types.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ lib }:

with lib.types; {

/**
Synopsis: maybeResolveRel <tomlfilepath> <string|path>

If it's a string, returns an absolute path transforming relative
paths with regard to the tomlfile attribute, first. Or
transparently passes through path types, otherwise.
Comment on lines +8 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs update before merging.


Use for fields that can define relative paths in devshell TOML files.
**/
maybeResolveRel = tomlfile: let
tomldir = builtins.dirOf tomlfile;
in obj:
# Not a toml file: return as-is
if (file == null) then
obj
# It must be a string
else if (!(builtins.isString obj)) then
# Never happens untill nix gains some sort type
# caster for importTOML: prepare for day X.
builtins.throw "${obj} defined in ${tomlfile} is not a string."
# It looks like an absolute path: type cast into a path type
else if (lib.strings.hasPrefix "/" obj) then
/. + obj
# It looks like an explicit relpath: strip "." to conform to the builtin path type caster
else if (lib.strings.hasPrefix "./" obj) || then
/. + (tomldir + (lib.strings.removePrefix "." obj))
# It is treated as an implicit relpat: prefix with "/" to conform to ...
else
/. + (tomldir + "/" obj)
;

pathType = tomlfile: o: coercedTo path maybeResolveRel tomlfile o;
}