summary refs log tree commit diff
path: root/lib/strings.nix
diff options
context:
space:
mode:
authorChristian Zagrodnick <cz@flyingcircus.io>2015-11-24 10:00:44 +0100
committerVladimír Čunát <vcunat@gmail.com>2015-11-25 08:50:02 +0100
commit1cdacc6aa2f604fbdec60919dcd72cd7cde4551f (patch)
treeb83e5ad6ffd13ece5e699b3c39a203b0c390c5aa /lib/strings.nix
parent882344e480cc37f15a9ecffebfc8f6c931c59282 (diff)
downloadnixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.tar
nixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.tar.gz
nixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.tar.bz2
nixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.tar.lz
nixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.tar.xz
nixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.tar.zst
nixpkgs-1cdacc6aa2f604fbdec60919dcd72cd7cde4551f.zip
lib/strings: add a `toInt` helper (close #11242)
While the function itself is pretty easy, it's not straitforward to find a way to convert string to int with nix.
Diffstat (limited to 'lib/strings.nix')
-rw-r--r--lib/strings.nix8
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index e72bdc6d968..bf6cbd2cbfa 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -223,4 +223,12 @@ rec {
   # Check whether a value is a store path.
   isStorePath = x: builtins.substring 0 1 (toString x) == "/" && dirOf (builtins.toPath x) == builtins.storeDir;
 
+  # Convert string to int
+  # Obviously, it is a bit hacky to use fromJSON that way.
+  toInt = str:
+    let may_be_int = builtins.fromJSON str; in
+    if builtins.isInt may_be_int
+    then may_be_int
+    else throw "Could not convert ${str} to int.";
+
 }