sources.nix 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: spec:
  12. if spec.builtin or true then
  13. builtins_fetchTarball { inherit (spec) url sha256; }
  14. else
  15. pkgs.fetchzip { inherit (spec) url sha256; };
  16. fetch_git = spec:
  17. builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
  18. fetch_builtin-tarball = spec:
  19. builtins.trace
  20. ''
  21. WARNING:
  22. The niv type "builtin-tarball" will soon be deprecated. You should
  23. instead use `builtin = true`.
  24. $ niv modify <package> -a type=tarball -a builtin=true
  25. ''
  26. builtins_fetchTarball { inherit (spec) url sha256; };
  27. fetch_builtin-url = spec:
  28. builtins.trace
  29. ''
  30. WARNING:
  31. The niv type "builtin-url" will soon be deprecated. You should
  32. instead use `builtin = true`.
  33. $ niv modify <package> -a type=file -a builtin=true
  34. ''
  35. (builtins_fetchurl { inherit (spec) url sha256; });
  36. #
  37. # Various helpers
  38. #
  39. # The set of packages used when specs are fetched using non-builtins.
  40. mkPkgs = sources:
  41. let
  42. sourcesNixpkgs =
  43. import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
  44. hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
  45. hasThisAsNixpkgsPath = <nixpkgs> == ./.;
  46. in
  47. if builtins.hasAttr "nixpkgs" sources
  48. then sourcesNixpkgs
  49. else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
  50. import <nixpkgs> {}
  51. else
  52. abort
  53. ''
  54. Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
  55. add a package called "nixpkgs" to your sources.json.
  56. '';
  57. # The actual fetching function.
  58. fetch = pkgs: name: spec:
  59. if ! builtins.hasAttr "type" spec then
  60. abort "ERROR: niv spec ${name} does not have a 'type' attribute"
  61. else if spec.type == "file" then fetch_file pkgs spec
  62. else if spec.type == "tarball" then fetch_tarball pkgs spec
  63. else if spec.type == "git" then fetch_git spec
  64. else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
  65. else if spec.type == "builtin-url" then fetch_builtin-url spec
  66. else
  67. abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
  68. # Ports of functions for older nix versions
  69. # a Nix version of mapAttrs if the built-in doesn't exist
  70. mapAttrs = builtins.mapAttrs or (
  71. f: set: with builtins;
  72. listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
  73. );
  74. # fetchTarball version that is compatible between all the versions of Nix
  75. builtins_fetchTarball = { url, sha256 }@attrs:
  76. let
  77. inherit (builtins) lessThan nixVersion fetchTarball;
  78. in
  79. if lessThan nixVersion "1.12" then
  80. fetchTarball { inherit url; }
  81. else
  82. fetchTarball attrs;
  83. # fetchurl version that is compatible between all the versions of Nix
  84. builtins_fetchurl = { url, sha256 }@attrs:
  85. let
  86. inherit (builtins) lessThan nixVersion fetchurl;
  87. in
  88. if lessThan nixVersion "1.12" then
  89. fetchurl { inherit url; }
  90. else
  91. fetchurl attrs;
  92. # Create the final "sources" from the config
  93. mkSources = config:
  94. mapAttrs (
  95. name: spec:
  96. if builtins.hasAttr "outPath" spec
  97. then abort
  98. "The values in sources.json should not have an 'outPath' attribute"
  99. else
  100. spec // { outPath = fetch config.pkgs name spec; }
  101. ) config.sources;
  102. # The "config" used by the fetchers
  103. mkConfig =
  104. { sourcesFile ? ./sources.json
  105. , sources ? builtins.fromJSON (builtins.readFile sourcesFile)
  106. , pkgs ? mkPkgs sources
  107. }: rec {
  108. # The sources, i.e. the attribute set of spec name to spec
  109. inherit sources;
  110. # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
  111. inherit pkgs;
  112. };
  113. in
  114. mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }