2021-03-15 22:32:16 +01:00
|
|
|
{ depot, lib, ... }:
|
|
|
|
|
|
|
|
let
|
2021-04-20 10:18:28 +02:00
|
|
|
/* Get the basename of a store path without
|
|
|
|
the leading hash.
|
|
|
|
|
|
|
|
Type: (path | drv | string) -> string
|
|
|
|
|
|
|
|
Example:
|
|
|
|
storePathName ./foo.c
|
|
|
|
=> "foo.c"
|
|
|
|
|
|
|
|
storePathName (writeText "foo.c" "int main() { return 0; }")
|
|
|
|
=> "foo.c"
|
|
|
|
|
|
|
|
storePathName "${hello}/bin/hello"
|
|
|
|
=> "hello"
|
|
|
|
*/
|
|
|
|
storePathName = p:
|
|
|
|
if lib.isDerivation p
|
|
|
|
then p.name
|
|
|
|
else if builtins.isPath p
|
|
|
|
then builtins.baseNameOf p
|
2021-12-16 17:28:21 +01:00
|
|
|
else if builtins.isString p || (builtins.isAttrs p && (p ? outPath || p ? __toString))
|
2021-04-20 10:18:28 +02:00
|
|
|
then
|
|
|
|
let
|
2021-12-16 17:28:21 +01:00
|
|
|
strPath = toString p;
|
2021-04-20 10:18:28 +02:00
|
|
|
# strip leading storeDir and trailing slashes
|
|
|
|
noStoreDir = lib.removeSuffix "/"
|
2021-12-16 17:28:21 +01:00
|
|
|
(lib.removePrefix "${builtins.storeDir}/" strPath);
|
2021-04-20 10:18:28 +02:00
|
|
|
# a basename of a child of a store path isn't really
|
|
|
|
# referring to a store path, so removing the string
|
|
|
|
# context is safe (e. g. "hello" for "${hello}/bin/hello").
|
|
|
|
basename = builtins.unsafeDiscardStringContext
|
2021-12-16 17:28:21 +01:00
|
|
|
(builtins.baseNameOf strPath);
|
2021-04-20 10:18:28 +02:00
|
|
|
in
|
2022-01-30 17:06:58 +01:00
|
|
|
# If p is a direct child of storeDir, we need to remove
|
2021-04-20 10:18:28 +02:00
|
|
|
# the leading hash as well to make sure that:
|
|
|
|
# `storePathName drv == storePathName (toString drv)`.
|
2022-01-30 17:06:58 +01:00
|
|
|
if noStoreDir == basename
|
|
|
|
then builtins.substring 33 (-1) basename
|
|
|
|
else basename
|
2021-04-20 10:18:28 +02:00
|
|
|
else builtins.throw "Don't know how to get (base)name of "
|
2022-01-30 17:06:58 +01:00
|
|
|
+ lib.generators.toPretty { } p;
|
2021-04-20 10:18:28 +02:00
|
|
|
|
2022-05-28 13:49:34 +02:00
|
|
|
/* Retrieves the drvPath attribute from a given derivation, but ensures that
|
|
|
|
the resulting string only depends on the `.drv` file in the nix store and
|
|
|
|
not on its realised outputs as well.
|
|
|
|
|
|
|
|
Type: drv -> string
|
|
|
|
*/
|
|
|
|
onlyDrvPath = drv:
|
|
|
|
let
|
|
|
|
inherit (drv) drvPath;
|
|
|
|
unsafeDrvPath = builtins.unsafeDiscardStringContext drvPath;
|
|
|
|
in
|
|
|
|
builtins.appendContext unsafeDrvPath {
|
|
|
|
${unsafeDrvPath} = { path = true; };
|
|
|
|
};
|
|
|
|
|
2021-09-11 18:47:18 +02:00
|
|
|
/* Query the type of a path exposing the same information as would be by
|
|
|
|
`builtins.readDir`, but for a single, specific target path.
|
2021-08-18 11:38:59 +02:00
|
|
|
|
2021-09-11 18:47:18 +02:00
|
|
|
The information is returned as a tagged value, i. e. an attribute set with
|
|
|
|
exactly one attribute where the type of the path is encoded in the name
|
|
|
|
of the single attribute. The allowed tags and values are as follows:
|
|
|
|
|
|
|
|
* `regular`: is a regular file, always `true` if returned
|
|
|
|
* `directory`: is a directory, always `true` if returned
|
|
|
|
* `missing`: path does not exist, always `true` if returned
|
2022-05-28 13:28:37 +02:00
|
|
|
* `symlink`: path is a symlink, always `true` if returned
|
2021-09-11 18:47:18 +02:00
|
|
|
|
|
|
|
Type: path(-like) -> tag
|
|
|
|
|
|
|
|
`tag` refers to the attribute set format of `//nix/tag`.
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
pathType ./foo.c
|
2021-09-11 18:47:18 +02:00
|
|
|
=> { regular = true; }
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
pathType /home/lukas
|
2021-09-11 18:47:18 +02:00
|
|
|
=> { directory = true; }
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
pathType ./result
|
2022-05-28 13:28:37 +02:00
|
|
|
=> { symlink = true; }
|
2021-09-11 18:47:18 +02:00
|
|
|
|
|
|
|
pathType ./link-to-file
|
2022-05-28 13:28:37 +02:00
|
|
|
=> { symlink = true; }
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
pathType /does/not/exist
|
2021-09-11 18:47:18 +02:00
|
|
|
=> { missing = true; }
|
|
|
|
|
|
|
|
# Check if a path exists
|
|
|
|
!(pathType /file ? missing)
|
|
|
|
|
|
|
|
# Check if a path is a directory or a symlink to a directory
|
|
|
|
# A handy shorthand for this is provided as `realPathIsDirectory`.
|
|
|
|
pathType /path ? directory || (pathType /path).symlink or null == "directory"
|
|
|
|
|
|
|
|
# Match on the result using //nix/tag
|
|
|
|
nix.tag.match (nix.utils.pathType ./result) {
|
2022-05-28 13:28:37 +02:00
|
|
|
symlink = _: "symlink";
|
2021-09-11 18:47:18 +02:00
|
|
|
directory = _: "directory";
|
|
|
|
regular = _: "regular";
|
|
|
|
missing = _: "path does not exist";
|
|
|
|
}
|
2022-05-28 13:28:37 +02:00
|
|
|
=> "symlink"
|
2021-09-11 18:47:18 +02:00
|
|
|
|
|
|
|
# Query path type
|
|
|
|
nix.tag.tagName (pathType /path)
|
2021-08-18 11:38:59 +02:00
|
|
|
*/
|
|
|
|
pathType = path:
|
|
|
|
let
|
|
|
|
# baseNameOf is very annoyed if we proceed with string context.
|
|
|
|
# We need to call toString to prevent unsafeDiscardStringContext
|
|
|
|
# from importing a path into store which messes with base- and
|
|
|
|
# dirname of course.
|
2022-01-30 17:06:58 +01:00
|
|
|
path' = builtins.unsafeDiscardStringContext (toString path);
|
2021-08-18 11:38:59 +02:00
|
|
|
# To read the containing directory we absolutely need
|
|
|
|
# to keep the string context, otherwise a derivation
|
|
|
|
# would not be realized before our check (at eval time)
|
|
|
|
containingDir = builtins.readDir (builtins.dirOf path);
|
2021-09-11 18:47:18 +02:00
|
|
|
# Construct tag to use for the value
|
|
|
|
thisPathType = containingDir.${builtins.baseNameOf path'} or "missing";
|
|
|
|
# Trick to check if the symlink target exists and is a directory:
|
|
|
|
# if we append a "/." to the string version of the path, Nix won't
|
|
|
|
# canocalize it (which would strip any "/." in the path), so if
|
|
|
|
# path' + "/." exists, we know that the symlink points to an existing
|
|
|
|
# directory. If not, either the target doesn't exist or is a regular file.
|
|
|
|
# TODO(sterni): is there a way to check reliably if the symlink target exists?
|
|
|
|
isSymlinkDir = builtins.pathExists (path' + "/.");
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
|
|
|
{
|
2022-05-28 13:28:37 +02:00
|
|
|
${thisPathType} = true;
|
2021-09-11 18:47:18 +02:00
|
|
|
};
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
pathType' = path:
|
|
|
|
let
|
|
|
|
p = pathType path;
|
|
|
|
in
|
2022-01-30 17:06:58 +01:00
|
|
|
if p ? missing
|
|
|
|
then builtins.throw "${lib.generators.toPretty {} path} does not exist"
|
|
|
|
else p;
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
/* Check whether the given path is a directory.
|
|
|
|
Throws if the path in question doesn't exist.
|
|
|
|
|
|
|
|
Type: path(-like) -> bool
|
|
|
|
*/
|
2021-09-11 18:47:18 +02:00
|
|
|
isDirectory = path: pathType' path ? directory;
|
|
|
|
|
2021-08-18 11:38:59 +02:00
|
|
|
/* Check whether the given path is a regular file.
|
|
|
|
Throws if the path in question doesn't exist.
|
|
|
|
|
|
|
|
Type: path(-like) -> bool
|
|
|
|
*/
|
2021-09-11 18:47:18 +02:00
|
|
|
isRegularFile = path: pathType' path ? regular;
|
2021-08-18 11:38:59 +02:00
|
|
|
|
|
|
|
/* Check whether the given path is a symbolic link.
|
|
|
|
Throws if the path in question doesn't exist.
|
|
|
|
|
|
|
|
Type: path(-like) -> bool
|
|
|
|
*/
|
2021-09-11 18:47:18 +02:00
|
|
|
isSymlink = path: pathType' path ? symlink;
|
2021-08-18 11:38:59 +02:00
|
|
|
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
|
|
|
{
|
2021-03-15 22:32:16 +01:00
|
|
|
inherit
|
2021-04-20 10:18:28 +02:00
|
|
|
storePathName
|
2022-05-28 13:49:34 +02:00
|
|
|
onlyDrvPath
|
2021-08-18 11:38:59 +02:00
|
|
|
pathType
|
|
|
|
isDirectory
|
|
|
|
isRegularFile
|
|
|
|
isSymlink
|
2021-03-15 22:32:16 +01:00
|
|
|
;
|
|
|
|
}
|