@ -12,6 +12,8 @@ rec {
/* C o n c a t e n a t e a l i s t o f s t r i n g s .
Type : concatStrings : : [ string ] -> string
Example :
concatStrings [ " f o o " " b a r " ]
= > " f o o b a r "
@ -20,15 +22,19 @@ rec {
/* M a p a f u n c t i o n o v e r a l i s t a n d c o n c a t e n a t e t h e r e s u l t i n g s t r i n g s .
Type : concatMapStrings : : ( a -> string ) -> [ a ] -> string
Example :
concatMapStrings ( x : " a " + x ) [ " f o o " " b a r " ]
= > " a f o o a b a r "
* /
concatMapStrings = f : list : concatStrings ( map f list ) ;
/* L i k e ` c o n c a t M a p S t r i n g s ' e x c e p t t h a t t h e f f u n c t i o n s a l s o g e t s t h e
/* L i k e ` c o n c a t M a p S t r i n g s ` e x c e p t t h a t t h e f f u n c t i o n s a l s o g e t s t h e
position as a parameter .
Type : concatImapStrings : : ( int -> a -> string ) -> [ a ] -> string
Example :
concatImapStrings ( pos : x : " ${ toString pos } - ${ x } " ) [ " f o o " " b a r " ]
= > " 1 - f o o 2 - b a r "
@ -37,17 +43,25 @@ rec {
/* P l a c e a n e l e m e n t b e t w e e n e a c h e l e m e n t o f a l i s t
Type : intersperse : : a -> [ a ] -> [ a ]
Example :
intersperse " / " [ " u s r " " l o c a l " " b i n " ]
= > [ " u s r " " / " " l o c a l " " / " " b i n " ] .
* /
intersperse = separator : list :
intersperse =
# Separator to add between elements
separator :
# Input list
list :
if list == [ ] || length list == 1
then list
else tail ( lib . concatMap ( x : [ separator x ] ) list ) ;
/* C o n c a t e n a t e a l i s t o f s t r i n g s w i t h a s e p a r a t o r b e t w e e n e a c h e l e m e n t
Type : concatStringsSep : : string -> [ string ] -> string
Example :
concatStringsSep " / " [ " u s r " " l o c a l " " b i n " ]
= > " u s r / l o c a l / b i n "
@ -55,43 +69,77 @@ rec {
concatStringsSep = builtins . concatStringsSep or ( separator : list :
concatStrings ( intersperse separator list ) ) ;
/* F i r s t m a p s o v e r t h e l i s t a n d t h e n c o n c a t e n a t e s i t .
/* M a p s a f u n c t i o n o v e r a l i s t o f s t r i n g s a n d t h e n c o n c a t e n a t e s t h e
result with the specified separator interspersed between
elements .
Type : concatMapStringsSep : : string -> ( string -> string ) -> [ string ] -> string
Example :
concatMapStringsSep " - " ( x : toUpper x ) [ " f o o " " b a r " " b a z " ]
= > " F O O - B A R - B A Z "
* /
concatMapStringsSep = sep : f : list : concatStringsSep sep ( map f list ) ;
concatMapStringsSep =
# Separator to add between elements
sep :
# Function to map over the list
f :
# List of input strings
list : concatStringsSep sep ( map f list ) ;
/* F i r s t i m a p s o v e r t h e l i s t a n d t h e n c o n c a t e n a t e s i t .
/* S a m e a s ` c o n c a t M a p S t r i n g s S e p ` , b u t t h e m a p p i n g f u n c t i o n
additionally receives the position of its argument .
Example :
Type : concatMapStringsSep : : string -> ( int -> string -> string ) -> [ string ] -> string
Example :
concatImapStringsSep " - " ( pos : x : toString ( x / pos ) ) [ 6 6 6 ]
= > " 6 - 3 - 2 "
* /
concatImapStringsSep = sep : f : list : concatStringsSep sep ( lib . imap1 f list ) ;
concatImapStringsSep =
# Separator to add between elements
sep :
# Function that receives elements and their positions
f :
# List of input strings
list : concatStringsSep sep ( lib . imap1 f list ) ;
/* C o n s t r u c t a U n i x - s t y l e s e a r c h p a t h c o n s i s t i n g o f e a c h ` s u b D i r "
directory of the given list of packages .
/* C o n s t r u c t a U n i x - s t y l e , c o l o n - s e p a r a t e d s e a r c h p a t h c o n s i s t i n g o f
the given ` subDir ` appended to each of the given paths .
Type : makeSearchPath : : string -> [ string ] -> string
Example :
makeSearchPath " b i n " [ " / r o o t " " / u s r " " / u s r / l o c a l " ]
= > " / r o o t / b i n : / u s r / b i n : / u s r / l o c a l / b i n "
makeSearchPath " b i n " [ " / "]
= > " / / bi n "
makeSearchPath " b i n " [ " " ]
= > " / b i n "
* /
makeSearchPath = subDir : packages :
concatStringsSep " : " ( map ( path : path + " / " + subDir ) ( builtins . filter ( x : x != null ) packages ) ) ;
makeSearchPath =
# Directory name to append
subDir :
# List of base paths
paths :
concatStringsSep " : " ( map ( path : path + " / " + subDir ) ( builtins . filter ( x : x != null ) paths ) ) ;
/* C o n s t r u c t a U n i x - s t y l e s e a r c h p a t h b y a p p e n d i n g t h e g i v e n
` 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 .
/* C o n s t r u c t a U n i x - s t y l e s e a r c h p a t h , u s i n g g i v e n p a c k a g e o u t p u t .
If no output is found , fallback to ` . out ` and then to the default .
Type : string -> string -> [ package ] -> string
Example :
makeSearchPathOutput " d e v " " b i n " [ pkgs . openssl pkgs . zlib ]
= > " / n i x / s t o r e / 9 r z 8 g x h z f 8 s w 4 k f 2 j 2 f 1 g r r 4 9 w 8 z x 5 v j - o p e n s s l - 1 . 0 . 1 r - d e v / b i n : / n i x / s t o r e / w w h 7 m h w h 2 6 9 s f j k m 6 k 5 6 6 5 b 5 k g p 7 j r k 2 - z l i b - 1 . 2 . 8 / b i n "
* /
makeSearchPathOutput = output : subDir : pkgs : makeSearchPath subDir ( map ( lib . getOutput output ) pkgs ) ;
makeSearchPathOutput =
# Package output to use
output :
# Directory name to append
subDir :
# List of packages
pkgs : makeSearchPath subDir ( map ( lib . getOutput output ) pkgs ) ;
/* C o n s t r u c t a l i b r a r y s e a r c h p a t h ( s u c h a s R P A T H ) c o n t a i n i n g t h e
libraries for a set of packages
@ -117,13 +165,12 @@ rec {
/* C o n s t r u c t a p e r l s e a r c h p a t h ( s u c h a s $ P E R L 5 L I B )
FIXME ( zimbatm ) : this should be moved in perl-specific code
Example :
pkgs = import <nixpkgs> { }
makePerlPath [ pkgs . perlPackages . libnet ]
= > " / n i x / s t o r e / n 0 m 1 f k 9 c 9 6 0 d 8 w l r s 6 2 s n c n a d y g q q c 6 y - p e r l - N e t - S M T P - 1 . 2 5 / l i b / p e r l 5 / s i t e _ p e r l "
* /
# FIXME(zimbatm): this should be moved in perl-specific code
makePerlPath = makeSearchPathOutput " l i b " " l i b / p e r l 5 / s i t e _ p e r l " ;
/* C o n s t r u c t a p e r l s e a r c h p a t h r e c u r s i v e l y i n c l u d i n g a l l d e p e n d e n c i e s ( s u c h a s $ P E R L 5 L I B )
@ -138,34 +185,51 @@ rec {
/* D e p e n d i n g o n t h e b o o l e a n ` c o n d ' , r e t u r n e i t h e r t h e g i v e n s t r i n g
or the empty string . Useful to concatenate against a bigger string .
Type : optionalString : : bool -> string -> string
Example :
optionalString true " s o m e - s t r i n g "
= > " s o m e - s t r i n g "
optionalString false " s o m e - s t r i n g "
= > " "
* /
optionalString = cond : string : if cond then string else " " ;
optionalString =
# Condition
cond :
# String to return if condition is true
string : if cond then string else " " ;
/* D e t e r m i n e w h e t h e r a s t r i n g h a s g i v e n p r e f i x .
Type : hasPrefix : : string -> string -> bool
Example :
hasPrefix " f o o " " f o o b a r "
= > true
hasPrefix " f o o " " b a r f o o "
= > false
* /
hasPrefix = pref : str :
substring 0 ( stringLength pref ) str == pref ;
hasPrefix =
# Prefix to check for
pref :
# Input string
str : substring 0 ( stringLength pref ) str == pref ;
/* D e t e r m i n e w h e t h e r a s t r i n g h a s g i v e n s u f f i x .
Type : hasSuffix : : string -> string -> bool
Example :
hasSuffix " f o o " " f o o b a r "
= > false
hasSuffix " f o o " " b a r f o o "
= > true
* /
hasSuffix = suffix : content :
hasSuffix =
# Suffix to check for
suffix :
# Input string
content :
let
lenContent = stringLength content ;
lenSuffix = stringLength suffix ;
@ -180,6 +244,8 @@ rec {
Also note that Nix treats strings as a list of bytes and thus doesn't
handle unicode .
Type : stringtoCharacters : : string -> [ string ]
Example :
stringToCharacters " "
= > [ ]
@ -194,18 +260,25 @@ rec {
/* M a n i p u l a t e a s t r i n g c h a r a c t e r b y c h a r a c t e r a n d r e p l a c e t h e m b y
strings before concatenating the results .
Type : stringAsChars : : ( string -> string ) -> string -> string
Example :
stringAsChars ( x : if x == " a " then " i " else x ) " n a x "
= > " n i x "
* /
stringAsChars = f : s :
concatStrings (
stringAsChars =
# Function to map over each individual character
f :
# Input string
s : concatStrings (
map f ( stringToCharacters s )
) ;
/* E s c a p e o c c u r r e n c e o f t h e e l e m e n t s o f ‘ l i s t ’ i n ‘ s t r i n g ’ b y
/* E s c a p e o c c u r r e n c e o f t h e e l e m e n t s o f ` l i s t ` i n ` s t r i n g ` b y
prefixing it with a backslash .
Type : escape : : [ string ] -> string -> string
Example :
escape [ " ( " " ) " ] " ( f o o ) "
= > " \\ ( f o o \\ ) "
@ -214,6 +287,8 @@ rec {
/* Q u o t e s t r i n g t o b e u s e d s a f e l y w i t h i n t h e B o u r n e s h e l l .
Type : escapeShellArg : : string -> string
Example :
escapeShellArg " e s c ' a p e \n m e "
= > " ' e s c ' \\ ' ' a p e \n m e ' "
@ -222,6 +297,8 @@ rec {
/* Q u o t e a l l a r g u m e n t s t o b e s a f e l y p a s s e d t o t h e B o u r n e s h e l l .
Type : escapeShellArgs : : [ string ] -> string
Example :
escapeShellArgs [ " o n e " " t w o t h r e e " " f o u r ' f i v e " ]
= > " ' o n e ' ' t w o t h r e e ' ' f o u r ' \\ ' ' f i v e ' "
@ -230,13 +307,15 @@ rec {
/* T u r n a s t r i n g i n t o a N i x e x p r e s s i o n r e p r e s e n t i n g t h a t s t r i n g
Type : string -> string
Example :
escapeNixString " h e l l o \$ { } \n "
= > " \" h e l l o \\ \$ { } \\ n \" "
* /
escapeNixString = s : escape [ " $ " ] ( builtins . toJSON s ) ;
/* O b s o l e t e - u s e r e p l a c e S t r i n g s i n s t e a d . */
# Obsolete - use replaceStrings instead.
replaceChars = builtins . replaceStrings or (
del : new : s :
let
@ -256,6 +335,8 @@ rec {
/* C o n v e r t s a n A S C I I s t r i n g t o l o w e r - c a s e .
Type : toLower : : string -> string
Example :
toLower " H O M E "
= > " h o m e "
@ -264,6 +345,8 @@ rec {
/* C o n v e r t s a n A S C I I s t r i n g t o u p p e r - c a s e .
Type : toUpper : : string -> string
Example :
toUpper " h o m e "
= > " H O M E "
@ -273,7 +356,7 @@ rec {
/* A p p e n d s s t r i n g c o n t e x t f r o m a n o t h e r s t r i n g . T h i s i s a n i m p l e m e n t a t i o n
detail of Nix .
Strings in Nix carry an invisible ` context' which is a list of strings
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 .
@ -319,8 +402,9 @@ rec {
in
recurse 0 0 ;
/* R e t u r n t h e s u f f i x o f t h e s e c o n d a r g u m e n t i f t h e f i r s t a r g u m e n t m a t c h e s
its prefix .
/* R e t u r n a s t r i n g w i t h o u t t h e s p e c i f i e d p r e f i x , i f t h e p r e f i x m a t c h e s .
Type : string -> string -> string
Example :
removePrefix " f o o . " " f o o . b a r . b a z "
@ -328,18 +412,23 @@ rec {
removePrefix " x x x " " f o o . b a r . b a z "
= > " f o o . b a r . b a z "
* /
removePrefix = pre : s :
removePrefix =
# Prefix to remove if it matches
prefix :
# Input string
str :
let
preLen = stringLength pre ;
sLen = stringLength s ;
preLen = stringLength prefix ;
sLen = stringLength str ;
in
if hasPrefix pre s then
substring preLen ( sLen - preLen ) s
if hasPrefix prefix str then
substring preLen ( sLen - preLen ) str
else
s ;
str ;
/* R e t u r n a s t r i n g w i t h o u t t h e s p e c i f i e d s u f f i x , i f t h e s u f f i x m a t c h e s .
/* R e t u r n t h e p r e f i x o f t h e s e c o n d a r g u m e n t i f t h e f i r s t a r g u m e n t m a t c h e s
its suffix .
Type : string -> string -> string
Example :
removeSuffix " f r o n t " " h o m e f r o n t "
@ -347,17 +436,21 @@ rec {
removeSuffix " x x x " " h o m e f r o n t "
= > " h o m e f r o n t "
* /
removeSuffix = suf : s :
removeSuffix =
# Suffix to remove if it matches
suffix :
# Input string
str :
let
sufLen = stringLength suf ;
sLen = stringLength s ;
sufLen = stringLength suffix ;
sLen = stringLength str ;
in
if sufLen <= sLen && suf == substring ( sLen - sufLen ) sufLen s then
substring 0 ( sLen - sufLen ) s
if sufLen <= sLen && suffix == substring ( sLen - sufLen ) sufLen str then
substring 0 ( sLen - sufLen ) str
else
s ;
str ;
/* R e t u r n t r u e i f f s t r i n g v 1 d e n o t e s a v e r s i o n o l d e r t h a n v 2 .
/* R e t u r n t r u e i f s t r i n g v 1 d e n o t e s a v e r s i o n o l d e r t h a n v 2 .
Example :
versionOlder " 1 . 1 " " 1 . 2 "
@ -367,7 +460,7 @@ rec {
* /
versionOlder = v1 : v2 : builtins . compareVersions v2 v1 == 1 ;
/* R e t u r n t r u e i f f s t r i n g v 1 d e n o t e s a v e r s i o n e q u a l t o o r n e w e r t h a n v 2 .
/* R e t u r n t r u e i f s t r i n g v 1 d e n o t e s a v e r s i o n e q u a l t o o r n e w e r t h a n v 2 .
Example :
versionAtLeast " 1 . 1 " " 1 . 0 "
@ -459,6 +552,11 @@ rec {
/* C r e a t e a f i x e d w i d t h s t r i n g w i t h a d d i t i o n a l p r e f i x t o m a t c h
required width .
This function will fail if the input string is longer than the
requested length .
Type : fixedWidthString : : int -> string -> string
Example :
fixedWidthString 5 " 0 " ( toString 15 )
= > " 0 0 0 1 5 "
@ -506,8 +604,9 @@ rec {
&& builtins . substring 0 1 ( toString x ) == " / "
&& dirOf x == builtins . storeDir ;
/* C o n v e r t s t r i n g t o i n t
Obviously , it is a bit hacky to use fromJSON that way .
/* P a r s e a s t r i n g s t r i n g a s a n i n t .
Type : string -> int
Example :
toInt " 1 3 3 7 "
@ -517,17 +616,18 @@ rec {
toInt " 3 . 1 4 "
= > error : floating point JSON numbers are not supported
* /
# Obviously, it is a bit hacky to use fromJSON this way.
toInt = str :
let may_be_int = builtins . fromJSON str ; in
if builtins . isInt may_be_int
then may_be_int
else throw " C o u l d n o t c o n v e r t ${ str } t o i n t . " ;
/* R e a d a l i s t o f p a t h s f r o m ` f i l e ' , r e l a t i v e t o t h e ` r o o t P a t h ' . L i n e s
beginning with ` #' are treated as comments and ignored. Whitespace
is significant .
/* R e a d a l i s t o f p a t h s f r o m ` f i l e ` , r e l a t i v e t o t h e ` r o o t P a t h ` .
Lines beginning with ` #` are treated as comments and ignored.
Whitespace is significant .
NOTE : t his function is not performant and should be avoided
NOTE : T his function is not performant and should be avoided .
Example :
readPathsFromFile /prefix
@ -549,6 +649,8 @@ rec {
/* R e a d t h e c o n t e n t s o f a f i l e r e m o v i n g t h e t r a i l i n g \ n
Type : fileContents : : path -> string
Example :
$ echo " 1 . 0 " > ./version