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.

update.nix 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. { package ? null
  2. , maintainer ? null
  3. , path ? null
  4. }:
  5. # TODO: add assert statements
  6. let
  7. /* Remove duplicate elements from the list based on some extracted value. O(n^2) complexity.
  8. */
  9. nubOn = f: list:
  10. if list == [] then
  11. []
  12. else
  13. let
  14. x = pkgs.lib.head list;
  15. xs = pkgs.lib.filter (p: f x != f p) (pkgs.lib.drop 1 list);
  16. in
  17. [x] ++ nubOn f xs;
  18. pkgs = import ./../../default.nix { };
  19. packagesWith = cond: return: set:
  20. nubOn (pkg: pkg.updateScript)
  21. (pkgs.lib.flatten
  22. (pkgs.lib.mapAttrsToList
  23. (name: pkg:
  24. let
  25. result = builtins.tryEval (
  26. if pkgs.lib.isDerivation pkg && cond name pkg
  27. then [(return name pkg)]
  28. else if pkg.recurseForDerivations or false || pkg.recurseForRelease or false
  29. then packagesWith cond return pkg
  30. else []
  31. );
  32. in
  33. if result.success then result.value
  34. else []
  35. )
  36. set
  37. )
  38. );
  39. packagesWithUpdateScriptAndMaintainer = maintainer':
  40. let
  41. maintainer =
  42. if ! builtins.hasAttr maintainer' pkgs.lib.maintainers then
  43. builtins.throw "Maintainer with name `${maintainer'} does not exist in `maintainers/maintainer-list.nix`."
  44. else
  45. builtins.getAttr maintainer' pkgs.lib.maintainers;
  46. in
  47. packagesWith (name: pkg: builtins.hasAttr "updateScript" pkg &&
  48. (if builtins.hasAttr "maintainers" pkg.meta
  49. then (if builtins.isList pkg.meta.maintainers
  50. then builtins.elem maintainer pkg.meta.maintainers
  51. else maintainer == pkg.meta.maintainers
  52. )
  53. else false
  54. )
  55. )
  56. (name: pkg: pkg)
  57. pkgs;
  58. packagesWithUpdateScript = path:
  59. let
  60. attrSet = pkgs.lib.attrByPath (pkgs.lib.splitString "." path) null pkgs;
  61. in
  62. packagesWith (name: pkg: builtins.hasAttr "updateScript" pkg)
  63. (name: pkg: pkg)
  64. attrSet;
  65. packageByName = name:
  66. let
  67. package = pkgs.lib.attrByPath (pkgs.lib.splitString "." name) null pkgs;
  68. in
  69. if package == null then
  70. builtins.throw "Package with an attribute name `${name}` does not exists."
  71. else if ! builtins.hasAttr "updateScript" package then
  72. builtins.throw "Package with an attribute name `${name}` does not have a `passthru.updateScript` attribute defined."
  73. else
  74. package;
  75. packages =
  76. if package != null then
  77. [ (packageByName package) ]
  78. else if maintainer != null then
  79. packagesWithUpdateScriptAndMaintainer maintainer
  80. else if path != null then
  81. packagesWithUpdateScript path
  82. else
  83. builtins.throw "No arguments provided.\n\n${helpText}";
  84. helpText = ''
  85. Please run:
  86. % nix-shell maintainers/scripts/update.nix --argstr maintainer garbas
  87. to run all update scripts for all packages that lists \`garbas\` as a maintainer
  88. and have \`updateScript\` defined, or:
  89. % nix-shell maintainers/scripts/update.nix --argstr package garbas
  90. to run update script for specific package, or
  91. % nix-shell maintainers/scripts/update.nix --argstr path gnome3
  92. to run update script for all package under an attribute path.
  93. '';
  94. runUpdateScript = package: ''
  95. echo -ne " - ${package.name}: UPDATING ..."\\r
  96. ${package.updateScript} &> ${(builtins.parseDrvName package.name).name}.log
  97. CODE=$?
  98. if [ "$CODE" != "0" ]; then
  99. echo " - ${package.name}: ERROR "
  100. echo ""
  101. echo "--- SHOWING ERROR LOG FOR ${package.name} ----------------------"
  102. echo ""
  103. cat ${(builtins.parseDrvName package.name).name}.log
  104. echo ""
  105. echo "--- SHOWING ERROR LOG FOR ${package.name} ----------------------"
  106. exit $CODE
  107. else
  108. rm ${(builtins.parseDrvName package.name).name}.log
  109. fi
  110. echo " - ${package.name}: DONE. "
  111. '';
  112. in pkgs.stdenv.mkDerivation {
  113. name = "nixpkgs-update-script";
  114. buildCommand = ''
  115. echo ""
  116. echo "----------------------------------------------------------------"
  117. echo ""
  118. echo "Not possible to update packages using \`nix-build\`"
  119. echo ""
  120. echo "${helpText}"
  121. echo "----------------------------------------------------------------"
  122. exit 1
  123. '';
  124. shellHook = ''
  125. echo ""
  126. echo "Going to be running update for following packages:"
  127. echo "${builtins.concatStringsSep "\n" (map (x: " - ${x.name}") packages)}"
  128. echo ""
  129. read -n1 -r -p "Press space to continue..." confirm
  130. if [ "$confirm" = "" ]; then
  131. echo ""
  132. echo "Running update for:"
  133. ${builtins.concatStringsSep "\n" (map runUpdateScript packages)}
  134. echo ""
  135. echo "Packages updated!"
  136. exit 0
  137. else
  138. echo "Aborting!"
  139. exit 1
  140. fi
  141. '';
  142. }