sources.nix 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: spec:
  7. if spec.builtin or true then
  8. builtins_fetchurl { inherit (spec) url sha256; }
  9. else
  10. pkgs.fetchurl { inherit (spec) url sha256; };
  11. fetch_tarball = pkgs: name: spec:
  12. let
  13. ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str);
  14. # sanitize the name, though nix will still fail if name starts with period
  15. name' = stringAsChars (x: if ! ok x then "-" else x) "${name}-src";
  16. in
  17. if spec.builtin or true then
  18. builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
  19. else
  20. pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
  21. fetch_git = spec:
  22. builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
  23. fetch_local = spec: spec.path;
  24. fetch_builtin-tarball = name: throw
  25. ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
  26. $ niv modify ${name} -a type=tarball -a builtin=true'';
  27. fetch_builtin-url = name: throw
  28. ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
  29. $ niv modify ${name} -a type=file -a builtin=true'';
  30. #
  31. # Various helpers
  32. #
  33. # The set of packages used when specs are fetched using non-builtins.
  34. mkPkgs = sources:
  35. let
  36. sourcesNixpkgs =
  37. import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
  38. hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
  39. hasThisAsNixpkgsPath = <nixpkgs> == ./.;
  40. in
  41. if builtins.hasAttr "nixpkgs" sources
  42. then sourcesNixpkgs
  43. else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
  44. import <nixpkgs> {}
  45. else
  46. abort
  47. ''
  48. Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
  49. add a package called "nixpkgs" to your sources.json.
  50. '';
  51. # The actual fetching function.
  52. fetch = pkgs: name: spec:
  53. if ! builtins.hasAttr "type" spec then
  54. abort "ERROR: niv spec ${name} does not have a 'type' attribute"
  55. else if spec.type == "file" then fetch_file pkgs spec
  56. else if spec.type == "tarball" then fetch_tarball pkgs name spec
  57. else if spec.type == "git" then fetch_git spec
  58. else if spec.type == "local" then fetch_local spec
  59. else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
  60. else if spec.type == "builtin-url" then fetch_builtin-url name
  61. else
  62. abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
  63. # If the environment variable NIV_OVERRIDE_${name} is set, then use
  64. # the path directly as opposed to the fetched source.
  65. replace = name: drv:
  66. let
  67. saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
  68. ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
  69. in
  70. if ersatz == "" then drv else ersatz;
  71. # Ports of functions for older nix versions
  72. # a Nix version of mapAttrs if the built-in doesn't exist
  73. mapAttrs = builtins.mapAttrs or (
  74. f: set: with builtins;
  75. listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
  76. );
  77. # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
  78. range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);
  79. # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
  80. stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
  81. # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
  82. stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
  83. concatStrings = builtins.concatStringsSep "";
  84. # fetchTarball version that is compatible between all the versions of Nix
  85. builtins_fetchTarball = { url, name, sha256 }@attrs:
  86. let
  87. inherit (builtins) lessThan nixVersion fetchTarball;
  88. in
  89. if lessThan nixVersion "1.12" then
  90. fetchTarball { inherit name url; }
  91. else
  92. fetchTarball attrs;
  93. # fetchurl version that is compatible between all the versions of Nix
  94. builtins_fetchurl = { url, sha256 }@attrs:
  95. let
  96. inherit (builtins) lessThan nixVersion fetchurl;
  97. in
  98. if lessThan nixVersion "1.12" then
  99. fetchurl { inherit url; }
  100. else
  101. fetchurl attrs;
  102. # Create the final "sources" from the config
  103. mkSources = config:
  104. mapAttrs (
  105. name: spec:
  106. if builtins.hasAttr "outPath" spec
  107. then abort
  108. "The values in sources.json should not have an 'outPath' attribute"
  109. else
  110. spec // { outPath = replace name (fetch config.pkgs name spec); }
  111. ) config.sources;
  112. # The "config" used by the fetchers
  113. mkConfig =
  114. { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
  115. , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
  116. , pkgs ? mkPkgs sources
  117. }: rec {
  118. # The sources, i.e. the attribute set of spec name to spec
  119. inherit sources;
  120. # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
  121. inherit pkgs;
  122. };
  123. in
  124. mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }