builtins.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright (C) 2016, 2017 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. /* XXX: Hack our way to use the 'download' script from 'LIBEXECDIR/guix'
  39. or just 'LIBEXECDIR', depending on whether we're running uninstalled or
  40. not. */
  41. const string subdir = getenv("GUIX_UNINSTALLED") != NULL
  42. ? "" : "/guix";
  43. const string program = settings.nixLibexecDir + subdir + "/download";
  44. execv(program.c_str(), (char *const *) argv);
  45. throw SysError(format("failed to run download program '%1%'") % program);
  46. }
  47. static const std::map<std::string, derivationBuilder> builtins =
  48. {
  49. { "download", builtinDownload }
  50. };
  51. derivationBuilder lookupBuiltinBuilder(const std::string & name)
  52. {
  53. if (name.substr(0, 8) == "builtin:")
  54. {
  55. auto realName = name.substr(8);
  56. auto builder = builtins.find(realName);
  57. return builder == builtins.end() ? NULL : builder->second;
  58. }
  59. else
  60. return NULL;
  61. }
  62. std::list<std::string> builtinBuilderNames()
  63. {
  64. std::list<std::string> result;
  65. for(auto&& iter: builtins)
  66. {
  67. result.push_back(iter.first);
  68. }
  69. return result;
  70. }
  71. }