2019-11-06 19:37:25 +00:00
#! /usr/bin/env nix-shell
2024-07-25 00:41:39 +01:00
#! nix-shell -i python -p python3Packages.looseversion nix nixfmt-classic nix-prefetch-git
2019-11-06 19:37:25 +00:00
2020-12-18 16:52:21 +00:00
""" This script automatically updates chromium, google-chrome, chromedriver, and ungoogled-chromium
2023-07-29 17:53:34 +01:00
via upstream - info . nix . """
2021-08-02 10:37:36 +01:00
# Usage: ./update.py [--commit]
2020-12-18 16:52:21 +00:00
2021-11-20 17:06:30 +00:00
import base64
2019-11-06 19:37:25 +00:00
import csv
import json
2020-11-03 12:08:09 +00:00
import re
2019-11-06 19:37:25 +00:00
import subprocess
2020-09-10 11:01:01 +01:00
import sys
2019-11-06 19:37:25 +00:00
from codecs import iterdecode
2020-09-10 11:01:01 +01:00
from collections import OrderedDict
2020-11-03 12:08:09 +00:00
from datetime import datetime
2024-07-25 00:41:39 +01:00
from looseversion import LooseVersion
2019-11-06 19:37:25 +00:00
from os . path import abspath , dirname
from urllib . request import urlopen
2023-04-07 22:33:45 +01:00
RELEASES_URL = ' https://versionhistory.googleapis.com/v1/chrome/platforms/linux/channels/all/versions/all/releases '
2019-11-06 19:37:25 +00:00
2023-07-29 17:53:34 +01:00
PIN_PATH = dirname ( abspath ( __file__ ) ) + ' /upstream-info.nix '
2022-02-05 19:02:07 +00:00
UNGOOGLED_FLAGS_PATH = dirname ( abspath ( __file__ ) ) + ' /ungoogled-flags.toml '
2021-08-02 10:37:36 +01:00
COMMIT_MESSAGE_SCRIPT = dirname ( abspath ( __file__ ) ) + ' /get-commit-message.py '
2023-10-19 21:20:19 +01:00
NIXPKGS_PATH = subprocess . check_output ( [ " git " , " rev-parse " , " --show-toplevel " ] , cwd = dirname ( PIN_PATH ) ) . strip ( )
2020-12-18 16:52:21 +00:00
2023-07-29 17:53:34 +01:00
def load_as_json ( path ) :
""" Loads the given nix file as JSON. """
out = subprocess . check_output ( [ ' nix-instantiate ' , ' --eval ' , ' --strict ' , ' --json ' , path ] )
return json . loads ( out )
2019-11-06 19:37:25 +00:00
2023-07-29 17:53:34 +01:00
def save_dict_as_nix ( path , input ) :
""" Saves the given dict/JSON as nix file. """
json_string = json . dumps ( input )
nix = subprocess . check_output ( [ ' nix-instantiate ' , ' --eval ' , ' --expr ' , ' { json }: builtins.fromJSON json ' , ' --argstr ' , ' json ' , json_string ] )
formatted = subprocess . check_output ( [ ' nixfmt ' ] , input = nix )
with open ( path , ' w ' ) as out :
out . write ( formatted . decode ( ) )
2020-12-18 16:52:21 +00:00
2023-10-19 21:20:19 +01:00
def prefetch_src_sri_hash ( attr_path , version ) :
""" Prefetches the fixed-output-derivation source tarball and returns its SRI-Hash. """
print ( f ' nix-build (FOD prefetch) { attr_path } { version } ' )
out = subprocess . run (
[ " nix-build " , " --expr " , f ' (import ./. {{ }} ). { attr_path } .browser.passthru.recompressTarball {{ version = " { version } " ; }} ' ] ,
cwd = NIXPKGS_PATH ,
stderr = subprocess . PIPE
) . stderr . decode ( )
for line in iter ( out . split ( " \n " ) ) :
match = re . match ( r " \ s+got: \ s+(.+)$ " , line )
if match :
print ( f ' Hash: { match . group ( 1 ) } ' )
return match . group ( 1 )
print ( f ' { out } \n \n Error: Expected hash in nix-build stderr output. ' , file = sys . stderr )
sys . exit ( 1 )
2019-11-06 19:37:25 +00:00
def nix_prefetch_url ( url , algo = ' sha256 ' ) :
2020-12-18 16:52:21 +00:00
""" Prefetches the content of the given URL. """
2023-10-21 11:58:07 +01:00
print ( f ' nix store prefetch-file { url } ' )
out = subprocess . check_output ( [ ' nix ' , ' store ' , ' prefetch-file ' , ' --json ' , ' --hash-type ' , algo , url ] )
return json . loads ( out ) [ ' hash ' ]
2019-11-06 19:37:25 +00:00
2020-12-18 16:52:21 +00:00
2020-11-03 12:08:09 +00:00
def nix_prefetch_git ( url , rev ) :
2020-12-18 16:52:21 +00:00
""" Prefetches the requested Git revision of the given repository URL. """
2020-11-03 12:08:09 +00:00
print ( f ' nix-prefetch-git { url } { rev } ' )
out = subprocess . check_output ( [ ' nix-prefetch-git ' , ' --quiet ' , ' --url ' , url , ' --rev ' , rev ] )
return json . loads ( out )
2020-12-18 16:52:21 +00:00
2020-11-03 12:08:09 +00:00
def get_file_revision ( revision , file_path ) :
2020-12-18 16:52:21 +00:00
""" Fetches the requested Git revision of the given Chromium file. """
2021-11-20 17:06:30 +00:00
url = f ' https://chromium.googlesource.com/chromium/src/+/refs/tags/ { revision } / { file_path } ?format=TEXT '
2020-11-03 12:08:09 +00:00
with urlopen ( url ) as http_response :
2021-11-20 17:06:30 +00:00
resp = http_response . read ( )
return base64 . b64decode ( resp )
2020-11-03 12:08:09 +00:00
2023-11-29 21:15:00 +00:00
def get_ungoogled_file_revision ( revision , file_path ) :
""" Fetches the requested Git revision of the given Chromium file. """
url = f ' https://raw.githubusercontent.com/ungoogled-software/ungoogled-chromium/ { revision } / { file_path } '
with urlopen ( url ) as http_response :
resp = http_response . read ( )
return resp . decode ( " utf-8 " )
2020-12-18 16:52:21 +00:00
2023-08-04 15:47:48 +01:00
def get_chromedriver ( channel ) :
""" Get the latest chromedriver builds given a channel """
# See https://chromedriver.chromium.org/downloads/version-selection#h.4wiyvw42q63v
chromedriver_versions_url = f ' https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json '
print ( f ' GET { chromedriver_versions_url } ' )
with urlopen ( chromedriver_versions_url ) as http_response :
chromedrivers = json . load ( http_response )
channel = chromedrivers [ ' channels ' ] [ channel ]
downloads = channel [ ' downloads ' ] [ ' chromedriver ' ]
def get_chromedriver_url ( platform ) :
for download in downloads :
if download [ ' platform ' ] == platform :
return download [ ' url ' ]
2020-11-27 11:38:07 +00:00
return {
2023-08-04 15:47:48 +01:00
' version ' : channel [ ' version ' ] ,
2023-10-21 11:58:07 +01:00
' hash_linux ' : nix_prefetch_url ( get_chromedriver_url ( ' linux64 ' ) ) ,
' hash_darwin ' : nix_prefetch_url ( get_chromedriver_url ( ' mac-x64 ' ) ) ,
' hash_darwin_aarch64 ' : nix_prefetch_url ( get_chromedriver_url ( ' mac-arm64 ' ) )
2020-11-27 11:38:07 +00:00
}
2020-12-18 16:52:21 +00:00
2020-12-18 18:08:37 +00:00
def get_channel_dependencies ( version ) :
2020-12-18 16:52:21 +00:00
""" Gets all dependencies for the given Chromium version. """
2020-12-18 18:08:37 +00:00
deps = get_file_revision ( version , ' DEPS ' )
2020-11-03 12:08:09 +00:00
gn_pattern = b " ' gn_version ' : ' git_revision:([0-9a-f] {40} ) ' "
gn_commit = re . search ( gn_pattern , deps ) . group ( 1 ) . decode ( )
gn = nix_prefetch_git ( ' https://gn.googlesource.com/gn ' , gn_commit )
return {
' gn ' : {
' version ' : datetime . fromisoformat ( gn [ ' date ' ] ) . date ( ) . isoformat ( ) ,
' url ' : gn [ ' url ' ] ,
' rev ' : gn [ ' rev ' ] ,
2023-10-21 11:58:07 +01:00
' hash ' : gn [ ' hash ' ]
2020-11-03 12:08:09 +00:00
}
}
2020-12-18 18:08:37 +00:00
2023-04-07 10:00:46 +01:00
def get_latest_ungoogled_chromium_tag ( linux_stable_versions ) :
""" Returns the latest ungoogled-chromium tag for linux using the GitHub API. """
2023-04-07 08:41:31 +01:00
api_tag_url = ' https://api.github.com/repos/ungoogled-software/ungoogled-chromium/tags '
2020-12-18 18:08:37 +00:00
with urlopen ( api_tag_url ) as http_response :
2023-04-07 10:00:46 +01:00
tags = json . load ( http_response )
for tag in tags :
if not tag [ ' name ' ] . split ( ' - ' ) [ 0 ] in linux_stable_versions :
continue
return tag [ ' name ' ]
2020-12-18 18:08:37 +00:00
2023-04-07 10:00:46 +01:00
def get_latest_ungoogled_chromium_build ( linux_stable_versions ) :
2020-12-22 12:21:12 +00:00
""" Returns a dictionary for the latest ungoogled-chromium build. """
2023-04-07 10:00:46 +01:00
tag = get_latest_ungoogled_chromium_tag ( linux_stable_versions )
2020-12-22 12:21:12 +00:00
version = tag . split ( ' - ' ) [ 0 ]
return {
2023-04-07 22:33:45 +01:00
' name ' : ' chrome/platforms/linux/channels/ungoogled-chromium/versions/ ' ,
2020-12-18 18:08:37 +00:00
' version ' : version ,
2023-11-29 21:15:00 +00:00
' ungoogled_rev ' : tag
}
def get_ungoogled_chromium_build_by_ref ( ungoogled_chromium_ref ) :
""" Returns a dictionary for an ungoogled-chromium build referenced by a ref in the ungoogled-chromium repository. """
version = get_ungoogled_file_revision ( ungoogled_chromium_ref , " chromium_version.txt " ) . strip ( " \n " )
return {
' name ' : ' chrome/platforms/linux/channels/ungoogled-chromium/versions/ ' ,
' version ' : version ,
' ungoogled_rev ' : ungoogled_chromium_ref
2020-12-18 18:08:37 +00:00
}
2022-02-05 19:02:07 +00:00
def get_ungoogled_chromium_gn_flags ( revision ) :
""" Returns ungoogled-chromium ' s GN build flags for the given revision. """
2023-04-07 08:41:31 +01:00
gn_flags_url = f ' https://raw.githubusercontent.com/ungoogled-software/ungoogled-chromium/ { revision } /flags.gn '
2022-02-05 19:02:07 +00:00
return urlopen ( gn_flags_url ) . read ( ) . decode ( )
2021-01-14 19:37:01 +00:00
def channel_name_to_attr_name ( channel_name ) :
""" Maps a channel name to the corresponding main Nixpkgs attribute name. """
if channel_name == ' stable ' :
return ' chromium '
if channel_name == ' ungoogled-chromium ' :
return ' ungoogled-chromium '
print ( f ' Error: Unexpected channel: { channel_name } ' , file = sys . stderr )
sys . exit ( 1 )
2021-08-02 10:37:36 +01:00
def get_channel_key ( item ) :
""" Orders Chromium channels by their name. """
channel_name = item [ 0 ]
if channel_name == ' stable ' :
return 0
if channel_name == ' beta ' :
return 1
if channel_name == ' dev ' :
return 2
if channel_name == ' ungoogled-chromium ' :
return 3
print ( f ' Error: Unexpected channel: { channel_name } ' , file = sys . stderr )
sys . exit ( 1 )
2021-01-14 19:37:01 +00:00
def print_updates ( channels_old , channels_new ) :
""" Print a summary of the updates. """
print ( ' Updates: ' )
for channel_name in channels_old :
version_old = channels_old [ channel_name ] [ " version " ]
version_new = channels_new [ channel_name ] [ " version " ]
2021-01-28 14:34:34 +00:00
if LooseVersion ( version_old ) < LooseVersion ( version_new ) :
2021-01-14 19:37:01 +00:00
attr_name = channel_name_to_attr_name ( channel_name )
print ( f ' - { attr_name } : { version_old } -> { version_new } ' )
2019-11-06 19:37:25 +00:00
channels = { }
2023-07-29 17:53:34 +01:00
last_channels = load_as_json ( PIN_PATH )
2019-11-06 19:37:25 +00:00
2024-02-13 23:14:47 +00:00
src_hash_cache = { }
2020-12-18 16:52:21 +00:00
2023-04-07 22:33:45 +01:00
print ( f ' GET { RELEASES_URL } ' , file = sys . stderr )
with urlopen ( RELEASES_URL ) as resp :
releases = json . load ( resp ) [ ' releases ' ]
2023-04-07 10:00:46 +01:00
2023-11-29 21:15:00 +00:00
if len ( sys . argv ) == 3 and sys . argv [ 1 ] == ' ungoogled-rev ' :
releases . append ( get_ungoogled_chromium_build_by_ref ( sys . argv [ 2 ] ) )
else :
linux_stable_versions = [ release [ ' version ' ] for release in releases if release [ ' name ' ] . startswith ( ' chrome/platforms/linux/channels/stable/versions/ ' ) ]
releases . append ( get_latest_ungoogled_chromium_build ( linux_stable_versions ) )
2023-04-07 10:00:46 +01:00
2023-04-07 22:33:45 +01:00
for release in releases :
2024-07-25 00:41:39 +01:00
channel_name = re . findall ( " chrome/platforms/linux/channels/(.*)/versions/ " , release [ ' name ' ] ) [ 0 ]
2019-11-06 19:37:25 +00:00
2023-04-07 22:33:45 +01:00
# If we've already found a newer release for this channel, we're
2019-11-06 19:37:25 +00:00
# no longer interested in it.
if channel_name in channels :
continue
chromium{Beta,Dev},google-chrome-{beta,dev}: drop
This is mainly due to the lack of maintenance in nixpkgs.
`google-chrome-{beta,dev}` depend on `chromium{Beta,Dev}`'s version
info.
`chromium{Beta,Dev}` are rarely updated and explicitly blocklisted by
`hydra.nixos.org`, meaning they are almost always outdated and not
cached in `cache.nixos.org`.
`chromium{Beta,Dev}` were intended to fix the build derivation of each
new major release (if something broke) *before* stable reached that
new major release.
Allowing for fast bumps in nixpkgs, especially if the stable bump
contains very important critical security fixes.
Something that can easily be replicated by using an early-stable release
or by manually entering a dev/beta version string in stable's
`upstream-info.nix`.
This resolves exposing end-users to outdated and vulnerable
`google-chrome-{beta,dev}` and `chromium{Beta,Dev}` versions.
2023-10-18 18:00:09 +01:00
# We only look for channels that are listed in our version pin file.
if channel_name not in last_channels :
continue
2023-04-07 22:33:45 +01:00
# If we're back at the last release we used, we don't need to
2019-11-06 19:37:25 +00:00
# keep going -- there's no new version available, and we can
# just reuse the info from last time.
2023-04-07 22:33:45 +01:00
if release [ ' version ' ] == last_channels [ channel_name ] [ ' version ' ] :
2019-11-06 19:37:25 +00:00
channels [ channel_name ] = last_channels [ channel_name ]
continue
2023-04-07 22:33:45 +01:00
channel = { ' version ' : release [ ' version ' ] }
2020-12-22 12:21:12 +00:00
if channel_name == ' dev ' :
google_chrome_suffix = ' unstable '
elif channel_name == ' ungoogled-chromium ' :
google_chrome_suffix = ' stable '
else :
google_chrome_suffix = channel_name
2019-11-06 19:37:25 +00:00
try :
2024-02-13 23:14:47 +00:00
version = release [ " version " ]
2024-02-14 01:31:26 +00:00
existing_releases = dict ( map ( lambda channel : ( channel [ 1 ] [ ' version ' ] , channel [ 1 ] [ ' hash ' ] ) , last_channels . items ( ) ) )
2024-02-13 23:14:47 +00:00
if version in src_hash_cache :
print ( f ' Already got hash { src_hash_cache [ version ] } for { version } , skipping FOD prefetch for { channel_name_to_attr_name ( channel_name ) } ' )
channel [ " hash " ] = src_hash_cache [ version ]
2024-02-14 01:31:26 +00:00
elif version in existing_releases :
print ( f ' Already got hash { existing_releases [ version ] } for { version } (from upstream-info.nix), skipping FOD prefetch for { channel_name_to_attr_name ( channel_name ) } ' )
channel [ " hash " ] = existing_releases [ version ]
2024-02-13 23:14:47 +00:00
else :
channel [ " hash " ] = prefetch_src_sri_hash (
channel_name_to_attr_name ( channel_name ) ,
version
)
src_hash_cache [ version ] = channel [ " hash " ]
2019-11-06 19:37:25 +00:00
except subprocess . CalledProcessError :
2023-04-07 10:00:46 +01:00
# This release isn't actually available yet. Continue to
# the next one.
continue
2019-11-06 19:37:25 +00:00
2020-12-18 18:08:37 +00:00
channel [ ' deps ' ] = get_channel_dependencies ( channel [ ' version ' ] )
2020-11-27 11:38:07 +00:00
if channel_name == ' stable ' :
2023-08-04 15:47:48 +01:00
channel [ ' chromedriver ' ] = get_chromedriver ( ' Stable ' )
2020-12-22 12:21:12 +00:00
elif channel_name == ' ungoogled-chromium ' :
2023-04-07 08:41:31 +01:00
ungoogled_repo_url = ' https://github.com/ungoogled-software/ungoogled-chromium.git '
2020-12-22 12:21:12 +00:00
channel [ ' deps ' ] [ ' ungoogled-patches ' ] = {
2023-11-29 21:15:00 +00:00
' rev ' : release [ ' ungoogled_rev ' ] ,
' hash ' : nix_prefetch_git ( ungoogled_repo_url , release [ ' ungoogled_rev ' ] ) [ ' hash ' ]
2020-12-22 12:21:12 +00:00
}
2022-02-05 19:02:07 +00:00
with open ( UNGOOGLED_FLAGS_PATH , ' w ' ) as out :
2023-11-29 21:15:00 +00:00
out . write ( get_ungoogled_chromium_gn_flags ( release [ ' ungoogled_rev ' ] ) )
2020-11-03 12:08:09 +00:00
2019-11-06 19:37:25 +00:00
channels [ channel_name ] = channel
2020-12-18 16:52:21 +00:00
2021-08-02 10:37:36 +01:00
sorted_channels = OrderedDict ( sorted ( channels . items ( ) , key = get_channel_key ) )
if len ( sys . argv ) == 2 and sys . argv [ 1 ] == ' --commit ' :
for channel_name in sorted_channels . keys ( ) :
version_old = last_channels [ channel_name ] [ ' version ' ]
version_new = sorted_channels [ channel_name ] [ ' version ' ]
if LooseVersion ( version_old ) < LooseVersion ( version_new ) :
last_channels [ channel_name ] = sorted_channels [ channel_name ]
2023-07-29 17:53:34 +01:00
save_dict_as_nix ( PIN_PATH , last_channels )
2021-08-02 10:37:36 +01:00
attr_name = channel_name_to_attr_name ( channel_name )
commit_message = f ' { attr_name } : { version_old } -> { version_new } '
if channel_name == ' stable ' :
2021-08-03 12:20:15 +01:00
body = subprocess . check_output ( [ COMMIT_MESSAGE_SCRIPT , version_new ] ) . decode ( ' utf-8 ' )
commit_message + = ' \n \n ' + body
2022-02-05 19:02:07 +00:00
elif channel_name == ' ungoogled-chromium ' :
subprocess . run ( [ ' git ' , ' add ' , UNGOOGLED_FLAGS_PATH ] , check = True )
2021-08-02 10:37:36 +01:00
subprocess . run ( [ ' git ' , ' add ' , JSON_PATH ] , check = True )
subprocess . run ( [ ' git ' , ' commit ' , ' --file=- ' ] , input = commit_message . encode ( ) , check = True )
else :
2023-07-29 17:53:34 +01:00
save_dict_as_nix ( PIN_PATH , sorted_channels )
2021-01-14 19:37:01 +00:00
print_updates ( last_channels , sorted_channels )