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.

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