6
6
# The fetchers. fetch_<type> fetches specs of type <type>.
7
7
#
8
8
9
- fetch_file = pkgs : spec :
10
- if spec . builtin or true then
11
- builtins_fetchurl { inherit ( spec ) url sha256 ; }
12
- else
13
- pkgs . fetchurl { inherit ( spec ) url sha256 ; } ;
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 ; } ;
9
+ fetch_file = pkgs : name : spec :
10
+ let
11
+ name' = sanitizeName name + "-src" ;
12
+ in
13
+ if spec . builtin or true then
14
+ builtins_fetchurl { inherit ( spec ) url sha256 ; name = name' ; }
15
+ else
16
+ pkgs . fetchurl { inherit ( spec ) url sha256 ; name = name' ; } ;
20
17
21
- fetch_git = spec :
22
- builtins . fetchGit { url = spec . repo ; inherit ( spec ) rev ref ; } ;
18
+ fetch_tarball = pkgs : name : spec :
19
+ let
20
+ name' = sanitizeName name + "-src" ;
21
+ in
22
+ if spec . builtin or true then
23
+ builtins_fetchTarball { name = name' ; inherit ( spec ) url sha256 ; }
24
+ else
25
+ pkgs . fetchzip { name = name' ; inherit ( spec ) url sha256 ; } ;
23
26
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`.
27
+ fetch_git = name : spec :
28
+ let
29
+ ref =
30
+ if spec ? ref then spec . ref else
31
+ if spec ? branch then "refs/heads/${ spec . branch } " else
32
+ if spec ? tag then "refs/tags/${ spec . tag } " else
33
+ abort "In git source '${ name } ': Please specify `ref`, `tag` or `branch`!" ;
34
+ in
35
+ builtins . fetchGit { url = spec . repo ; inherit ( spec ) rev ; inherit ref ; } ;
30
36
31
- $ niv modify <package> -a type=tarball -a builtin=true
32
- ''
33
- builtins_fetchTarball { inherit ( spec ) url sha256 ; } ;
37
+ fetch_local = spec : spec . path ;
34
38
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`.
39
+ fetch_builtin-tarball = name : throw
40
+ ''[${ name } ] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
41
+ $ niv modify ${ name } -a type=tarball -a builtin=true'' ;
41
42
42
- $ niv modify <package> -a type=file -a builtin=true
43
- ''
44
- ( builtins_fetchurl { inherit ( spec ) url sha256 ; } ) ;
43
+ fetch_builtin-url = name : throw
44
+ ''[ ${ name } ] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
45
+ $ niv modify ${ name } -a type=file -a builtin=true'' ;
45
46
46
47
#
47
48
# Various helpers
48
49
#
49
50
51
+ # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
52
+ sanitizeName = name :
53
+ (
54
+ concatMapStrings ( s : if builtins . isList s then "-" else s )
55
+ (
56
+ builtins . split "[^[:alnum:]+._?=-]+"
57
+ ( ( x : builtins . elemAt ( builtins . match "\\ .*(.*)" x ) 0 ) name )
58
+ )
59
+ ) ;
60
+
50
61
# The set of packages used when specs are fetched using non-builtins.
51
- mkPkgs = sources :
62
+ mkPkgs = sources : system :
52
63
let
53
64
sourcesNixpkgs =
54
- import ( builtins_fetchTarball { inherit ( sources . nixpkgs ) url sha256 ; } ) { } ;
65
+ import ( builtins_fetchTarball { inherit ( sources . nixpkgs ) url sha256 ; } ) { inherit system ; } ;
55
66
hasNixpkgsPath = builtins . any ( x : x . prefix == "nixpkgs" ) builtins . nixPath ;
56
67
hasThisAsNixpkgsPath = <nixpkgs> == ./. ;
57
68
in
71
82
72
83
if ! builtins . hasAttr "type" spec then
73
84
abort "ERROR: niv spec ${ name } does not have a 'type' attribute"
74
- else if spec . type == "file" then fetch_file pkgs spec
75
- else if spec . type == "tarball" then fetch_tarball pkgs spec
76
- 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
85
+ else if spec . type == "file" then fetch_file pkgs name spec
86
+ else if spec . type == "tarball" then fetch_tarball pkgs name spec
87
+ else if spec . type == "git" then fetch_git name spec
88
+ else if spec . type == "local" then fetch_local spec
89
+ else if spec . type == "builtin-tarball" then fetch_builtin-tarball name
90
+ else if spec . type == "builtin-url" then fetch_builtin-url name
79
91
else
80
92
abort "ERROR: niv spec ${ name } has unknown type ${ builtins . toJSON spec . type } " ;
81
93
94
+ # If the environment variable NIV_OVERRIDE_${name} is set, then use
95
+ # the path directly as opposed to the fetched source.
96
+ replace = name : drv :
97
+ let
98
+ saneName = stringAsChars ( c : if isNull ( builtins . match "[a-zA-Z0-9]" c ) then "_" else c ) name ;
99
+ ersatz = builtins . getEnv "NIV_OVERRIDE_${ saneName } " ;
100
+ in
101
+ if ersatz == "" then drv else
102
+ # this turns the string into an actual Nix path (for both absolute and
103
+ # relative paths)
104
+ if builtins . substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins . getEnv "PWD" + "/${ ersatz } " ;
105
+
82
106
# Ports of functions for older nix versions
83
107
84
108
# a Nix version of mapAttrs if the built-in doesn't exist
87
111
listToAttrs ( map ( attr : { name = attr ; value = f attr set . ${ attr } ; } ) ( attrNames set ) )
88
112
) ;
89
113
114
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
115
+ range = first : last : if first > last then [ ] else builtins . genList ( n : first + n ) ( last - first + 1 ) ;
116
+
117
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
118
+ stringToCharacters = s : map ( p : builtins . substring p 1 s ) ( range 0 ( builtins . stringLength s - 1 ) ) ;
119
+
120
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
121
+ stringAsChars = f : s : concatStrings ( map f ( stringToCharacters s ) ) ;
122
+ concatMapStrings = f : list : concatStrings ( map f list ) ;
123
+ concatStrings = builtins . concatStringsSep "" ;
124
+
125
+ # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
126
+ optionalAttrs = cond : as : if cond then as else { } ;
127
+
90
128
# fetchTarball version that is compatible between all the versions of Nix
91
- builtins_fetchTarball = { url , sha256 } @attrs :
129
+ builtins_fetchTarball = { url , name ? null , sha256 } @attrs :
92
130
let
93
131
inherit ( builtins ) lessThan nixVersion fetchTarball ;
94
132
in
95
133
if lessThan nixVersion "1.12" then
96
- fetchTarball { inherit url ; }
134
+ fetchTarball ( { inherit url ; } // ( optionalAttrs ( ! isNull name ) { inherit name ; } ) )
97
135
else
98
136
fetchTarball attrs ;
99
137
100
138
# fetchurl version that is compatible between all the versions of Nix
101
- builtins_fetchurl = { url , sha256 } @attrs :
139
+ builtins_fetchurl = { url , name ? null , sha256 } @attrs :
102
140
let
103
141
inherit ( builtins ) lessThan nixVersion fetchurl ;
104
142
in
105
143
if lessThan nixVersion "1.12" then
106
- fetchurl { inherit url ; }
144
+ fetchurl ( { inherit url ; } // ( optionalAttrs ( ! isNull name ) { inherit name ; } ) )
107
145
else
108
146
fetchurl attrs ;
109
147
@@ -115,20 +153,22 @@ let
115
153
then abort
116
154
"The values in sources.json should not have an 'outPath' attribute"
117
155
else
118
- spec // { outPath = fetch config . pkgs name spec ; }
156
+ spec // { outPath = replace name ( fetch config . pkgs name spec ) ; }
119
157
) config . sources ;
120
158
121
159
# The "config" used by the fetchers
122
160
mkConfig =
123
- { sourcesFile ? ./sources.json
124
- , sources ? builtins . fromJSON ( builtins . readFile sourcesFile )
125
- , pkgs ? mkPkgs sources
161
+ { sourcesFile ? if builtins . pathExists ./sources.json then ./sources.json else null
162
+ , sources ? if isNull sourcesFile then { } else builtins . fromJSON ( builtins . readFile sourcesFile )
163
+ , system ? builtins . currentSystem
164
+ , pkgs ? mkPkgs sources system
126
165
} : rec {
127
166
# The sources, i.e. the attribute set of spec name to spec
128
167
inherit sources ;
129
168
130
169
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
131
170
inherit pkgs ;
132
171
} ;
172
+
133
173
in
134
174
mkSources ( mkConfig { } ) // { __functor = _ : settings : mkSources ( mkConfig settings ) ; }
0 commit comments