builtins.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright (C) 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <builtins.hh>
  15. #include <util.hh>
  16. #include <globals.hh>
  17. #include <unistd.h>
  18. #include <cstdlib>
  19. namespace nix {
  20. static void builtinDownload(const Derivation &drv,
  21. const std::string &drvPath,
  22. const std::string &output)
  23. {
  24. /* Invoke 'guix perform-download'. */
  25. Strings args;
  26. args.push_back("perform-download");
  27. args.push_back(drvPath);
  28. /* Close all other file descriptors. */
  29. closeMostFDs(set<int>());
  30. const char *const argv[] =
  31. {
  32. "download", drvPath.c_str(), output.c_str(), NULL
  33. };
  34. /* Tell the script what the store file name is, so that
  35. 'strip-store-file-name' (used for instance to determine the URL of
  36. content-addressed mirrors) works correctly. */
  37. setenv("NIX_STORE", settings.nixStore.c_str(), 1);
  38. /* Tell it about options such as "print-extended-build-trace". */
  39. setenv("_NIX_OPTIONS", settings.pack().c_str(), 1);
  40. const string program = settings.nixLibexecDir + "/download";
  41. execv(program.c_str(), (char *const *) argv);
  42. throw SysError(format("failed to run download program '%1%'") % program);
  43. }
  44. static const std::map<std::string, derivationBuilder> builtins =
  45. {
  46. { "download", builtinDownload }
  47. };
  48. derivationBuilder lookupBuiltinBuilder(const std::string & name)
  49. {
  50. if (name.substr(0, 8) == "builtin:")
  51. {
  52. auto realName = name.substr(8);
  53. auto builder = builtins.find(realName);
  54. return builder == builtins.end() ? NULL : builder->second;
  55. }
  56. else
  57. return NULL;
  58. }
  59. std::list<std::string> builtinBuilderNames()
  60. {
  61. std::list<std::string> result;
  62. for(auto&& iter: builtins)
  63. {
  64. result.push_back(iter.first);
  65. }
  66. return result;
  67. }
  68. }