scripts/update: support gitUpdater-style updateScripts

This commit is contained in:
2024-07-21 07:26:39 +00:00
parent cd16f8c3b6
commit 0af5e43944
2 changed files with 35 additions and 20 deletions

View File

@@ -58,6 +58,8 @@ let
{ name = "servo"; system = "x86_64-linux"; }
];
pkgs = mkPkgs {};
subAttrs = attrs: lib.filterAttrs (name: value: builtins.isAttrs value) attrs;
subAttrNames = attrs: builtins.attrNames (subAttrs attrs);
@@ -72,16 +74,39 @@ let
# a package can set `passthru.updateWithSuper = false;` if it doesn't want to be auto-updated.
mkUpdateListIfAuto = prefix: pkg: lib.optionals (pkg.updateWithSuper or true) (mkUpdateList prefix pkg);
mkUpdateTargets = prefix: pkg: let
updateList = mkUpdateList prefix pkg;
in lib.optionalAttrs (updateList != []) {
"${prefix}" = mkUpdateList prefix pkg;
mkUpdateInfo = prefix: pkg: {
"${prefix}" = rec {
subPackages = mkUpdateList prefix pkg;
updateArgv = lib.optionals (pkg ? updateScript) (
if builtins.isList pkg.updateScript then pkg.updateScript
else if pkg.updateScript ? command then lib.map builtins.toString pkg.updateScript.command
else []
);
updateScript = let
pname = pkg.pname or (pkg.name or "unknown");
script = pkgs.writeShellScriptBin "update-${pname}" ''
# update script assumes $PWD is an entry point to a writable copy of my nix config,
# so provide that:
pushd /home/colin/nixos/integrations/nix-update
UPDATE_NIX_NAME=${pkg.name or ""} \
UPDATE_NIX_PNAME=${pkg.pname or ""} \
UPDATE_NIX_OLD_VERSION=${pkg.version or ""} \
UPDATE_NIX_ATTR_PATH=${prefix} \
${lib.escapeShellArgs updateArgv}
popd
'';
in lib.optionalString (updateArgv != []) (lib.getExe script);
};
} // lib.foldl'
(acc: subPkgName: acc // mkUpdateTargets "${prefix}.${subPkgName}" pkg."${subPkgName}")
(acc: subPkgName: acc // mkUpdateInfo "${prefix}.${subPkgName}" pkg."${subPkgName}")
{}
(if pkg.recurseForDerivations or false then subAttrNames pkg else [])
;
updateInfo = mkUpdateInfo "sane" pkgs.sane;
in {
inherit hosts;
updateTargets = mkUpdateTargets "sane" (mkPkgs {}).sane;
} // mkPkgs {}
inherit updateInfo;
updateTargets = builtins.mapAttrs (_: v: v.subPackages) (lib.filterAttrs (_: v: v.subPackages != []) updateInfo);
updateScripts = builtins.mapAttrs (_: v: v.updateScript) (lib.filterAttrs (_: v: v.updateScript != "") updateInfo);
} // pkgs

View File

@@ -61,32 +61,22 @@ getPkgs() {
updateOnePkg() {
local attrPath="$1"
local updateScript=$(nix eval --raw -f "$NIX_FILES_TOP" $attrPath.passthru.updateScript --apply 'builtins.concatStringsSep " "')
local updateScript=$(nix eval --raw -f "$NIX_FILES_TOP" 'updateScripts."'"$attrPath"'"')
if [ -z "$updateScript" ]; then
warn "don't know how to update '$attrPath'"
return
fi
# make sure everything needed to invoke the update script exists in-store
local context=$(nix eval --raw -f "$NIX_FILES_TOP" $attrPath.passthru.updateScript --apply 's: builtins.concatStringsSep " " (builtins.foldl'"'"' (acc: next: acc ++ next) [] (builtins.map builtins.attrNames (builtins.map builtins.getContext s)))')
local context=$(nix eval --raw -f "$NIX_FILES_TOP" 'updateScripts."'"$attrPath"'"' --apply 's: builtins.concatStringsSep " " (builtins.attrNames (builtins.getContext s))')
for c in $context; do
debug "realizing updateScript requisite: $context"
nix-store --realize "$c" || true
done
local UPDATE_NIX_NAME=$(nix eval --raw -f "$NIX_FILES_TOP" $attrPath.name)
local UPDATE_NIX_PNAME=$(nix eval --raw -f "$NIX_FILES_TOP" $attrPath.pname)
local UPDATE_NIX_OLD_VERSION=$(nix eval --raw -f "$NIX_FILES_TOP" $attrPath.version)
info "updating: '$attrPath'"
debug "$updateScript"
(
# update script assumes $PWD is an entry point to a writable copy of my nix config,
# so provide that:
pushd "$NIX_FILES_TOP/integrations/nix-update"
# we lose spaces inside the exec args... could `nix eval` without `--raw` to fix that.
UPDATE_NIX_NAME="$UPDATE_NIX_NAME" UPDATE_NIX_PNAME="$UPDATE_NIX_PNAME" UPDATE_NIX_OLD_VERSION="$UPDATE_NIX_OLD_VERSION" UPDATE_NIX_ATTR_PATH="$attrPath" eval $updateScript
popd
)
"$updateScript"
}
updatePkgsInParallel() {