12
12
else
13
13
pkgs . fetchurl { inherit ( spec ) url sha256 ; } ;
14
14
15
- fetch_tarball = pkgs : spec :
16
- if spec . builtin or true then
17
- builtins_fetchTarball { inherit ( spec ) url sha256 ; }
18
- else
19
- pkgs . fetchzip { inherit ( spec ) url sha256 ; } ;
15
+ fetch_tarball = pkgs : name : spec :
16
+ let
17
+ ok = str : ! builtins . isNull ( builtins . match "[a-zA-Z0-9+-._?=]" str ) ;
18
+ # sanitize the name, though nix will still fail if name starts with period
19
+ name' = stringAsChars ( x : if ! ok x then "-" else x ) "${ name } -src" ;
20
+ in
21
+ if spec . builtin or true then
22
+ builtins_fetchTarball { name = name' ; inherit ( spec ) url sha256 ; }
23
+ else
24
+ pkgs . fetchzip { name = name' ; inherit ( spec ) url sha256 ; } ;
20
25
21
26
fetch_git = spec :
22
27
builtins . fetchGit { url = spec . repo ; inherit ( spec ) rev ref ; } ;
23
28
24
- fetch_builtin-tarball = spec :
25
- builtins . trace
26
- ''
27
- WARNING:
28
- The niv type "builtin-tarball" will soon be deprecated. You should
29
- instead use `builtin = true`.
30
-
31
- $ niv modify <package> -a type=tarball -a builtin=true
32
- ''
33
- builtins_fetchTarball { inherit ( spec ) url sha256 ; } ;
29
+ fetch_local = spec : spec . path ;
34
30
35
- fetch_builtin-url = spec :
36
- builtins . trace
37
- ''
38
- WARNING:
39
- The niv type "builtin-url" will soon be deprecated. You should
40
- instead use `builtin = true`.
31
+ fetch_builtin-tarball = name : throw
32
+ ''[${ name } ] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
33
+ $ niv modify ${ name } -a type=tarball -a builtin=true'' ;
41
34
42
- $ niv modify <package> -a type=file -a builtin=true
43
- ''
44
- ( builtins_fetchurl { inherit ( spec ) url sha256 ; } ) ;
35
+ fetch_builtin-url = name : throw
36
+ ''[ ${ name } ] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
37
+ $ niv modify ${ name } -a type=file -a builtin=true'' ;
45
38
46
39
#
47
40
# Various helpers
72
65
if ! builtins . hasAttr "type" spec then
73
66
abort "ERROR: niv spec ${ name } does not have a 'type' attribute"
74
67
else if spec . type == "file" then fetch_file pkgs spec
75
- else if spec . type == "tarball" then fetch_tarball pkgs spec
68
+ else if spec . type == "tarball" then fetch_tarball pkgs name spec
76
69
else if spec . type == "git" then fetch_git spec
77
- else if spec . type == "builtin-tarball" then fetch_builtin-tarball spec
78
- else if spec . type == "builtin-url" then fetch_builtin-url spec
70
+ else if spec . type == "local" then fetch_local spec
71
+ else if spec . type == "builtin-tarball" then fetch_builtin-tarball name
72
+ else if spec . type == "builtin-url" then fetch_builtin-url name
79
73
else
80
74
abort "ERROR: niv spec ${ name } has unknown type ${ builtins . toJSON spec . type } " ;
81
75
76
+ # If the environment variable NIV_OVERRIDE_${name} is set, then use
77
+ # the path directly as opposed to the fetched source.
78
+ replace = name : drv :
79
+ let
80
+ saneName = stringAsChars ( c : if isNull ( builtins . match "[a-zA-Z0-9]" c ) then "_" else c ) name ;
81
+ ersatz = builtins . getEnv "NIV_OVERRIDE_${ saneName } " ;
82
+ in
83
+ if ersatz == "" then drv else ersatz ;
84
+
82
85
# Ports of functions for older nix versions
83
86
84
87
# a Nix version of mapAttrs if the built-in doesn't exist
87
90
listToAttrs ( map ( attr : { name = attr ; value = f attr set . ${ attr } ; } ) ( attrNames set ) )
88
91
) ;
89
92
93
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
94
+ range = first : last : if first > last then [ ] else builtins . genList ( n : first + n ) ( last - first + 1 ) ;
95
+
96
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
97
+ stringToCharacters = s : map ( p : builtins . substring p 1 s ) ( range 0 ( builtins . stringLength s - 1 ) ) ;
98
+
99
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
100
+ stringAsChars = f : s : concatStrings ( map f ( stringToCharacters s ) ) ;
101
+ concatStrings = builtins . concatStringsSep "" ;
102
+
90
103
# fetchTarball version that is compatible between all the versions of Nix
91
- builtins_fetchTarball = { url , sha256 } @attrs :
104
+ builtins_fetchTarball = { url , name , sha256 } @attrs :
92
105
let
93
106
inherit ( builtins ) lessThan nixVersion fetchTarball ;
94
107
in
95
108
if lessThan nixVersion "1.12" then
96
- fetchTarball { inherit url ; }
109
+ fetchTarball { inherit name url ; }
97
110
else
98
111
fetchTarball attrs ;
99
112
@@ -115,13 +128,13 @@ let
115
128
then abort
116
129
"The values in sources.json should not have an 'outPath' attribute"
117
130
else
118
- spec // { outPath = fetch config . pkgs name spec ; }
131
+ spec // { outPath = replace name ( fetch config . pkgs name spec ) ; }
119
132
) config . sources ;
120
133
121
134
# The "config" used by the fetchers
122
135
mkConfig =
123
- { sourcesFile ? . /sources.json
124
- , sources ? builtins . fromJSON ( builtins . readFile sourcesFile )
136
+ { sourcesFile ? if builtins . pathExists . /sources.json then ./sources.json else null
137
+ , sources ? if isNull sourcesFile then { } else builtins . fromJSON ( builtins . readFile sourcesFile )
125
138
, pkgs ? mkPkgs sources
126
139
} : rec {
127
140
# The sources, i.e. the attribute set of spec name to spec
130
143
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
131
144
inherit pkgs ;
132
145
} ;
146
+
133
147
in
134
148
mkSources ( mkConfig { } ) // { __functor = _ : settings : mkSources ( mkConfig settings ) ; }
0 commit comments