You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.1 KiB

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