1212 else
1313 pkgs . fetchurl { inherit ( spec ) url sha256 ; } ;
1414
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 ; } ;
2025
2126 fetch_git = spec :
2227 builtins . fetchGit { url = spec . repo ; inherit ( spec ) rev ref ; } ;
2328
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 ;
3430
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'' ;
4134
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'' ;
4538
4639 #
4740 # Various helpers
7265 if ! builtins . hasAttr "type" spec then
7366 abort "ERROR: niv spec ${ name } does not have a 'type' attribute"
7467 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
7669 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
7973 else
8074 abort "ERROR: niv spec ${ name } has unknown type ${ builtins . toJSON spec . type } " ;
8175
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+
8285 # Ports of functions for older nix versions
8386
8487 # a Nix version of mapAttrs if the built-in doesn't exist
8790 listToAttrs ( map ( attr : { name = attr ; value = f attr set . ${ attr } ; } ) ( attrNames set ) )
8891 ) ;
8992
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+
90103 # fetchTarball version that is compatible between all the versions of Nix
91- builtins_fetchTarball = { url , sha256 } @attrs :
104+ builtins_fetchTarball = { url , name , sha256 } @attrs :
92105 let
93106 inherit ( builtins ) lessThan nixVersion fetchTarball ;
94107 in
95108 if lessThan nixVersion "1.12" then
96- fetchTarball { inherit url ; }
109+ fetchTarball { inherit name url ; }
97110 else
98111 fetchTarball attrs ;
99112
@@ -115,13 +128,13 @@ let
115128 then abort
116129 "The values in sources.json should not have an 'outPath' attribute"
117130 else
118- spec // { outPath = fetch config . pkgs name spec ; }
131+ spec // { outPath = replace name ( fetch config . pkgs name spec ) ; }
119132 ) config . sources ;
120133
121134 # The "config" used by the fetchers
122135 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 )
125138 , pkgs ? mkPkgs sources
126139 } : rec {
127140 # The sources, i.e. the attribute set of spec name to spec
130143 # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
131144 inherit pkgs ;
132145 } ;
146+
133147in
134148mkSources ( mkConfig { } ) // { __functor = _ : settings : mkSources ( mkConfig settings ) ; }
0 commit comments