lib.strings: string manipulation functions
String manipulation functions.
lib.strings.join
Concatenates a list of strings with a separator between each element.
Inputs
-
sep -
Separator to add between elements
-
list -
List of strings that will be joined
Type
join :: String -> [String] -> String
Examples
Example
lib.strings.join usage example
join ", " ["foo" "bar"]
=> "foo, bar"
Located at lib/strings.nix:72 in <nixpkgs>.
lib.strings.concatStrings
Concatenate a list of strings.
Type
concatStrings :: [String] -> String
Examples
Example
lib.strings.concatStrings usage example
concatStrings ["foo" "bar"]
=> "foobar"
Located at lib/strings.nix:94 in <nixpkgs>.
lib.strings.concatMapStrings
Map a function over a list and concatenate the resulting strings.
Inputs
-
f -
1. Function argument
-
list -
2. Function argument
Type
concatMapStrings :: (a -> String) -> [a] -> String
Examples
Example
lib.strings.concatMapStrings usage example
concatMapStrings (x: "a" + x) ["foo" "bar"]
=> "afooabar"
Located at lib/strings.nix:124 in <nixpkgs>.
lib.strings.concatImapStrings
Like concatMapStrings except that the function f also gets the
position as a parameter.
Inputs
-
f -
1. Function argument
-
list -
2. Function argument
Type
concatImapStrings :: (Int -> a -> String) -> [a] -> String
Examples
Example
lib.strings.concatImapStrings usage example
concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"]
=> "1-foo2-bar"
Located at lib/strings.nix:155 in <nixpkgs>.
lib.strings.intersperse
Place an element between each element of a list
Inputs
-
separator -
Separator to add between elements
-
list -
Input list
Type
intersperse :: a -> [a] -> [a]
Examples
Example
lib.strings.intersperse usage example
intersperse "/" ["usr" "local" "bin"]
=> ["usr" "/" "local" "/" "bin"].
Located at lib/strings.nix:185 in <nixpkgs>.
lib.strings.concatStringsSep
Concatenate a list of strings with a separator between each element
Inputs
-
sep -
Separator to add between elements
-
list -
List of input strings
Type
concatStringsSep :: String -> [String] -> String
Examples
Example
lib.strings.concatStringsSep usage example
concatStringsSep "/" ["usr" "local" "bin"]
=> "usr/local/bin"
Located at lib/strings.nix:225 in <nixpkgs>.
lib.strings.concatMapStringsSep
Maps a function over a list of strings and then concatenates the result with the specified separator interspersed between elements.
Inputs
-
sep -
Separator to add between elements
-
f -
Function to map over the list
-
list -
List of input strings
Type
concatMapStringsSep :: String -> (a -> String) -> [a] -> String
Examples
Example
lib.strings.concatMapStringsSep usage example
concatMapStringsSep "-" (x: toUpper x) ["foo" "bar" "baz"]
=> "FOO-BAR-BAZ"
Located at lib/strings.nix:260 in <nixpkgs>.
lib.strings.concatImapStringsSep
Same as concatMapStringsSep, but the mapping function
additionally receives the position of its argument.
Inputs
-
sep -
Separator to add between elements
-
f -
Function that receives elements and their positions
-
list -
List of input strings
Type
concatIMapStringsSep :: String -> (Int -> a -> String) -> [a] -> String
Examples
Example
lib.strings.concatImapStringsSep usage example
concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ]
=> "6-3-2"
Located at lib/strings.nix:296 in <nixpkgs>.
lib.strings.concatMapAttrsStringSep
Like concatMapStringsSep
but takes an attribute set instead of a list.
Inputs
-
sep -
Separator to add between item strings
-
f -
Function that takes each key and value and return a string
-
attrs -
Attribute set to map from
Type
concatMapAttrsStringSep :: String -> (String -> a -> String) -> { [String] :: a } -> String
Examples
Example
lib.strings.concatMapAttrsStringSep usage example
concatMapAttrsStringSep "\n" (name: value: "${name}: foo-${value}") { a = "0.1.0"; b = "0.2.0"; }
=> "a: foo-0.1.0\nb: foo-0.2.0"
Located at lib/strings.nix:333 in <nixpkgs>.
lib.strings.concatLines
Concatenate a list of strings, adding a newline at the end of each one.
Inputs
-
list -
List of strings. Any element that is not a string will be implicitly converted to a string.
Type
concatLines :: [String] -> String
Examples
Example
lib.strings.concatLines usage example
concatLines [ "foo" "bar" ]
=> "foo\nbar\n"
Located at lib/strings.nix:362 in <nixpkgs>.
lib.strings.replaceString
Given string s, replace every occurrence of the string from with the string to.
Inputs
-
from -
The string to be replaced
-
to -
The string to replace with
-
s -
The original string where replacements will be made
Type
replaceString :: String -> String -> String -> String
Examples
Example
lib.strings.replaceString usage example
replaceString "world" "Nix" "Hello, world!"
=> "Hello, Nix!"
replaceString "." "_" "v1.2.3"
=> "v1_2_3"
Located at lib/strings.nix:397 in <nixpkgs>.
lib.strings.replicate
Repeat a string n times,
and concatenate the parts into a new string.
Inputs
-
n -
1. Function argument
-
s -
2. Function argument
Type
replicate :: Int -> String -> String
Examples
Example
lib.strings.replicate usage example
replicate 3 "v"
=> "vvv"
replicate 5 "hello"
=> "hellohellohellohellohello"
Located at lib/strings.nix:430 in <nixpkgs>.
lib.strings.trim
Remove leading and trailing whitespace from a string s.
Whitespace is defined as any of the following characters: " ", "\t" "\r" "\n"
Inputs
-
s -
The string to trim
Type
trim :: String -> String
Examples
Example
lib.strings.trim usage example
trim " hello, world! "
=> "hello, world!"
Located at lib/strings.nix:460 in <nixpkgs>.
lib.strings.trimWith
Remove leading and/or trailing whitespace from a string s.
To remove both leading and trailing whitespace, you can also use trim
Whitespace is defined as any of the following characters: " ", "\t" "\r" "\n"
Inputs
-
config(Attribute set) -
start -
Whether to trim leading whitespace (
falseby default) -
end -
Whether to trim trailing whitespace (
falseby default) -
s -
The string to trim
Type
trimWith :: { start :: Bool; end :: Bool; } -> String -> String
Examples
Example
lib.strings.trimWith usage example
trimWith { start = true; } " hello, world! "}
=> "hello, world! "
trimWith { end = true; } " hello, world! "}
=> " hello, world!"
Located at lib/strings.nix:504 in <nixpkgs>.
lib.strings.makeSearchPath
Construct a Unix-style, colon-separated search path consisting of
the given subDir appended to each of the given paths.
Inputs
-
subDir -
Directory name to append
-
paths -
List of base paths
Type
makeSearchPath :: String -> [String] -> String
Examples
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:565 in <nixpkgs>.
lib.strings.makeSearchPathOutput
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.
Inputs
-
output -
Package output to use
-
subDir -
Directory name to append
-
pkgs -
List of packages
Type
makeSearchPathOutput :: String -> String -> [Derivation] -> String
Examples
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:606 in <nixpkgs>.
lib.strings.makeLibraryPath
Construct a library search path (such as RPATH) containing the libraries for a set of packages
Inputs
-
packages -
List of packages
Type
makeLibraryPath :: [Derivation] -> String
Examples
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:645 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.
Inputs
-
packages -
List of packages
Type
makeIncludePath :: [Derivation] -> String
Examples
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:676 in <nixpkgs>.
lib.strings.makeBinPath
Construct a binary search path (such as $PATH) containing the binaries for a set of packages.
Inputs
-
packages -
List of packages
Type
makeBinPath :: [Derivation] -> String
Examples
Example
lib.strings.makeBinPath usage example
makeBinPath ["/root" "/usr" "/usr/local"]
=> "/root/bin:/usr/bin:/usr/local/bin"
Located at lib/strings.nix:704 in <nixpkgs>.
lib.strings.normalizePath
Normalize path, removing extraneous /s
Inputs
-
s -
1. Function argument
Type
normalizePath :: String -> String
Examples
Example
lib.strings.normalizePath usage example
normalizePath "/a//b///c/"
=> "/a/b/c/"
Located at lib/strings.nix:731 in <nixpkgs>.
lib.strings.optionalString
Depending on the boolean cond, return either the given string
or the empty string. Useful to concatenate against a bigger string.
Inputs
-
cond -
Condition
-
string -
String to return if condition is true
Type
optionalString :: Bool -> String -> String
Examples
Example
lib.strings.optionalString usage example
optionalString true "some-string"
=> "some-string"
optionalString false "some-string"
=> ""
Located at lib/strings.nix:776 in <nixpkgs>.
lib.strings.hasPrefix
Determine whether a string has given prefix.
Inputs
-
pref -
Prefix to check for
-
str -
Input string
Type
hasPrefix :: String -> String -> Bool
Examples
Example
lib.strings.hasPrefix usage example
hasPrefix "foo" "foobar"
=> true
hasPrefix "foo" "barfoo"
=> false
Located at lib/strings.nix:808 in <nixpkgs>.
lib.strings.hasSuffix
Determine whether a string has given suffix.
Inputs
-
suffix -
Suffix to check for
-
content -
Input string
Type
hasSuffix :: String -> String -> Bool
Examples
Example
lib.strings.hasSuffix usage example
hasSuffix "foo" "foobar"
=> false
hasSuffix "foo" "barfoo"
=> true
Located at lib/strings.nix:852 in <nixpkgs>.
lib.strings.hasInfix
Determine whether a string contains the given infix
Inputs
-
infix -
1. Function argument
-
content -
2. Function argument
Type
hasInfix :: String -> String -> Bool
Examples
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:905 in <nixpkgs>.
lib.strings.stringToCharacters
Convert a string s 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.
Inputs
-
s -
1. Function argument
Type
stringToCharacters :: String -> [String]
Examples
Example
lib.strings.stringToCharacters usage example
stringToCharacters ""
=> [ ]
stringToCharacters "abc"
=> [ "a" "b" "c" ]
stringToCharacters "🦄"
=> [ "�" "�" "�" "�" ]
Located at lib/strings.nix:955 in <nixpkgs>.
lib.strings.stringAsChars
Manipulate a string character by character and replace them by strings before concatenating the results.
Inputs
-
f -
Function to map over each individual character
-
s -
Input string
Type
stringAsChars :: (String -> String) -> String -> String
Examples
Example
lib.strings.stringAsChars usage example
stringAsChars (x: if x == "a" then "i" else x) "nax"
=> "nix"
Located at lib/strings.nix:986 in <nixpkgs>.
lib.strings.charToInt
Convert char to ascii value, must be in printable range
Inputs
-
c -
1. Function argument
Type
charToInt :: String -> Int
Examples
Example
lib.strings.charToInt usage example
charToInt "A"
=> 65
charToInt "("
=> 40
Located at lib/strings.nix:1020 in <nixpkgs>.
lib.strings.escape
Escape occurrence of the elements of list in string by
prefixing it with a backslash.
Inputs
-
list -
1. Function argument
-
string -
2. Function argument
Type
escape :: [String] -> String -> String
Examples
Example
lib.strings.escape usage example
escape ["(" ")"] "(foo)"
=> "\\(foo\\)"
Located at lib/strings.nix:1051 in <nixpkgs>.
lib.strings.escapeC
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.
Inputs
-
list -
1. Function argument
-
string -
2. Function argument
Type
escapeC :: [String] -> String -> String
Examples
Example
lib.strings.escapeC usage example
escapeC [" "] "foo bar"
=> "foo\\x20bar"
Located at lib/strings.nix:1083 in <nixpkgs>.
lib.strings.escapeURL
Escape the string so it can be safely placed inside a URL
query.
Inputs
-
string -
1. Function argument
Type
escapeURL :: String -> String
Examples
Example
lib.strings.escapeURL usage example
escapeURL "foo/bar baz"
=> "foo%2Fbar%20baz"
Located at lib/strings.nix:1115 in <nixpkgs>.
lib.strings.escapeShellArg
Quote string to be used safely within the Bourne shell if it has any
special characters.
Inputs
-
string -
1. Function argument
Type
escapeShellArg :: String -> String
Examples
Example
lib.strings.escapeShellArg usage example
escapeShellArg "esc'ape\nme"
=> "'esc'\\''ape\nme'"
Located at lib/strings.nix:1217 in <nixpkgs>.
lib.strings.escapeShellArgs
Quote all arguments that have special characters to be safely passed to the Bourne shell.
Inputs
-
args -
1. Function argument
Type
escapeShellArgs :: [String] -> String
Examples
Example
lib.strings.escapeShellArgs usage example
escapeShellArgs ["one" "two three" "four'five"]
=> "one 'two three' 'four'\\''five'"
Located at lib/strings.nix:1253 in <nixpkgs>.
lib.strings.isValidPosixName
Test whether the given name is a valid POSIX shell variable name.
Inputs
-
name -
1. Function argument
Type
isValidPosixName :: String -> Bool
Examples
Example
lib.strings.isValidPosixName usage example
isValidPosixName "foo_bar000"
=> true
isValidPosixName "0-bad.jpg"
=> false
Located at lib/strings.nix:1282 in <nixpkgs>.
lib.strings.toShellVar
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).
Inputs
-
name -
1. Function argument
-
value -
2. Function argument
Type
toShellVar :: String -> (String | [String] | { [String] :: String }) -> String
Examples
Example
lib.strings.toShellVar usage example
''
${toShellVar "foo" "some string"}
[[ "$foo" == "some string" ]]
''
Located at lib/strings.nix:1322 in <nixpkgs>.
lib.strings.toShellVars
Translate an attribute set vars into corresponding shell variable declarations
using toShellVar.
Inputs
-
vars -
1. Function argument
Type
toShellVars :: {
[String] :: String | [String] | { [String] :: String };
} -> String
Examples
Example
lib.strings.toShellVars usage example
let
foo = "value";
bar = foo;
in ''
${toShellVars { inherit foo bar; }}
[[ "$foo" == "$bar" ]]
''
Located at lib/strings.nix:1370 in <nixpkgs>.
lib.strings.escapeNixString
Turn a string s into a Nix expression representing that string
Inputs
-
s -
1. Function argument
Type
escapeNixString :: String -> String
Examples
Example
lib.strings.escapeNixString usage example
escapeNixString "hello\${}\n"
=> "\"hello\\\${}\\n\""
Located at lib/strings.nix:1397 in <nixpkgs>.
lib.strings.escapeRegex
Turn a string s into an exact regular expression
Inputs
-
s -
1. Function argument
Type
escapeRegex :: String -> String
Examples
Example
lib.strings.escapeRegex usage example
escapeRegex "[^a-z]*"
=> "\\[\\^a-z]\\*"
Located at lib/strings.nix:1424 in <nixpkgs>.
lib.strings.escapeNixIdentifier
Quotes a string s if it can't be used as an identifier directly.
Inputs
-
s -
1. Function argument
Type
escapeNixIdentifier :: String -> String
Examples
Example
lib.strings.escapeNixIdentifier usage example
escapeNixIdentifier "hello"
=> "hello"
escapeNixIdentifier "0abc"
=> "\"0abc\""
Located at lib/strings.nix:1453 in <nixpkgs>.
lib.strings.escapeXML
Escapes a string s such that it is safe to include verbatim in an XML
document.
Inputs
-
s -
1. Function argument
Type
escapeXML :: String -> String
Examples
Example
lib.strings.escapeXML usage example
escapeXML ''"test" 'test' < & >''
=> ""test" 'test' < & >"
Located at lib/strings.nix:1502 in <nixpkgs>.
lib.strings.toLower
Converts an ASCII string s to lower-case.
Inputs
-
s -
The string to convert to lower-case.
Type
toLower :: String -> String
Examples
Example
lib.strings.toLower usage example
toLower "HOME"
=> "home"
Located at lib/strings.nix:1536 in <nixpkgs>.
lib.strings.toUpper
Converts an ASCII string s to upper-case.
Inputs
-
s -
The string to convert to upper-case.
Type
toUpper :: String -> String
Examples
Example
lib.strings.toUpper usage example
toUpper "home"
=> "HOME"
Located at lib/strings.nix:1563 in <nixpkgs>.
lib.strings.toSentenceCase
Converts the first character of a string s to upper-case.
Inputs
-
str -
The string to convert to sentence case.
Type
toSentenceCase :: String -> String
Examples
Example
lib.strings.toSentenceCase usage example
toSentenceCase "home"
=> "Home"
Located at lib/strings.nix:1590 in <nixpkgs>.
lib.strings.toCamelCase
Converts a string to camelCase. Handles snake_case, PascalCase, kebab-case strings as well as strings delimited by spaces.
Inputs
-
string -
The string to convert to camelCase
Type
toCamelCase :: String -> String
Examples
Example
lib.strings.toCamelCase usage example
toCamelCase "hello-world"
=> "helloWorld"
toCamelCase "hello_world"
=> "helloWorld"
toCamelCase "hello world"
=> "helloWorld"
toCamelCase "HelloWorld"
=> "helloWorld"
Located at lib/strings.nix:1633 in <nixpkgs>.
lib.strings.addContextFrom
Appends string context from string like object src to target.
Warning
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.
Inputs
-
src -
The string to take the context from. If the argument is not a string, it will be implicitly converted to a string.
-
target -
The string to append the context to. If the argument is not a string, it will be implicitly converted to a string.
Type
addContextFrom :: String -> String -> String
Examples
Example
lib.strings.addContextFrom usage example
pkgs = import <nixpkgs> { };
addContextFrom pkgs.coreutils "bar"
=> "bar"
The context can be displayed using the toString function:
nix-repl> builtins.getContext (lib.strings.addContextFrom pkgs.coreutils "bar")
{
"/nix/store/m1s1d2dk2dqqlw3j90jl3cjy2cykbdxz-coreutils-9.5.drv" = { ... };
}
Located at lib/strings.nix:1709 in <nixpkgs>.
lib.strings.splitString
Cut a string with a separator and produces a list of strings which were separated by this separator.
Inputs
-
sep -
1. Function argument
-
s -
2. Function argument
Type
splitString :: String -> String -> [String]
Examples
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:1742 in <nixpkgs>.
lib.strings.splitStringBy
Splits a string into substrings based on a predicate that examines adjacent characters.
This function provides a flexible way to split strings by checking pairs of characters against a custom predicate function. Unlike simpler splitting functions, this allows for context-aware splitting based on character transitions and patterns.
Inputs
-
predicate -
Function that takes two arguments (previous character and current character) and returns true when the string should be split at the current position. For the first character, previous will be "" (empty string).
-
keepSplit -
Boolean that determines whether the splitting character should be kept as part of the result. If true, the character will be included at the beginning of the next substring; if false, it will be discarded.
-
str -
The input string to split.
Return
A list of substrings from the original string, split according to the predicate.
Type
splitStringBy :: (String -> String -> Bool) -> Bool -> String -> [String]
Examples
Example
lib.strings.splitStringBy usage example
Split on periods and hyphens, discarding the separators:
splitStringBy (prev: curr: builtins.elem curr [ "." "-" ]) false "foo.bar-baz"
=> [ "foo" "bar" "baz" ]
Split on transitions from lowercase to uppercase, keeping the uppercase characters:
splitStringBy (prev: curr: builtins.match "[a-z]" prev != null && builtins.match "[A-Z]" curr != null) true "fooBarBaz"
=> [ "foo" "Bar" "Baz" ]
Handle leading separators correctly:
splitStringBy (prev: curr: builtins.elem curr [ "." ]) false ".foo.bar.baz"
=> [ "" "foo" "bar" "baz" ]
Handle trailing separators correctly:
splitStringBy (prev: curr: builtins.elem curr [ "." ]) false "foo.bar.baz."
=> [ "foo" "bar" "baz" "" ]
Located at lib/strings.nix:1810 in <nixpkgs>.
lib.strings.removePrefix
Returns a string without the specified prefix, if the prefix matches.
Inputs
-
prefix -
Prefix to remove if it matches
-
str -
Input string
Type
removePrefix :: String -> String -> String
Examples
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:1866 in <nixpkgs>.
lib.strings.removeSuffix
Returns a string without the specified suffix, if the suffix matches.
Inputs
-
suffix -
Suffix to remove if it matches
-
str -
Input string
Type
removeSuffix :: String -> String -> String
Examples
Example
lib.strings.removeSuffix usage example
removeSuffix "front" "homefront"
=> "home"
removeSuffix "xxx" "homefront"
=> "homefront"
Located at lib/strings.nix:1916 in <nixpkgs>.
lib.strings.versionOlder
Returns true if string v1 denotes a version older than v2.
Inputs
-
v1 -
1. Function argument
-
v2 -
2. Function argument
Type
versionOlder :: String -> String -> Bool
Examples
Example
lib.strings.versionOlder usage example
versionOlder "1.1" "1.2"
=> true
versionOlder "1.1" "1.1"
=> false
Located at lib/strings.nix:1968 in <nixpkgs>.
lib.strings.versionAtLeast
Returns true if string v1 denotes a version equal to or newer than v2.
Inputs
-
v1 -
1. Function argument
-
v2 -
2. Function argument
Type
versionAtLeast :: String -> String -> Bool
Examples
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:2002 in <nixpkgs>.
lib.strings.getName
This function takes an argument x that's either a derivation or a
derivation's "name" attribute and extracts the name part from that
argument.
Inputs
-
x -
1. Function argument
Type
getName :: String | Derivation -> String
Examples
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:2033 in <nixpkgs>.
lib.strings.getVersion
This function takes an argument x that's either a derivation or a
derivation's "name" attribute and extracts the version part from that
argument.
Inputs
-
x -
1. Function argument
Type
getVersion :: String | Derivation -> String
Examples
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:2068 in <nixpkgs>.
lib.strings.nameFromURL
Extract name and version from a URL as shown in the examples.
Separator sep is used to determine the end of the extension.
Inputs
-
url -
1. Function argument
-
sep -
2. Function argument
Type
nameFromURL :: String -> String
Examples
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:2106 in <nixpkgs>.
lib.strings.cmakeOptionType
Create a "-D<feature>:<type>=<value>" string that can be passed to typical
CMake invocations.
Inputs
-
type -
The type of the feature to be set, as described in the CMake set documentation the possible values (case insensitive) are: BOOL FILEPATH PATH STRING INTERNAL LIST
-
feature -
The feature to be set
-
feature -
The feature to be set
-
value -
The desired value
Type
cmakeOptionType :: String -> String -> String -> String
Examples
Example
lib.strings.cmakeOptionType usage example
cmakeOptionType "string" "ENGINE" "sdl2"
=> "-DENGINE:STRING=sdl2"
Located at lib/strings.nix:2154 in <nixpkgs>.
lib.strings.cmakeBool
Create a "-D<condition>={TRUE,FALSE}" string that can be passed to typical
CMake invocations.
Inputs
-
condition -
The condition to be made true or false
-
flag -
The controlling flag of the condition
Type
cmakeBool :: String -> Bool -> String
Examples
Example
lib.strings.cmakeBool usage example
cmakeBool "ENABLE_STATIC_LIBS" false
=> "-DENABLESTATIC_LIBS:BOOL=FALSE"
Located at lib/strings.nix:2201 in <nixpkgs>.
lib.strings.cmakeFeature
Create a "-D<feature>:STRING=<value>" string that can be passed to typical
CMake invocations.
This is the most typical usage, so it deserves a special case.
Inputs
-
feature -
The feature to be set
-
value -
The desired value
Type
cmakeFeature :: String -> String -> String
Examples
Example
lib.strings.cmakeFeature usage example
cmakeFeature "MODULES" "badblock"
=> "-DMODULES:STRING=badblock"
Located at lib/strings.nix:2236 in <nixpkgs>.
lib.strings.mesonOption
Create a "-D<feature>=<value>" string that can be passed to typical Meson
invocations.
Inputs
-
feature -
The feature to be set
-
value -
The desired value
Type
mesonOption :: String -> String -> String
Examples
Example
lib.strings.mesonOption usage example
mesonOption "engine" "opengl"
=> "-Dengine=opengl"
Located at lib/strings.nix:2267 in <nixpkgs>.
lib.strings.mesonBool
Create a "-D<condition>={true,false}" string that can be passed to typical
Meson invocations.
Inputs
-
condition -
The condition to be made true or false
-
flag -
The controlling flag of the condition
Type
mesonBool :: String -> Bool -> String
Examples
Example
lib.strings.mesonBool usage example
mesonBool "hardened" true
=> "-Dhardened=true"
mesonBool "static" false
=> "-Dstatic=false"
Located at lib/strings.nix:2304 in <nixpkgs>.
lib.strings.mesonEnable
Create a "-D<feature>={enabled,disabled}" string that can be passed to
typical Meson invocations.
Inputs
-
feature -
The feature to be enabled or disabled
-
flag -
The controlling flag
Type
mesonEnable :: String -> Bool -> String
Examples
Example
lib.strings.mesonEnable usage example
mesonEnable "docs" true
=> "-Ddocs=enabled"
mesonEnable "savage" false
=> "-Dsavage=disabled"
Located at lib/strings.nix:2340 in <nixpkgs>.
lib.strings.enableFeature
Create an "--{enable,disable}-<feature>" string that can be passed to
standard GNU Autoconf scripts.
Inputs
-
flag -
1. Function argument
-
feature -
2. Function argument
Type
enableFeature :: Bool -> String -> String
Examples
Example
lib.strings.enableFeature usage example
enableFeature true "shared"
=> "--enable-shared"
enableFeature false "shared"
=> "--disable-shared"
Located at lib/strings.nix:2376 in <nixpkgs>.
lib.strings.enableFeatureAs
Create an "--{enable-<feature>=<value>,disable-<feature>}" string that
can be passed to standard GNU Autoconf scripts.
Inputs
-
flag -
1. Function argument
-
feature -
2. Function argument
-
value -
3. Function argument
Type
enableFeatureAs :: Bool -> String -> String -> String
Examples
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:2416 in <nixpkgs>.
lib.strings.withFeature
Create an "--{with,without}-<feature>" string that can be passed to
standard GNU Autoconf scripts.
Inputs
-
flag -
1. Function argument
-
feature -
2. Function argument
Type
withFeature :: Bool -> String -> String
Examples
Example
lib.strings.withFeature usage example
withFeature true "shared"
=> "--with-shared"
withFeature false "shared"
=> "--without-shared"
Located at lib/strings.nix:2451 in <nixpkgs>.
lib.strings.withFeatureAs
Create an "--{with-<feature>=<value>,without-<feature>}" string that can be passed to
standard GNU Autoconf scripts.
Inputs
-
flag -
1. Function argument
-
feature -
2. Function argument
-
value -
3. Function argument
Type
withFeatureAs :: Bool -> String -> String -> String
Examples
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:2490 in <nixpkgs>.
lib.strings.fixedWidthString
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.
Inputs
-
width -
1. Function argument
-
filler -
2. Function argument
-
str -
3. Function argument
Type
fixedWidthString :: Int -> String -> String -> String
Examples
Example
lib.strings.fixedWidthString usage example
fixedWidthString 5 "0" (toString 15)
=> "00015"
Located at lib/strings.nix:2529 in <nixpkgs>.
lib.strings.fixedWidthNumber
Format a number adding leading zeroes up to fixed width.
Inputs
-
width -
1. Function argument
-
n -
2. Function argument
Type
fixedWidthNumber :: Int -> Int -> String
Examples
Example
lib.strings.fixedWidthNumber usage example
fixedWidthNumber 5 15
=> "00015"
Located at lib/strings.nix:2568 in <nixpkgs>.
lib.strings.floatToString
Convert a float to a string, but emit a warning when precision is lost during the conversion
Inputs
-
float -
1. Function argument
Type
floatToString :: Float -> String
Examples
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:2599 in <nixpkgs>.
lib.strings.isConvertibleWithToString
Check whether a list or other value x 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.
Inputs
-
val -
1. Function argument
Type
isConvertibleWithToString :: Any -> Bool
Located at lib/strings.nix:2624 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.
Inputs
-
x -
1. Function argument
Type
isStringLike :: Any -> Bool
Located at lib/strings.nix:2653 in <nixpkgs>.
lib.strings.isStorePath
Check whether a value x is a store path.
Inputs
-
x -
1. Function argument
Type
isStorePath :: Any -> Bool
Examples
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:2686 in <nixpkgs>.
lib.strings.toInt
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.
Inputs
-
str -
A string to be interpreted as an int.
Type
toInt :: String -> Int
Examples
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:2743 in <nixpkgs>.
lib.strings.toIntBase10
Parse a string as a base 10 int. This supports parsing of zero-padded integers.
Inputs
-
str -
A string to be interpreted as an int.
Type
toIntBase10 :: String -> Int
Examples
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:2813 in <nixpkgs>.
lib.strings.fileContents
Read the contents of a file removing the trailing \n
Inputs
-
file -
1. Function argument
Type
fileContents :: Path -> String
Examples
Example
lib.strings.fileContents usage example
$ echo "1.0" > ./version
fileContents ./version
=> "1.0"
Located at lib/strings.nix:2874 in <nixpkgs>.
lib.strings.sanitizeDerivationName
Creates a valid derivation name from a potentially invalid one.
Inputs
-
string -
1. Function argument
Type
sanitizeDerivationName :: String -> String
Examples
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:2905 in <nixpkgs>.
lib.strings.levenshtein
Computes the Levenshtein distance between two strings a and b.
Complexity O(n*m) where n and m are the lengths of the strings. Algorithm adjusted from this stackoverflow comment
Inputs
-
a -
1. Function argument
-
b -
2. Function argument
Type
levenshtein :: String -> String -> Int
Examples
Example
lib.strings.levenshtein usage example
levenshtein "foo" "foo"
=> 0
levenshtein "book" "hook"
=> 1
levenshtein "hello" "Heyo"
=> 3
Located at lib/strings.nix:2967 in <nixpkgs>.
lib.strings.commonPrefixLength
Returns the length of the prefix that appears in both strings a and b.
Inputs
-
a -
1. Function argument
-
b -
2. Function argument
Type
commonPrefixLength :: String -> String -> Int
Located at lib/strings.nix:3004 in <nixpkgs>.
lib.strings.commonSuffixLength
Returns the length of the suffix common to both strings a and b.
Inputs
-
a -
1. Function argument
-
b -
2. Function argument
Type
commonSuffixLength :: String -> String -> Int
Located at lib/strings.nix:3036 in <nixpkgs>.
lib.strings.levenshteinAtMost
Returns whether the levenshtein distance between two strings a and b is at most some value k.
Complexity is O(min(n,m)) for k <= 2 and O(n*m) otherwise
Inputs
-
k -
Distance threshold
-
a -
String
a -
b -
String
b
Type
levenshteinAtMost :: Int -> String -> String -> Bool
Examples
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:3092 in <nixpkgs>.