Skip to content

lib.strings: string manipulation functions

String manipulation functions.

lib.strings.concatStrings

Type: concatStrings :: [string] -> string

Concatenate a list of strings.

Example

# `lib.strings.concatStrings` usage example
concatStrings ["foo" "bar"]
=> "foobar"

Located at lib/strings.nix:50 in <nixpkgs>.

lib.strings.concatMapStrings

Type: concatMapStrings :: (a -> string) -> [a] -> string

Map a function over a list and concatenate the resulting strings.

f

: Function argument

list

: Function argument

Example

lib.strings.concatMapStrings usage example

concatMapStrings (x: "a" + x) ["foo" "bar"]
=> "afooabar"

Located at lib/strings.nix:60 in <nixpkgs>.

lib.strings.concatImapStrings

Type: concatImapStrings :: (int -> a -> string) -> [a] -> string

Like concatMapStrings except that the f functions also gets the position as a parameter.

f

: Function argument

list

: Function argument

Example

lib.strings.concatImapStrings usage example

concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"]
=> "1-foo2-bar"

Located at lib/strings.nix:71 in <nixpkgs>.

lib.strings.intersperse

Type: intersperse :: a -> [a] -> [a]

Place an element between each element of a list

separator

: Separator to add between elements

list

: Input list

Example

lib.strings.intersperse usage example

intersperse "/" ["usr" "local" "bin"]
=> ["usr" "/" "local" "/" "bin"].

Located at lib/strings.nix:81 in <nixpkgs>.

lib.strings.concatStringsSep

Type: concatStringsSep :: string -> [string] -> string

Concatenate a list of strings with a separator between each element

Example

# `lib.strings.concatStringsSep` usage example
concatStringsSep "/" ["usr" "local" "bin"]
=> "usr/local/bin"

Located at lib/strings.nix:98 in <nixpkgs>.

lib.strings.concatMapStringsSep

Type: concatMapStringsSep :: string -> (a -> string) -> [a] -> string

Maps a function over a list of strings and then concatenates the result with the specified separator interspersed between elements.

sep

: Separator to add between elements

f

: Function to map over the list

list

: List of input strings

Example

lib.strings.concatMapStringsSep usage example

concatMapStringsSep "-" (x: toUpper x)  ["foo" "bar" "baz"]
=> "FOO-BAR-BAZ"

Located at lib/strings.nix:110 in <nixpkgs>.

lib.strings.concatImapStringsSep

Type: concatIMapStringsSep :: string -> (int -> a -> string) -> [a] -> string

Same as concatMapStringsSep, but the mapping function additionally receives the position of its argument.

sep

: Separator to add between elements

f

: Function that receives elements and their positions

list

: List of input strings

Example

lib.strings.concatImapStringsSep usage example

concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ]
=> "6-3-2"

Located at lib/strings.nix:127 in <nixpkgs>.

lib.strings.concatLines

Type: concatLines :: [string] -> string

Concatenate a list of strings, adding a newline at the end of each one. Defined as concatMapStrings (s: s + "\n").

Example

# `lib.strings.concatLines` usage example
concatLines [ "foo" "bar" ]
=> "foo\nbar\n"

Located at lib/strings.nix:144 in <nixpkgs>.

lib.strings.replicate

Type: replicate :: int -> string -> string

Replicate a string n times, and concatenate the parts into a new string.

n

: Function argument

s

: Function argument

Example

lib.strings.replicate usage example

replicate 3 "v"
=> "vvv"
replicate 5 "hello"
=> "hellohellohellohellohello"

Located at lib/strings.nix:158 in <nixpkgs>.

lib.strings.makeSearchPath

Type: makeSearchPath :: string -> [string] -> string

Construct a Unix-style, colon-separated search path consisting of the given subDir appended to each of the given paths.

subDir

: Directory name to append

paths

: List of base paths

Example

lib.strings.makeSearchPath usage example

makeSearchPath "bin" ["/root" "/usr" "/usr/local"]
=> "/root/bin:/usr/bin:/usr/local/bin"
makeSearchPath "bin" [""]
=> "/bin"

Located at lib/strings.nix:171 in <nixpkgs>.

lib.strings.makeSearchPathOutput

Type: string -> string -> [package] -> string

Construct a Unix-style search path by appending the given subDir to the specified output of each of the packages. If no output by the given name is found, fallback to .out and then to the default.

