Skip to content

Instantly share code, notes, and snippets.

@layus
Last active February 3, 2018 00:14
Show Gist options
  • Save layus/5c334432cf3115f71f50c094279395b7 to your computer and use it in GitHub Desktop.
Save layus/5c334432cf3115f71f50c094279395b7 to your computer and use it in GitHub Desktop.
Prefetch nix packages and update files in place

This bash script updates a package version and source hash directly in the source files.

It uses unsafeGetAttrPos to find the files to edit, which in turn require to get access to the real attrset. This is why we have to define the overlays.

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p jshon diffutils
set -e
set -o pipefail
main () {
local drvAttr=$1
local newVersion='"'$2'"'
# Get the old hash _before_ bumping the version.
local oldHash=$(nixVal "$drvAttr.src.outputHash")
if [ "$newVersion" != '""' ]; then
log "Bumping version..."
local oldVersion=$(nixVal "$drvAttr._args.version")
local versionFile=$(nixAttrFile "$drvAttr._args" '"version"')
update "$versionFile" "$oldVersion" "$newVersion"
fi
log "Prefetching source..."
local newHash='"'$(nix-prefetch-url -A "$drvAttr.src")'"'
log "Rewriting hash..."
local hashMethod=$(nixVal "$drvAttr.src.outputHashAlgo")
local hashFile=$(nixAttrFile "$drvAttr.src._args" "$hashMethod")
update "$hashFile" "$oldHash" "$newHash"
}
update () {
local file=$1 old=$2 new=$3
[ -f "$file" ] || exit 1
diff -U3 "$file" <(sed -e "s/$old/$new/" "$file") --color || true
sed -e "s/$old/$new/" -i "$file"
}
nixVal () {
local expr=$1; shift
nix-instantiate --eval --strict -E "
#begin:nix
with (import ./. {
overlays = [ (self: super: with super; {
fetchurl = _args: (fetchurl _args) // { inherit _args; };
stdenv = stdenv // {
mkDerivation = _args: (stdenv.mkDerivation _args) // { inherit _args; };
};
})];
}).pkgs;
#end:nix
$expr" $@
}
nixAttrFile () {
nixVal "builtins.unsafeGetAttrPos $2 $1" --json | jshon -e file -u
}
log () { echo >&2; echo "$@" >&2; }
# Run the stuff!
main "$@"
~/projets/nixpkgs$ pkgs/build-support/fetchurl/prefetch.sh cups-filters 1.20.0
Bumping version...
--- /home/gmaudoux/projets/nixpkgs/pkgs/misc/cups/filters.nix 2018-01-31 17:11:16.448974182 +0100
+++ /dev/fd/63 2018-01-31 17:18:18.813967126 +0100
@@ -9,7 +9,7 @@
in stdenv.mkDerivation rec {
name = "cups-filters-${version}";
- version = "1.16.0";
+ version = "1.20.0";
src = fetchurl {
url = "http://openprinting.org/download/cups-filters/${name}.tar.xz";
Prefetching source...
path is '/nix/store/99hnv30fyc7vgp3aikl37sygf1by28bz-cups-filters-1.20.0.tar.xz'
Rewriting hash...
--- /home/gmaudoux/projets/nixpkgs/pkgs/misc/cups/filters.nix 2018-01-31 17:18:18.819967155 +0100
+++ /dev/fd/63 2018-01-31 17:18:21.356979118 +0100
@@ -13,7 +13,7 @@
src = fetchurl {
url = "http://openprinting.org/download/cups-filters/${name}.tar.xz";
- sha256 = "1kcndzpbbcaxafnz1wa6acy3p3r5likfqmf057i5q0q6i176lz5k";
+ sha256 = "0g6npicm1cwmxqi6ymfvf9wkplp4z2rzvjjl9v4yfvqdjq85gxnp";
};
nativeBuildInputs = [ pkgconfig makeWrapper ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment