Skip to content

Commit

Permalink
lib: add isInStore and isPath, slighly change isStorePath
Browse files Browse the repository at this point in the history
- isStorePath now returns true for paths
- isPath returns true only when the type of value is path
- isInStore just asserts that the path is prefixed with store dir
  • Loading branch information
domenkozar committed Nov 15, 2017
1 parent a7f4d97 commit 7897128
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/default.nix
Expand Up @@ -84,7 +84,7 @@ let
toLower toUpper addContextFrom splitString removePrefix
removeSuffix versionOlder versionAtLeast getVersion nameFromURL
enableFeature fixedWidthString fixedWidthNumber isStorePath
toInt readPathsFromFile fileContents;
isInStore isPath toInt readPathsFromFile fileContents;
inherit (stringsWithDeps) textClosureList textClosureMap
noDepEntry fullDepEntry packEntry stringAfter;
inherit (customisation) overrideDerivation makeOverridable
Expand Down
48 changes: 44 additions & 4 deletions lib/strings.nix
Expand Up @@ -429,7 +429,7 @@ rec {
*/
fixedWidthNumber = width: n: fixedWidthString width "0" (toString n);

/* Check whether a value is a store path.
/* Check whether a string or a path is a store path.
Example:
isStorePath "/nix/store/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11/bin/python"
Expand All @@ -438,13 +438,53 @@ rec {
=> true
isStorePath pkgs.python
=> true
isStorePath ./types.nix
=> true
isStorePath [] || isStorePath 42 || isStorePath {} || …
=> false
*/
isStorePath = x:
builtins.isString x
&& builtins.substring 0 1 (toString x) == "/"
&& dirOf (builtins.toPath x) == builtins.storeDir;
let
toStringContext = y: "${y}";
in (builtins.isString x || isPath x)
&& builtins.substring 0 1 (toStringContext x) == "/"
&& dirOf (toStringContext x) == builtins.storeDir;

/* Check whether a string or a path is in the store.
Example:
isInStore "/nix/store/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11/bin/python"
=> true
isInStore "/nix/store/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11/"
=> true
isInStore "/home/me/whatever.nix"
=> false
isInStore ./types.nix
=> true
isInStore (toString ./types.nix)
=> false
isInStore [] || isInStore 42 || isInStore {} || …
=> false
*/
isInStore = s:
(builtins.isString s || isPath s)
&& (builtins.substring 0 (builtins.stringLength builtins.storeDir) s) == builtins.storeDir;

/* Check whether a value is of type path.
Example:
isPath "./types.nix"
=> false
isPath ./types.nix
=> true
isPath [] || isPath 42 || isPath {} || …
=> false
*/
# Hacky: no isPath priomp
isPath = s:
(!builtins.isString s)
&& (!builtins.isAttrs s)
&& builtins.substring 0 1 (toString s) == "/";

/* Convert string to int
Obviously, it is a bit hacky to use fromJSON that way.
Expand Down
3 changes: 1 addition & 2 deletions lib/types.nix
Expand Up @@ -219,8 +219,7 @@ rec {

path = mkOptionType {
name = "path";
# Hacky: there is no ‘isPath’ primop.
check = x: builtins.substring 0 1 (toString x) == "/";
check = x: isString x || isPath x;
merge = mergeOneOption;
};

Expand Down

0 comments on commit 7897128

Please sign in to comment.