output

: Package output to use

subDir

: Directory name to append

pkgs

: List of packages

Example

lib.strings.makeSearchPathOutput usage example

makeSearchPathOutput "dev" "bin" [ pkgs.openssl pkgs.zlib ]
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev/bin:/nix/store/wwh7mhwh269sfjkm6k5665b5kgp7jrk2-zlib-1.2.8/bin"

Located at lib/strings.nix:189 in <nixpkgs>.

lib.strings.makeLibraryPath

Construct a library search path (such as RPATH) containing the libraries for a set of packages

Example

# `lib.strings.makeLibraryPath` usage example
makeLibraryPath [ "/usr" "/usr/local" ]
=> "/usr/lib:/usr/local/lib"
pkgs = import <nixpkgs> { }
makeLibraryPath [ pkgs.openssl pkgs.zlib ]
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r/lib:/nix/store/wwh7mhwh269sfjkm6k5665b5kgp7jrk2-zlib-1.2.8/lib"

Located at lib/strings.nix:207 in <nixpkgs>.

lib.strings.makeIncludePath

Construct an include search path (such as C_INCLUDE_PATH) containing the header files for a set of packages or paths.

Example

# `lib.strings.makeIncludePath` usage example
makeIncludePath [ "/usr" "/usr/local" ]
=> "/usr/include:/usr/local/include"
pkgs = import <nixpkgs> { }
makeIncludePath [ pkgs.openssl pkgs.zlib ]
=> "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev/include:/nix/store/wwh7mhwh269sfjkm6k5665b5kgp7jrk2-zlib-1.2.8-dev/include"

Located at lib/strings.nix:219 in <nixpkgs>.

lib.strings.makeBinPath

Construct a binary search path (such as $PATH) containing the binaries for a set of packages.

Example

# `lib.strings.makeBinPath` usage example
makeBinPath ["/root" "/usr" "/usr/local"]
=> "/root/bin:/usr/bin:/usr/local/bin"

Located at lib/strings.nix:228 in <nixpkgs>.

lib.strings.normalizePath

Type: normalizePath :: string -> string

Normalize path, removing extraneous /s

s

: Function argument

Example

lib.strings.normalizePath usage example

normalizePath "/a//b///c/"
=> "/a/b/c/"

Located at lib/strings.nix:238 in <nixpkgs>.

lib.strings.optionalString

Type: optionalString :: bool -> string -> string

Depending on the boolean `cond', return either the given string or the empty string. Useful to concatenate against a bigger string.

cond

: Condition

string

: String to return if condition is true

Example

lib.strings.optionalString usage example

optionalString true "some-string"
=> "some-string"
optionalString false "some-string"
=> ""

Located at lib/strings.nix:264 in <nixpkgs>.

lib.strings.hasPrefix

Type: hasPrefix :: string -> string -> bool

Determine whether a string has given prefix.

pref

: Prefix to check for

str

: Input string

Example

lib.strings.hasPrefix usage example

hasPrefix "foo" "foobar"
=> true
hasPrefix "foo" "barfoo"
=> false

Located at lib/strings.nix:280 in <nixpkgs>.

lib.strings.hasSuffix

Type: hasSuffix :: string -> string -> bool

Determine whether a string has given suffix.

suffix

: Suffix to check for

content

: Input string

Example

lib.strings.hasSuffix usage example

hasSuffix "foo" "foobar"
=> false
hasSuffix "foo" "barfoo"
=> true

Located at lib/strings.nix:307 in <nixpkgs>.

lib.strings.hasInfix

Type: hasInfix :: string -> string -> bool

Determine whether a string contains the given infix

infix

: Function argument

content

: Function argument

Example

lib.strings.hasInfix usage example

hasInfix "bc" "abcd"
=> true
hasInfix "ab" "abcd"
=> true
hasInfix "cd" "abcd"
=> true
hasInfix "foo" "abcd"
=> false

Located at lib/strings.nix:344 in <nixpkgs>.

lib.strings.stringToCharacters

Type: stringToCharacters :: string -> [string]

Convert a string to a list of characters (i.e. singleton strings). This allows you to, e.g., map a function over each character. However, note that this will likely be horribly inefficient; Nix is not a general purpose programming language. Complex string manipulations should, if appropriate, be done in a derivation. Also note that Nix treats strings as a list of bytes and thus doesn't handle unicode.

s

: Function argument

Example

lib.strings.stringToCharacters usage example

stringToCharacters ""
=> [ ]
stringToCharacters "abc"
=> [ "a" "b" "c" ]
stringToCharacters "🦄"
=> [ "�" "�" "�" "�" ]

Located at lib/strings.nix:374 in <nixpkgs>.

lib.strings.stringAsChars

Type: stringAsChars :: (string -> string) -> string -> string

Manipulate a string character by character and replace them by strings before concatenating the results.

f

: Function to map over each individual character

s

: Input string

Example

lib.strings.stringAsChars usage example

stringAsChars (x: if x == "a" then "i" else x) "nax"
=> "nix"

Located at lib/strings.nix:386 in <nixpkgs>.

lib.strings.charToInt

Type: charToInt :: string -> int

Convert char to ascii value, must be in printable range

c

: Function argument

Example

lib.strings.charToInt usage example

charToInt "A"
=> 65
charToInt "("
=> 40

Located at lib/strings.nix:405 in <nixpkgs>.

lib.strings.escape

Type: escape :: [string] -> string -> string

Escape occurrence of the elements of list in string by prefixing it with a backslash.

list

: Function argument

Example

lib.strings.escape usage example

escape ["(" ")"] "(foo)"
=> "\\(foo\\)"

Located at lib/strings.nix:416 in <nixpkgs>.

lib.strings.escapeC

Type: escapeC = [string] -> string -> string

Escape occurrence of the element of list in string by converting to its ASCII value and prefixing it with \x. Only works for printable ascii characters.

list

: Function argument

Example

lib.strings.escapeC usage example

escapeC [" "] "foo bar"
=> "foo\\x20bar"

Located at lib/strings.nix:429 in <nixpkgs>.

lib.strings.escapeURL

Type: escapeURL :: string -> string

Escape the string so it can be safely placed inside a URL query.

Example

# `lib.strings.escapeURL` usage example
escapeURL "foo/bar baz"
=> "foo%2Fbar%20baz"

Located at lib/strings.nix:440 in <nixpkgs>.

lib.strings.escapeShellArg

Type: escapeShellArg :: string -> string

Quote string to be used safely within the Bourne shell.

arg

: Function argument

Example

lib.strings.escapeShellArg usage example

escapeShellArg "esc'ape\nme"
=> "'esc'\\''ape\nme'"

Located at lib/strings.nix:454 in <nixpkgs>.

lib.strings.escapeShellArgs

Type: escapeShellArgs :: [string] -> string

Quote all arguments to be safely passed to the Bourne shell.

Example

# `lib.strings.escapeShellArgs` usage example
escapeShellArgs ["one" "two three" "four'five"]
=> "'one' 'two three' 'four'\\''five'"

Located at lib/strings.nix:464 in <nixpkgs>.

lib.strings.isValidPosixName

Type: string -> bool

Test whether the given name is a valid POSIX shell variable name.

name

: Function argument

Example

lib.strings.isValidPosixName usage example

isValidPosixName "foo_bar000"
=> true
isValidPosixName "0-bad.jpg"
=> false

Located at lib/strings.nix:476 in <nixpkgs>.

lib.strings.toShellVar

Type: string -> (string | listOf string | attrsOf string) -> string

Translate a Nix value into a shell variable declaration, with proper escaping.

The value can be a string (mapped to a regular variable), a list of strings (mapped to a Bash-style array) or an attribute set of strings (mapped to a Bash-style associative array). Note that "string" includes string-coercible values like paths or derivations.

Strings are translated into POSIX sh-compatible code; lists and attribute sets assume a shell that understands Bash syntax (e.g. Bash or ZSH).

name

: Function argument

value

: Function argument

Example

lib.strings.toShellVar usage example

''
  ${toShellVar "foo" "some string"}
  [[ "$foo" == "some string" ]]
''

Located at lib/strings.nix:496 in <nixpkgs>.

lib.strings.toShellVars

Type: attrsOf (string | listOf string | attrsOf string) -> string

Translate an attribute set into corresponding shell variable declarations using toShellVar.

vars

: Function argument

Example

lib.strings.toShellVars usage example

let
  foo = "value";
  bar = foo;
in ''
  ${toShellVars { inherit foo bar; }}
  [[ "$foo" == "$bar" ]]
''

Located at lib/strings.nix:524 in <nixpkgs>.

lib.strings.escapeNixString

Type: string -> string

Turn a string into a Nix expression representing that string

s

: Function argument

Example

lib.strings.escapeNixString usage example

escapeNixString "hello\${}\n"
=> "\"hello\\\${}\\n\""

Located at lib/strings.nix:534 in <nixpkgs>.

lib.strings.escapeRegex

Type: string -> string

Turn a string into an exact regular expression

Example

# `lib.strings.escapeRegex` usage example
escapeRegex "[^a-z]*"
=> "\\[\\^a-z]\\*"

Located at lib/strings.nix:544 in <nixpkgs>.

lib.strings.escapeNixIdentifier

Type: string -> string

Quotes a string if it can't be used as an identifier directly.

s

: Function argument

Example

lib.strings.escapeNixIdentifier usage example

escapeNixIdentifier "hello"
=> "hello"
escapeNixIdentifier "0abc"
=> "\"0abc\""

Located at lib/strings.nix:556 in <nixpkgs>.

lib.strings.escapeXML

Type: string -> string

Escapes a string such that it is safe to include verbatim in an XML document.

Example

# `lib.strings.escapeXML` usage example
escapeXML ''"test" 'test' < & >''
=> "&quot;test&quot; &apos;test&apos; &lt; &amp; &gt;"

Located at lib/strings.nix:570 in <nixpkgs>.

lib.strings.toLower

Type: toLower :: string -> string

Converts an ASCII string to lower-case.

Example

# `lib.strings.toLower` usage example
toLower "HOME"
=> "home"

Located at lib/strings.nix:589 in <nixpkgs>.

lib.strings.toUpper

Type: toUpper :: string -> string

Converts an ASCII string to upper-case.

Example

# `lib.strings.toUpper` usage example
toUpper "home"
=> "HOME"

Located at lib/strings.nix:599 in <nixpkgs>.

lib.strings.addContextFrom

Appends string context from another string. This is an implementation detail of Nix and should be used carefully.

Strings in Nix carry an invisible context which is a list of strings representing store paths. If the string is later used in a derivation attribute, the derivation will properly populate the inputDrvs and inputSrcs.

a

: Function argument

b

: Function argument

Example

lib.strings.addContextFrom usage example

pkgs = import <nixpkgs> { };
addContextFrom pkgs.coreutils "bar"
=> "bar"

Located at lib/strings.nix:614 in <nixpkgs>.

lib.strings.splitString

Cut a string with a separator and produces a list of strings which were separated by this separator.

sep

: Function argument

s

: Function argument

Example

lib.strings.splitString usage example

splitString "." "foo.bar.baz"
=> [ "foo" "bar" "baz" ]
splitString "/" "/usr/local/bin"
=> [ "" "usr" "local" "bin" ]

Located at lib/strings.nix:625 in <nixpkgs>.

lib.strings.removePrefix

Type: string -> string -> string

Return a string without the specified prefix, if the prefix matches.

prefix

: Prefix to remove if it matches

str

: Input string

Example

lib.strings.removePrefix usage example

removePrefix "foo." "foo.bar.baz"
=> "bar.baz"
removePrefix "xxx" "foo.bar.baz"
=> "foo.bar.baz"

Located at lib/strings.nix:641 in <nixpkgs>.

lib.strings.removeSuffix

Type: string -> string -> string

Return a string without the specified suffix, if the suffix matches.

suffix

: Suffix to remove if it matches

str

: Input string

Example

lib.strings.removeSuffix usage example

removeSuffix "front" "homefront"
=> "home"
removeSuffix "xxx" "homefront"
=> "homefront"

Located at lib/strings.nix:674 in <nixpkgs>.

lib.strings.versionOlder

Return true if string v1 denotes a version older than v2.

v1

: Function argument

v2

: Function argument

Example

lib.strings.versionOlder usage example

versionOlder "1.1" "1.2"
=> true
versionOlder "1.1" "1.1"
=> false

Located at lib/strings.nix:705 in <nixpkgs>.

lib.strings.versionAtLeast

Return true if string v1 denotes a version equal to or newer than v2.

v1

: Function argument

v2

: Function argument

Example

lib.strings.versionAtLeast usage example

versionAtLeast "1.1" "1.0"
=> true
versionAtLeast "1.1" "1.1"
=> true
versionAtLeast "1.1" "1.2"
=> false

Located at lib/strings.nix:717 in <nixpkgs>.

lib.strings.getName

This function takes an argument that's either a derivation or a derivation's "name" attribute and extracts the name part from that argument.

Example

# `lib.strings.getName` usage example
getName "youtube-dl-2016.01.01"
=> "youtube-dl"
getName pkgs.youtube-dl
=> "youtube-dl"

Located at lib/strings.nix:729 in <nixpkgs>.

lib.strings.getVersion

This function takes an argument that's either a derivation or a derivation's "name" attribute and extracts the version part from that argument.

Example

# `lib.strings.getVersion` usage example
getVersion "youtube-dl-2016.01.01"
=> "2016.01.01"
getVersion pkgs.youtube-dl
=> "2016.01.01"

Located at lib/strings.nix:746 in <nixpkgs>.

lib.strings.nameFromURL

Extract name with version from URL. Ask for separator which is supposed to start extension.

url

: Function argument

sep

: Function argument

Example

lib.strings.nameFromURL usage example

nameFromURL "https://nixos.org/releases/nix/nix-1.7/nix-1.7-x86_64-linux.tar.bz2" "-"
=> "nix"
nameFromURL "https://nixos.org/releases/nix/nix-1.7/nix-1.7-x86_64-linux.tar.bz2" "_"
=> "nix-1.7-x86"

Located at lib/strings.nix:762 in <nixpkgs>.

lib.strings.cmakeOptionType

Type:

cmakeOptionType :: string -> string -> string -> string

@param feature The feature to be set
@param type The type of the feature to be set, as described in
            https://cmake.org/cmake/help/latest/command/set.html
            the possible values (case insensitive) are:
            BOOL FILEPATH PATH STRING INTERNAL
@param value The desired value

Create a "-D:=" string that can be passed to typical CMake invocations.

Example

# `lib.strings.cmakeOptionType` usage example
cmakeOptionType "string" "ENGINE" "sdl2"
=> "-DENGINE:STRING=sdl2"

Located at lib/strings.nix:785 in <nixpkgs>.

lib.strings.cmakeBool

Type:

cmakeBool :: string -> bool -> string

@param condition The condition to be made true or false
@param flag The controlling flag of the condition

Create a -D={TRUE,FALSE} string that can be passed to typical CMake invocations.

condition

: Function argument

flag

: Function argument

Example

lib.strings.cmakeBool usage example

cmakeBool "ENABLE_STATIC_LIBS" false
=> "-DENABLESTATIC_LIBS:BOOL=FALSE"

Located at lib/strings.nix:805 in <nixpkgs>.

lib.strings.cmakeFeature

Type:

cmakeFeature :: string -> string -> string

@param condition The condition to be made true or false
@param flag The controlling flag of the condition

Create a -D:STRING= string that can be passed to typical CMake invocations. This is the most typical usage, so it deserves a special case.

feature

: Function argument

value

: Function argument

Example

lib.strings.cmakeFeature usage example

cmakeFeature "MODULES" "badblock"
=> "-DMODULES:STRING=badblock"

Located at lib/strings.nix:823 in <nixpkgs>.

lib.strings.mesonOption

Type:

mesonOption :: string -> string -> string

@param feature The feature to be set
@param value The desired value

Create a -D= string that can be passed to typical Meson invocations.

feature

: Function argument

value

: Function argument

Example

lib.strings.mesonOption usage example

mesonOption "engine" "opengl"
=> "-Dengine=opengl"

Located at lib/strings.nix:840 in <nixpkgs>.

lib.strings.mesonBool

Type:

mesonBool :: string -> bool -> string

@param condition The condition to be made true or false
@param flag The controlling flag of the condition

Create a -D={true,false} string that can be passed to typical Meson invocations.

condition

: Function argument

flag

: Function argument

Example

lib.strings.mesonBool usage example

mesonBool "hardened" true
=> "-Dhardened=true"
mesonBool "static" false
=> "-Dstatic=false"

Located at lib/strings.nix:859 in <nixpkgs>.

lib.strings.mesonEnable

Type:

mesonEnable :: string -> bool -> string

@param feature The feature to be enabled or disabled
@param flag The controlling flag

Create a -D={enabled,disabled} string that can be passed to typical Meson invocations.

feature

: Function argument

flag

: Function argument

Example

lib.strings.mesonEnable usage example

mesonEnable "docs" true
=> "-Ddocs=enabled"
mesonEnable "savage" false
=> "-Dsavage=disabled"

Located at lib/strings.nix:878 in <nixpkgs>.

lib.strings.enableFeature

Create an --{enable,disable}- string that can be passed to standard GNU Autoconf scripts.

flag

: Function argument

feature

: Function argument

Example

lib.strings.enableFeature usage example

enableFeature true "shared"
=> "--enable-shared"
enableFeature false "shared"
=> "--disable-shared"

Located at lib/strings.nix:892 in <nixpkgs>.

lib.strings.enableFeatureAs

Create an --{enable-=,disable-} string that can be passed to standard GNU Autoconf scripts.

flag

: Function argument

feature

: Function argument

value

: Function argument

Example

lib.strings.enableFeatureAs usage example

enableFeatureAs true "shared" "foo"
=> "--enable-shared=foo"
enableFeatureAs false "shared" (throw "ignored")
=> "--disable-shared"

Located at lib/strings.nix:906 in <nixpkgs>.

lib.strings.withFeature

Create an --{with,without}- string that can be passed to standard GNU Autoconf scripts.

flag

: Function argument

feature

: Function argument

Example

lib.strings.withFeature usage example

withFeature true "shared"
=> "--with-shared"
withFeature false "shared"
=> "--without-shared"

Located at lib/strings.nix:918 in <nixpkgs>.

lib.strings.withFeatureAs

Create an --{with-=,without-} string that can be passed to standard GNU Autoconf scripts.

flag

: Function argument

feature

: Function argument

value

: Function argument

Example

lib.strings.withFeatureAs usage example

withFeatureAs true "shared" "foo"
=> "--with-shared=foo"
withFeatureAs false "shared" (throw "ignored")
=> "--without-shared"

Located at lib/strings.nix:931 in <nixpkgs>.

lib.strings.fixedWidthString

Type: fixedWidthString :: int -> string -> string -> string

Create a fixed width string with additional prefix to match required width.

This function will fail if the input string is longer than the requested length.

width

: Function argument

filler

: Function argument

str

: Function argument

Example

lib.strings.fixedWidthString usage example

fixedWidthString 5 "0" (toString 15)
=> "00015"

Located at lib/strings.nix:946 in <nixpkgs>.

lib.strings.fixedWidthNumber

Format a number adding leading zeroes up to fixed width.

width

: Function argument

n

: Function argument

Example

lib.strings.fixedWidthNumber usage example

fixedWidthNumber 5 15
=> "00015"

Located at lib/strings.nix:963 in <nixpkgs>.

lib.strings.floatToString

Convert a float to a string, but emit a warning when precision is lost during the conversion

float

: Function argument

Example

lib.strings.floatToString usage example

floatToString 0.000001
=> "0.000001"
floatToString 0.0000001
=> trace: warning: Imprecise conversion from float to string 0.000000
   "0.000000"

Located at lib/strings.nix:975 in <nixpkgs>.

lib.strings.isCoercibleToString

Soft-deprecated function. While the original implementation is available as isConvertibleWithToString, consider using isStringLike instead, if suitable.

Located at lib/strings.nix:983 in <nixpkgs>.

lib.strings.isConvertibleWithToString

Check whether a list or other value can be passed to toString.

Many types of value are coercible to string this way, including int, float, null, bool, list of similarly coercible values.

Located at lib/strings.nix:992 in <nixpkgs>.

lib.strings.isStringLike

Check whether a value can be coerced to a string. The value must be a string, path, or attribute set.

String-like values can be used without explicit conversion in string interpolations and in most functions that expect a string.

x

: Function argument

Located at lib/strings.nix:1005 in <nixpkgs>.

lib.strings.isStorePath

Check whether a value is a store path.

x

: Function argument

Example

lib.strings.isStorePath usage example

isStorePath "/nix/store/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11/bin/python"
=> false
isStorePath "/nix/store/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11"
=> true
isStorePath pkgs.python
=> true
isStorePath [] || isStorePath 42 || isStorePath {} || …
=> false

Located at lib/strings.nix:1023 in <nixpkgs>.

lib.strings.toInt

Type: string -> int

Parse a string as an int. Does not support parsing of integers with preceding zero due to ambiguity between zero-padded and octal numbers. See toIntBase10.

Example

# `lib.strings.toInt` usage example
toInt "1337"
=> 1337

toInt "-4"
=> -4

toInt " 123 "
=> 123

toInt "00024"
=> error: Ambiguity in interpretation of 00024 between octal and zero padded integer.

toInt "3.14"
=> error: floating point JSON numbers are not supported

Located at lib/strings.nix:1053 in <nixpkgs>.

lib.strings.toIntBase10

Type: string -> int

Parse a string as a base 10 int. This supports parsing of zero-padded integers.

Example

# `lib.strings.toIntBase10` usage example
toIntBase10 "1337"
=> 1337

toIntBase10 "-4"
=> -4

toIntBase10 " 123 "
=> 123

toIntBase10 "00024"
=> 24

toIntBase10 "3.14"
=> error: floating point JSON numbers are not supported

Located at lib/strings.nix:1106 in <nixpkgs>.

lib.strings.readPathsFromFile

Read a list of paths from file, relative to the rootPath. Lines beginning with # are treated as comments and ignored. Whitespace is significant.

NOTE: This function is not performant and should be avoided.

Example

# `lib.strings.readPathsFromFile` usage example
readPathsFromFile /prefix
  ./pkgs/development/libraries/qt-5/5.4/qtbase/series
=> [ "/prefix/dlopen-resolv.patch" "/prefix/tzdir.patch"
     "/prefix/dlopen-libXcursor.patch" "/prefix/dlopen-openssl.patch"
     "/prefix/dlopen-dbus.patch" "/prefix/xdg-config-dirs.patch"
     "/prefix/nix-profiles-library-paths.patch"
     "/prefix/compose-search-path.patch" ]

Located at lib/strings.nix:1154 in <nixpkgs>.

lib.strings.fileContents

Type: fileContents :: path -> string

Read the contents of a file removing the trailing \n

file

: Function argument

Example

lib.strings.fileContents usage example

$ echo "1.0" > ./version

fileContents ./version
=> "1.0"

Located at lib/strings.nix:1174 in <nixpkgs>.

lib.strings.sanitizeDerivationName

Type: sanitizeDerivationName :: String -> String

Creates a valid derivation name from a potentially invalid one.

Example

# `lib.strings.sanitizeDerivationName` usage example
sanitizeDerivationName "../hello.bar # foo"
=> "-hello.bar-foo"
sanitizeDerivationName ""
=> "unknown"
sanitizeDerivationName pkgs.hello
=> "-nix-store-2g75chlbpxlrqn15zlby2dfh8hr9qwbk-hello-2.10"

Located at lib/strings.nix:1189 in <nixpkgs>.

lib.strings.levenshtein

Type: levenshtein :: string -> string -> int

Computes the Levenshtein distance between two strings. Complexity O(n*m) where n and m are the lengths of the strings. Algorithm adjusted from https://stackoverflow.com/a/9750974/6605742

a

: Function argument

b

: Function argument

Example

lib.strings.levenshtein usage example

levenshtein "foo" "foo"
=> 0
levenshtein "book" "hook"
=> 1
levenshtein "hello" "Heyo"
=> 3

Located at lib/strings.nix:1228 in <nixpkgs>.

lib.strings.commonPrefixLength

Returns the length of the prefix common to both strings.

a

: Function argument

b

: Function argument

Located at lib/strings.nix:1249 in <nixpkgs>.

lib.strings.commonSuffixLength

Returns the length of the suffix common to both strings.

a

: Function argument

b

: Function argument

Located at lib/strings.nix:1257 in <nixpkgs>.

lib.strings.levenshteinAtMost

Type: levenshteinAtMost :: int -> string -> string -> bool

Returns whether the levenshtein distance between two strings is at most some value Complexity is O(min(n,m)) for k <= 2 and O(n*m) otherwise

Example

# `lib.strings.levenshteinAtMost` usage example
levenshteinAtMost 0 "foo" "foo"
=> true
levenshteinAtMost 1 "foo" "boa"
=> false
levenshteinAtMost 2 "foo" "boa"
=> true
levenshteinAtMost 2 "This is a sentence" "this is a sentense."
=> false
levenshteinAtMost 3 "This is a sentence" "this is a sentense."
=> true

Located at lib/strings.nix:1281 in <nixpkgs>.